简体   繁体   English

访问多个php数组值

[英]Accessing multi php array values

Hi I'm trying to access elements in array. 嗨,我正在尝试访问数组中的元素。

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

By using for loop (under) 通过使用for循环(下)

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

I could get request_id values. 我可以得到request_id值。 However, I'm having a trouble getting a 'ct' values. 但是,我在获取'ct'值时遇到了麻烦。

Can you guys help me how to print 'ct' values? 你们能帮助我如何打印“ct”值吗?

how about like this.. 这个怎么样..

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

try this. 尝试这个。 should work 应该管用

You can get "ct" value : 你可以获得“ct”值:

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

You can try like below code 您可以尝试以下代码

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

To access an elements of above array you can use following recursive function: 要访问上面数组的元素,您可以使用以下递归函数:

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

you can call above function in following way: 你可以通过以下方式调用上面的函数:

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


    $this->_convertToString($str);

You can modify output or recursive function to meet your requirements. 您可以修改输出或递归函数以满足您的要求。 Can you add original array in your question, I mean not var_dump() so that I can use that directly and modify code in my answer, if needed. 你可以在你的问题中添加原始数组,我的意思是不是var_dump()所以我可以直接使用它并在需要时修改我的答案中的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM