P
P
PoodingRex2017-03-05 20:08:51
PHP
PoodingRex, 2017-03-05 20:08:51

Why return inside foreach doesn't return all values?

Good day. I'm trying to display the results of foreach, but return only returns the first value from the entire loop.
those. should get:

  • Item 1 x 10; product id 12
  • Item 2 x 123 ;item id 13
  • Item 3 x 3 ;item id 16

But gives only Goods 1 x 10; product id 12.
The code is as follows:
foreach ($products as $product) {
    $name = $product['name'];
    $count = $product['count'];
    $id = $product['p_id'];
    $output = $name . "x" . $count .";". "id товара" . $id ; 
    return $output; 
    
}

At the same time, if you replace return with echo, everything works as it should. But modx is not friendly with echo, you need to use return =(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2017-03-05
@GavriKos

Because the execution of the loop will end with the first return.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question