0

I have this array:

$items_pool = Array ( 
[0] => Array ( [id] => 1 [quantity] => 1 ) 
[1] => Array ( [id] => 2 [quantity] => 1 ) 
[2] => Array ( [id] => 72 [quantity] => 6 ) 
[3] => Array ( [id] => 4 [quantity] => 1 )
[4] => Array ( [id] => 5 [quantity] => 1 ) 
[5] => Array ( [id] => 7 [quantity] => 1 ) 
[6] => Array ( [id] => 8 [quantity] => 1 ) 
[7] => Array ( [id] => 9 [quantity] => 1 ) 
[8] => Array ( [id] => 19 [quantity] => 1 ) 
[9] => Array ( [id] => 20 [quantity] => 1 ) 
[10] => Array ( [id] => 22 [quantity] => 1 ) 
[11] => Array ( [id] => 29 [quantity] => 0 ) 
) 

I'm trying to loop through this array and perform a conditional based on $items_pool[][id]'s value. I want to then report back TRUE or NULL/FALSE, so I'm just testing the presence of to be specific.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dmanexe
  • 1,034
  • 4
  • 16
  • 40
  • 1
    A bit unclear what you are trying to achive... please give an example... – Vidar Vestnes Apr 09 '10 at 22:30
  • *(reference)* http://de2.php.net/manual/en/language.types.array.php, http://de2.php.net/manual/en/language.control-structures.php – Gordon Apr 09 '10 at 22:34
  • 1
    http://stackoverflow.com/questions/2611040/how-do-i-make-these-fields-autopopulate-from-the-database/2611449#2611449 - grrr; angry look. – bigstylee Apr 09 '10 at 23:02

2 Answers2

2

Something like this:

$items_pool = array(...);
$result = false;

foreach ($items_pool as $item) {
    if ('something' == $item['id']) {
        $result = true;
        break;
    }
}
Crozin
  • 43,890
  • 13
  • 88
  • 135
  • 1
    It might be worth adding in a `break;` after `$result = true;` to stop the looping carrying on needlessly – ͢bts Jan 05 '12 at 11:53
  • @TheArtfulBenny: `break` is actually a requirement in such cases. Thanks for notice, fixed. – Crozin Jan 05 '12 at 13:26
1

Loop through an check if anything is empty..

foreach($items_pool as $arr){
   echo $arr['id'].'==>'.$arr['quantity'];
   if($arr['quantity'] == 0){
      echo 'id:'.$arr['id'].' is empty!';
      return false;
   }
}
Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100