这是我的数组:
[2555] => Array
(
[0] => stdClass Object
(
[meta_id] => 1246
[post_id] => 2555
[meta_key] => event_date
[meta_value] => Sept 24th - 29th
)
[1] => stdClass Object
(
[meta_id] => 1245
[post_id] => 2555
[meta_key] => _edit_last
[meta_value] => 1
)
[2] => stdClass Object
(
[meta_id] => 1244
[post_id] => 2555
[meta_key] => _edit_lock
[meta_value] => 1252519100
)
[3] => stdClass Object
(
[meta_id] => 1251
[post_id] => 2555
[meta_key] => articleimg
[meta_value] => /image1.jpg
)
)
[2038] => Array
(
[0] => stdClass Object
(
[meta_id] => 462
[post_id] => 2038
[meta_key] => articleimg
[meta_value] => /image2.jpg
)
[1] => stdClass Object
(
[meta_id] => 463
[post_id] => 2038
[meta_key] => _edit_lock
[meta_value] => 1251846014
)
[2] => stdClass Object
(
[meta_id] => 464
[post_id] => 2038
[meta_key] => _edit_last
[meta_value] => 1
)
[3] => stdClass Object
(
[meta_id] => 467
[post_id] => 2038
[meta_key] => event_date
[meta_value] => Sept 15
)
)
我正试图把它变成一个看起来像这样的数组:
[2555] (
[event_date] => Sept 24th - 29th
[articleimg] => /image1.jpg
)
etc...
我写了一些讨厌的foreach和for循环,我的头在游泳。我错过了一个简单的方法吗?
答案 0 :(得分:2)
尝试这样的事情:
foreach ($array as $key => $objs) {
$tmp = array();
foreach ($objs as $obj) {
if ($obj->meta_key[0] !== '_') {
$tmp[$obj->meta_key] = $obj->meta_value;
}
}
$array[$key] = $tmp;
}
这会将对象数组展平为键/值对数组,其中对象的 meta_key 值为键,对象的 meta_value 值为值,如果 meta_key 值不以_
开头。