Reputation: 4976
Here's my array:
[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
)
)
I'm trying to get this into an array that looks like:
[2555] (
[event_date] => Sept 24th - 29th
[articleimg] => /image1.jpg
)
etc...
I've written some nasty foreach and for loops and my head is swimming. Am I missing a simple way to do this?
Upvotes: 0
Views: 224
Reputation: 655489
Try something like this:
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;
}
That will flatten the array of objects to an array of key/value pairs with the object’s meta_key value as the key and the object’s meta_value value as the value if the meta_key value does not start with _
.
Upvotes: 2