3

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($row=0;$row<count($list);$row++){
    echo $list[$row]['requests_users']['request_id'];
}

I could get request_id values. However, I'm having a trouble getting a 'ct' values.

Can you guys help me how to print 'ct' values?

0

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You for helping me. But do you know the way by using nested for loop?
0

You can get "ct" value :

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

Comments

0

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>";
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.