我有一个这样的数组结构
Array
(
[0] => stdClass Object
(
[foo] => 9992
[bar] => 1508
)
[1] => stdClass Object
(
[foo] => 10115
[bar] => 1598
)
[2] => stdClass Object
(
[foo] => 10263
[bar] => 1690
)
[3] => stdClass Object
(
[foo] => 10363
[bar] => 1794
)
[4] => stdClass Object
(
[foo] => 10486
[bar] => 1904
)
)
现在我想将此结构转换为:
Array
(
[0] => stdClass Object
(
[name] => 'foo'
[data] => Array (
9992, 10115, 10263, 10363, 10486
)
)
[1] => stdClass Object
(
[name] => 'bar'
[data] => Array(
1508, 1598, 1690, 1794, 1904
)
)
)
我已经尝试过这段代码,但它没有给我预期的结构
$return = array();
$i = 0;
foreach($object as &$row) {
foreach( $row as $key => $val ) {
$return[$i]['name'] = $key;
// This is my custom function that does the same
// thing for objects as array_search does for arrays
$search = object_search($key, $row);
if( $search ) {
$return[$search]['data'][] = $val;
} else {
$return[$i]['data'][] = $val;
}
}
$i++;
}
return $return;