2012-12-11 58 views
-1

這是一個包含許多嵌入式數組的大型數據結構。每個數組都有n個元素,在本例中爲37.我試圖訪問每個數組的第一個和第三個位置的'content'元素。試圖訪問嵌入式數組的元素

陣列[1] - > percentage->內容

陣列[3] - > grade->內容

$ TDATA是包含所述數據結構中的變量。我試過這樣循環:

$i=0; 
    foreach($tdata as $td){ 

    if($td[$i] == 1 OR $td[$i] == 3){ 
    var_dump($td[$i]) 
    } 

    $i++; 
} 

這是數據結構。

array (size=37) 

0 => 
array (size=2) 
    'leader' => 
    array (size=2) 
     'class' => string '...' (length=18) 
     'rowspan' => int 37 
    'itemname' => 
    array (size=2) 
     'colspan' => int 6 
     'content' => string '...' (length=159) 
1 => 
    array (size=2) 
    'grade' => 
     array (size=2) 
     'class' => string '...' (length=25) 
     'content' => string '43.37 %' (length=7) 
    'percentage' => 
     array (size=2) 
     'class' => string ' oddd1 baggb itemcenter ' (length=25) 
     'content' => string '43.37 %' (length=7) 
2 => 
    array (size=1) 
    'leader' => 
     array (size=2) 
     'class' => string ' oddd2 b1t b2b b1l' (length=18) 
     'rowspan' => int 32 
3 => 
    array (size=1) 
    'grade' => 
     array (size=2) 
     'class' => string ' oddd2 baggb itemcenter ' (length=25) 
     'content' => string '6.30 %' (length=6) 
    . 
    . 
    . 

array (size=37) 
    0 => 
    array (size=2) 
     'leader' => 
     array (size=2) 
      'class' => string '...' (length=18) 
      'rowspan' => int 37 
     'itemname' => 
     array (size=2) 
      'colspan' => int 6 
      'content' => string '...' (length=159) 
    1 => 
    array (size=2) 
     'grade' => 
     array (size=2) 
      'class' => string '...' (length=25) 
      'content' => string '26.49 %' (length=7) 
     'percentage' => 
     array (size=2) 
      'class' => string ' oddd1 baggb itemcenter ' (length=25) 
      'content' => string '26.49 %' (length=7) 
    2 => 
    array (size=1) 
     'leader' => 
     array (size=2) 
      'class' => string ' oddd2 b1t b2b b1l' (length=18) 
      'rowspan' => int 32 
    3 => 
    array (size=1) 
     'grade' => 
    array (size=2) 
    'class' => string ' oddd2 baggb itemcenter ' (length=25) 
    'content' => string '11.12 %' (length=6) 
    . 
    . 
    . 
+0

所以你的問題是如何確定何時關鍵是1或3或..? – RTB

+0

是的 - 謝謝。 – jamesTheProgrammer

回答

1
foreach($tdata as $i => $td) 
{ 
    if($i == 1 || $i == 3) 
    { 
     var_dump($td[$i]); 
    } 
} 
0
$i=0; 
    foreach($tdata as $td){ 

    if($i == 0 || $i == 2){ 
    var_dump($td[$i]) 
    } 

    $i++; 
} 
1
foreach($tdata as $key => $data){  
    if($key === 1 || $key === 3){ 
    var_dump($data); 
    } 
}