PHP 遍历数据并获得 key/value 对
PHP looping over data and getting key/value pairs
我正在使用 Laravel 8 来循环一些数据。我已经从我的数据库中取出了我的数据,但是我正在努力在 foreach
循环中循环数据,它似乎没有给我 key/value 对,只有 index/value对?
$applicationOptions = ApplicationOptions::where('form_type', $form_type)->get();
foreach ($applicationOptions as $key => $value) {
// only gives me: 0 - {"id": "3", "name": "john", "age": "20"} ...
echo "${key} - ${value}";
}
但是如果我var_dump()
我的$applicationOptions
我可以看到数组里面的属性和原来的??
object(Illuminate\Database\Eloquent\Collection)#279 (1) {
["items":protected]=>
array(1) {
[0]=>
object(Company\MyPackage\ApplicationOptions)#289 (27) {
["table":protected]=>
string(38) "inbound_management_application_options"
["connection":protected]=>
string(5) "mysql"
["primaryKey":protected]=>
string(2) "id"
["keyType":protected]=>
string(3) "int"
["incrementing"]=>
bool(true)
["with":protected]=>
array(0) {
}
["withCount":protected]=>
array(0) {
}
["perPage":protected]=>
int(15)
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
["attributes":protected]=>
array(22) {
["id"]=>
int(3)
["name"]=>
string(6) "john"
["age"]=>
int(20)
}
["original":protected]=>
array(22) {
["id"]=>
int(3)
["name"]=>
string(6) "john"
["age"]=>
int(20)
}
["changes":protected]=>
array(0) {
}
["casts":protected]=>
array(0) {
}
["classCastCache":protected]=>
array(0) {
}
["dates":protected]=>
array(0) {
}
["dateFormat":protected]=>
NULL
["appends":protected]=>
array(0) {
}
["dispatchesEvents":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["relations":protected]=>
array(0) {
}
["touches":protected]=>
array(0) {
}
["timestamps"]=>
bool(true)
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["fillable":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
}
}
}
我如何访问它?看来我的查询也将整行 key/value 作为单个对象字符串返回...
这是因为您正试图遍历一个集合,它有多个级别,而包装器只是对象类型。
改用 each
方法以获得更简洁的方法。
ApplicationOptions::where('form_type', $form_type)->get()->each(function ($option) {
echo $option->name:
};
您从数据库中获得了每个项目的集合 return。
这些项目中的每一个都是 ApplicationOptions 的一个实例
键是集合中的索引。与数据库无关。
在 laravel 中,您有几种可以调用集合的方法。
您的数据库将 return 是 Illuminate/Database/Eloquent/Collection 的一个实例
您可以阅读收集方法 here.
如果您希望检索集合中每个项目的单个属性值,您可以对结果使用 pluck 方法
$values = $applicationOptions->pluck('attributeName');
或者,您可以使用 each 方法遍历您的集合
$applicationOptions->each(function (ApplicationOptions $applicationOption) {
// insert any logic you want to do.
echo $applicationOption->attributeName;
})
如果你想使用结果,map会是更好的选择,你也可以使用mapWithKeys
$values = $applicationOptions->map(function (ApplicationOptions $applicationOption) {
// You could also loop through the ApplicationOptions's attributes here using a foreach incase you wish to do something with it.
return $applicationOption->attributeName;
})
$valuesWithKeys = $applicationOptions->mapWithKeys(function (ApplicationOptions $applicationOption) {
return [$applicationOption->attributeNameYouWantToUseForKey => $applicationOption->attributeNameForValue];
})
如果您只想按特定值进行键控,可以在 sql 查询中执行此操作,或者在集合中使用 keyBy 方法
$values = $applicationOptions->keyBy('attributeName');
最后,请注意,当您尝试将模型实例转换为字符串时,laravel 会调用模型的 toJson 方法。因此,为什么你在 echo 中得到 json_encoded 实例。
我正在使用 Laravel 8 来循环一些数据。我已经从我的数据库中取出了我的数据,但是我正在努力在 foreach
循环中循环数据,它似乎没有给我 key/value 对,只有 index/value对?
$applicationOptions = ApplicationOptions::where('form_type', $form_type)->get();
foreach ($applicationOptions as $key => $value) {
// only gives me: 0 - {"id": "3", "name": "john", "age": "20"} ...
echo "${key} - ${value}";
}
但是如果我var_dump()
我的$applicationOptions
我可以看到数组里面的属性和原来的??
object(Illuminate\Database\Eloquent\Collection)#279 (1) {
["items":protected]=>
array(1) {
[0]=>
object(Company\MyPackage\ApplicationOptions)#289 (27) {
["table":protected]=>
string(38) "inbound_management_application_options"
["connection":protected]=>
string(5) "mysql"
["primaryKey":protected]=>
string(2) "id"
["keyType":protected]=>
string(3) "int"
["incrementing"]=>
bool(true)
["with":protected]=>
array(0) {
}
["withCount":protected]=>
array(0) {
}
["perPage":protected]=>
int(15)
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
["attributes":protected]=>
array(22) {
["id"]=>
int(3)
["name"]=>
string(6) "john"
["age"]=>
int(20)
}
["original":protected]=>
array(22) {
["id"]=>
int(3)
["name"]=>
string(6) "john"
["age"]=>
int(20)
}
["changes":protected]=>
array(0) {
}
["casts":protected]=>
array(0) {
}
["classCastCache":protected]=>
array(0) {
}
["dates":protected]=>
array(0) {
}
["dateFormat":protected]=>
NULL
["appends":protected]=>
array(0) {
}
["dispatchesEvents":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["relations":protected]=>
array(0) {
}
["touches":protected]=>
array(0) {
}
["timestamps"]=>
bool(true)
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["fillable":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
}
}
}
我如何访问它?看来我的查询也将整行 key/value 作为单个对象字符串返回...
这是因为您正试图遍历一个集合,它有多个级别,而包装器只是对象类型。
改用 each
方法以获得更简洁的方法。
ApplicationOptions::where('form_type', $form_type)->get()->each(function ($option) {
echo $option->name:
};
您从数据库中获得了每个项目的集合 return。 这些项目中的每一个都是 ApplicationOptions 的一个实例 键是集合中的索引。与数据库无关。
在 laravel 中,您有几种可以调用集合的方法。 您的数据库将 return 是 Illuminate/Database/Eloquent/Collection 的一个实例 您可以阅读收集方法 here.
如果您希望检索集合中每个项目的单个属性值,您可以对结果使用 pluck 方法
$values = $applicationOptions->pluck('attributeName');
或者,您可以使用 each 方法遍历您的集合
$applicationOptions->each(function (ApplicationOptions $applicationOption) {
// insert any logic you want to do.
echo $applicationOption->attributeName;
})
如果你想使用结果,map会是更好的选择,你也可以使用mapWithKeys
$values = $applicationOptions->map(function (ApplicationOptions $applicationOption) {
// You could also loop through the ApplicationOptions's attributes here using a foreach incase you wish to do something with it.
return $applicationOption->attributeName;
})
$valuesWithKeys = $applicationOptions->mapWithKeys(function (ApplicationOptions $applicationOption) {
return [$applicationOption->attributeNameYouWantToUseForKey => $applicationOption->attributeNameForValue];
})
如果您只想按特定值进行键控,可以在 sql 查询中执行此操作,或者在集合中使用 keyBy 方法
$values = $applicationOptions->keyBy('attributeName');
最后,请注意,当您尝试将模型实例转换为字符串时,laravel 会调用模型的 toJson 方法。因此,为什么你在 echo 中得到 json_encoded 实例。