Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a array variable in a foreach loop [closed]

I am having an issue trying to work out how to use a function variable in a foreach loop so that I can do the following but its not working.

$var =

array(7) { [0]=> array(3) { ["listingId"]=> int(532712629) } [1]=> array(3) { ["listingId"]=> int(532712202) }

Works but not right:

foreach($var as $varr)
{
  var_dump($varr['id']);
{

Goal - Having the array variable as the foreach value

    foreach($var['id'] as $item)
    {
       if($item === $foo)
      {
      }
   }
like image 857
Jess McKenzie Avatar asked Aug 24 '26 01:08

Jess McKenzie


1 Answers

This will loop through the array in your $var array:

foreach ($var as $k=> $v){
   foreach ($v as $k2=> $v2){
      echo $k2." ".$v2;
   }
}

in the for each, the $k will retunr the array key (numeric OR textual) as $v will return the value. You can output an array's content by using print_r($array); in most case.

like image 73
Louis Loudog Trottier Avatar answered Aug 26 '26 15:08

Louis Loudog Trottier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!