2015-11-04 138 views
1

和我的問題:轉換多維數組多維關聯數組

轉換從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 
     ) 

但對於更清潔,更多nderstandable代碼,我想它看起來像:

    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']

回答

2

你可以嘗試後在array_combine循環array_chunk -

$keys = array('stock', 'prev', 'high', 'low', 'rev'); 
foreach($your_array as &$array) { 
    $array = (object) array_combine($keys, $array); 
}