简体   繁体   English

我如何在这个多维数组中获取值

[英]How do I get values inside this multi-dimensional array

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. 我想使用foreach循环访问ID和名称变量。

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. 出于某些奇怪的原因,我只能获取第一个$ listId和$ name的值,而不能得到第二个$ listId或$ name的值。

What am I doing wrong here? 我在这里做错了什么?

You're assuming that you still need to provide the key for each child element. 您假设您仍需要为每个子元素提供密钥。 This is not the case. 不是这种情况。

try 尝试

foreach($lists as $list){

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

    $listId $listName <br />";

}

the foreach() will iterate over them in turn. foreach()将依次对其进行迭代。

if you do need the index number, do this instead. 如果确实需要索引号,请执行此操作。

foreach($lists as $x => $list){

where $x is the index. 其中$x是索引。

The array you posted is wrong because it's missing closing ) , so correct that (I think that is TYPO mistake) 您发布的数组是错误的,因为它缺少close ) ,因此请更正(我认为这是TYPO错误)

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 输出: -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 输出: -https : //eval.in/846465

Your foreach iterates the first, not the second level of your multi dimensional array. 您的foreach迭代多维数组的第一层而不是第二层。

Since the first level only holds the lists array as one and only element the loop only executes once. 由于第一级仅将lists数组作为一个且仅作为元素,因此循环仅执行一次。

Pass the lists key to the foreach instead like so: 像这样将lists键传递给foreach

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

Also note how in here I reference the list elements by name to make it easier to read. 还要注意,在这里我如何通过名称引用列表元素以使其更易于阅读。

I think those numerical indexed will just confuse you so try this instead: 我认为这些数字索引只会使您感到困惑,因此请尝试以下操作:

$my_array = array(array("id" => "23ybdwhdwbed", "name" => "TEST"), array("id" => "e223edsewed", "name" => "TEST 2"));

To access the values: use: 要访问这些值:使用:

foreach($my_array as $my_data){
    echo "ID:" . $my_data["id"];
    echo "<br>";
    echo "NAME:" .$my_data["name"];
    echo "<br><br>";
}

you just need to do: 您只需要执行以下操作:

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

Try this, I fixed your array structure to work, this is also dynamic so it does not matter how many array you have 0 -> above 试试这个,我固定了你的数组结构,它是动态的,所以你有0->以上的数组都没关系

       $array = array(
                      'lists' => array(
                                      '0' => array(
                                                    'id' => '23ybdwhdwbed',
                                                    'name' => 'TEST 1'
                                                    ),
                                      '1' => array(
                                                    'id' => 'e223edsewed',
                                                    'name' => 'TEST 2'
                                                   )
                                      )
    );

      foreach ($array as $key => $value) {

        for($ctr = 0; $ctr < count($value); $ctr++){
             echo 'ID: ' . $value[$ctr]['id'] . '<br>';
             echo 'Name: : ' . $value[$ctr]['name'] . '<br><br>';
        }
      }

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

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