简体   繁体   中英

Php array returning just one of an arrays values when looping through

I have a php array and I am trying to var dump the values so I can see what is being returned but when I use a foreach loop, only one of the values is being dumped even though there are 2 values in the array. Can someone tell me what is incorrect in the code?

PHP

$items = ($items['things']);
              
 foreach ($items as $value) {
      var_dump($value);  // Returns just the first thing in my items array i.e. "textbook"
 }

If I var_dump($items) I get an array like this

array:2 [
  0 => "textbook"
  1 => "pencil"
] 

Its working with me:

    $items = [
        'things' => ['a', 'b', 'c']
    ];
    $items = ($items['things']);
    foreach ($items as $item) {
        var_dump($item);
    }

result:

string(1) "a"
string(1) "b"
string(1) "c"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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