试图访问嵌入式数组的元素

时间:2012-12-11 15:09:04

标签: php arrays data-structures

这是一个包含许多嵌入式阵列的大型数据结构。每个数组都有n个元素,在本例中为37.我试图访问每个数组的第1和第3个位置的'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)
  .
  .
  .

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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

   $i++;
 }