php - PHP循环数据并获取键/值对
问题描述
我正在使用 Laravel 8 循环一些数据。我已经从我的数据库中取出了我的数据,但是我正在努力循环遍历数据foreach
,它似乎没有给我键/值对,只有索引/值对?
$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) "*"
}
}
}
}
我如何访问它?看来我的查询也将整行键/值作为单个对象字符串返回...
解决方案
您将获得从数据库返回的每个项目的集合。这些项目中的每一个都是 ApplicationOptions 的实例,键是集合中的索引。并且与数据库无关。
在 laravel 中,你有几种方法可以调用 Collection。您的数据库将返回 Illuminate/Database/Eloquent/Collection 的实例 您可以在此处阅读收集方法。
如果您希望检索集合中的每个项目的单个属性值,您可以在结果上使用 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');
最后,请注意,当您尝试将 Model 实例转换为字符串时,laravel 会调用模型上的 toJson 方法。因此,为什么您会在回声中获得 json_encoded 实例。
推荐阅读
- python - 将转换应用于所有列的最佳方法 - Pyspark 数据框
- r - 在 R 中用 gg.gap 断轴
- json - Python 3.6+ 如何在 while 循环中立即创建 json 转储文件。它仅在 while 循环终止后添加
- python - 如何使用 Python 检测计算机安装了多少内存?
- javascript - React JS:组件未与自定义路由组件一起显示?
- python - VSCode Pylance 更改了文本颜色
- java - 如何在 javafx 阶段设置背景图像
- excel - 更改工作表中随机单元格的颜色
- java - 在使用“gradle jar”创建 jar 之前是否应该“gradle clean”?
- xml - 如何在固定结构中解组动态 XML?