Laravel foreach only return last key in array -


my array:

array:2 [▼   0 => array:2 [▼     "product_id" => "2"     "product_thumbnail" => "p38.jpg"   ]   1 => array:2 [▼     "product_id" => "4"     "product_thumbnail" => "p41.jpg"   ] ] 

my function:

 public function add(request $request, $id) {         $product_compare = product::where('product_id', $id)->first();         $product = [];         $product['product_id'] = $id;         $product['product_thumbnail'] = $product_compare->product_thumbnail;          if (session::has('product_compare')) {             foreach (session::get('product_compare') $item) {                 $check= $item['product_id'];             }             if (!($check == $id)) {                 $request->session()->push('product_compare', $product);             }         }          else {              $request->session()->push('product_compare', $product);         }         return back()->withinput();      } 

foreach loop in function doesn't print out of values in includes array. return last key in array.

it looks you're overwriting $check variable before using it.

you have put if statement inside foreach loop so:

foreach (session::get('product_compare') $item) {    $check= $item['product_id'];    if (!($check == $id)) {        $request->session()->push('product_compare', $product);    } } 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -