0

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"
] 
3
  • I ran your code. It gives my both values as expected. string(8) "textbook" string(6) "pencil" Commented Nov 27, 2020 at 16:08
  • @JasonK the var_dump in the foreach loop gave you both items? Hmm Commented Nov 27, 2020 at 16:10
  • 1
    Each run of the loop gave 1 value. Commented Nov 27, 2020 at 16:15

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

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.