2015-12-03 49 views
3

嗨我试图访问数组中的元素。访问多个php数组值

array(
    (int) 0 => array(
     'requests_users' => array(
      'request_id' => '1' 
     ), 
     (int) 0 => array(
      'ct' => '2' 
     ) 
    ), 
    (int) 1 => array(
     'requests_users' => array(
      'request_id' => '2' 
     ), 
     (int) 0 => array(
      'ct' => '1' 
     ) 
    ), 
    (int) 2 => array(
     'requests_users' => array(
      'request_id' => '4' 
     ), 
     (int) 0 => array(
      'ct' => '2' 
     ) 
    ), 
    (int) 3 => array(
     'requests_users' => array(
      'request_id' => '5' 
     ), 
     (int) 0 => array(
      'ct' => '2' 
     ) 
    ) 
) 

通过使用循环(下)

for($row=0;$row<count($list);$row++){ 
    echo $list[$row]['requests_users']['request_id']; 
} 

我能得到REQUEST_ID值。 但是,我在获取'ct'值时遇到了问题。

你们可以帮我打印'ct'的值吗?

回答

1

这个怎么样..

for($row=0;$row<count($list);$row++){ 
    echo $list[$row]['requests_users']['request_id']; 
    echo '<br/>'; 
    echo $list[$row][0]['ct']; 
} 

尝试。 应该工作

+0

谢谢你帮助我。但是,你是否知道使用嵌套for循环的方式? –

0

你可以得到“CT”值:

for($row=0;$row<count($list);$row++){ 
    echo $list[$row][0]['ct']; 
} 
0

你可以尝试像下面的代码

for($row=0;$row<count($list);$row++){ 
    echo $list[$row]['requests_users']['request_id']; 
    echo "<br>"; 
    echo $list[$row][0]['ct']; 
    echo "<br><br>"; 
} 
0

要访问上面数组,你可以使用下面的递归函数的元素:

public function _convertToString($data){ 
    foreach($data as $key => $value){ 
     if(is_array($value)){ 
       $this->_convertToString($value); 
     }else{ 
      echo $key .'->'.$value; 
     } 
    } 
} 

您可以按以下方式调用上面的函数:

$str = array(
     "data" => "check", 
     "test1" => array(
      "data" => "Hello", 
      "test3" => array(
       "data" => "satish" 
      ) 
     ), 
     "test2" => array(
      "data" => "world" 
     ) 
    ); 


    $this->_convertToString($str); 

可以修改输出或递归功能来满足您的要求。你可以在你的问题中添加原始数组,我的意思不是var_dump(),以便我可以直接使用它,并在需要时修改我的答案中的代码。