How do I get values inside this multi-dimensional array

kevinkt

I have a multi-dimensional array as follows:

Array
(
 [lists] => Array
    (
        [0] => Array
            (
                [id] => 23ybdwhdwbed
                [name] => TEST
            (
        [1] => Array
            (
                [id] => e223edsewed
                [name] => TEST 2
            (
    )
)

I want to access the ID & name variables using a foreach loop.

I'm using this:

$x = 0;
foreach($lists as $list){

    $listId = $list[$x]['id'];
    $listName = $list[$x]['name'];

    echo"$x | $listId $listName <br />";

$x++;
}

For some strange reason, I can only get the value of the first $listId & $name, not the second $listId or $name.

What am I doing wrong here?

Serving Quarantine period

The array you posted is wrong because it's missing closing ), so correct that (I think that is TYPO mistake)

After that you need to do it like below:-

foreach($lists['lists'] as $key=> $list){
   $listId = $list['id'];
   $listName = $list['name'];
   echo "$key | $listId $listName <br />";
}

Output:-https://eval.in/846464

Or an one-liner code:-

foreach($lists['lists'] as $key=> $list){
  echo "$key | ".$list['id']." ".$list['name']." <br />";
}

Output:-https://eval.in/846465

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to unmarshal multi dimensional array inside struct

How do you get the width and height of a multi-dimensional array?

how to get the index of the largest n values in a multi-dimensional numpy array

How can I find all the cells that have the same values in a multi-dimensional array in octave / matlab

How do I get the dimensions of multi-dimensional arrays?

How do I convert an array of arrays into a multi-dimensional array in Python?

Change multi dimensional array values

how do i correctly handle a multi dimensional numpy array

How to loop multi dimensional array to get values

Python: How to get values of a multi-dimensional array at certain indexed positions?

How do you copy a multi-dimensional array (i.e. an array of arrays) in awk?

How to get and manipulate values from multi dimensional array and display them in row wise

How do I return such an multi-dimensional array?

Why do I get "Undefined variable" when trying to resolve a multi-dimensional array?

How can I get the array of a single type from a multi dimensional array (without a loop)

Get values from multi dimensional array

CakePHP 2.4.4 How can I sort a multi-dimensional array with Hash::sort by string keys and values?

How do I sort a multi-dimensional array by time values in PHP?

How do I find maximum value in a multi dimensional array

AJAX: How to get array on multi dimensional array

How to get only duplicate values in multi-dimensional array

How do I trim '\n' in multi dimensional array in php

How to get specific values from a multi-dimensional array?

How do I return such an multi-dimensional array

How do I get an neighbor element in a n-dimensional array?

How to get the values of a multi-dimensional array by key in Laravel?

Formatting Multi dimensional array with values

How do I reshape a NumPy multi dimensional array to another array with the same dimensions, but different shape?

How would I populate a multi-dimensional array using values from multiple lists in C#