和我的问题:
将从array_chunk
生成的数字多维数组转换为关联数组或stdClass。我该怎么做?
我从
生成一个数组 foreach($xpath->query("./td[position()=2 ]", $row) as $cell)
{
$a[] = trim($cell->nodeValue);
}
此数组
看起来像:
Array
(
[0] => AAK AB
[1] => 642,00
[2] => 644,00
[3] => 635,00
[4] => 17 108 677
[5] => ABB Ltd
[6] => 164,90
[7] => 165,00
[8] => 163,20
[9] => 146 251 251
...
但是array_chunk
,我生成:
Array
(
[0] => Array
(
[0] => AAK AB
[1] => 642,00
[2] => 644,00
[3] => 635,00
[4] => 17 243 497
)
[1] => Array
(
[0] => ABB Ltd
[1] => 164,80
[2] => 165,00
[3] => 163,20
[4] => 146 335 464
)
但是对于更清晰,更易理解的代码,我希望它看起来像:
Array
(
[1] => stdClass Object
(
[stock] => AAK AB
[prev] => 634,50
[high] => 638,50
[low] => 622,50
[rev] => 32 094 048
)
[2] => stdClass Object
(
[stock] => ABB Ltd
[prev] => 162,80
[high] => 163,30
[low] => 161,90
[rev] => 167 481 268
)
)
它不必是stdClass,但是如果可以使用以下对象来回显数组中的数据:
$a->aktie
或$a['aktie']
答案 0 :(得分:2)
您可以在array_combine
-
array_chunk
$keys = array('stock', 'prev', 'high', 'low', 'rev');
foreach($your_array as &$array) {
$array = (object) array_combine($keys, $array);
}