Jump to content

Reading this array in PHP...


theITvideos

Recommended Posts

Hi there

 

I am working on PHP form which has an array named: $productOptions.

 

When I do:

 

print_r($productOptions);

 

it returns:

Array ( [124] => Array ( [optionId] => 124 [optionName] => acer aspire [optionListId] => 28 [dateCreated] => 2010-11-04 12:48:53 [enabled] => 1 [optionQtyId] => 84 [productId] => 40 [quantity] =>7 )
[114] => Array ( [optionId] => 114 [optionName] => acer fast [optionListId] => 28 [dateCreated] => 2010-11-01 02:04:53 [enabled] => 1 [optionQtyId] => 83 [productId] => 40 [quantity] => 0 ) 

 

Now from this Array, I would like to check if the quantity if greater than Zero. so I tried:

 

if (productOptions[quantity] > 0){

echo "blah blah..."

}

 

But for getting the quantity, the part:

 

productOptions[quantity]

 

returns nothing. Am I doing this correctly?

 

Please reply.

 

Thank you :)

Link to comment
Share on other sites

Close but no

 

this is your array

Array (

[124] => Array (

[optionId] => 124

[optionName] => acer aspire

[optionListId] => 28

[dateCreated] => 2010-11-04 12:48:53

[enabled] => 1

[optionQtyId] => 84

[productId] => 40

[quantity] =>7 )

[114] => Array (

[optionId] => 114

[optionName] => acer fast

[optionListId] => 28

[dateCreated] => 2010-11-01 02:04:53

[enabled] => 1

[optionQtyId] => 83

[productId] => 40

[quantity] => 0

)

 

So you see quantity is in an array in 124 and 114

So your need to do this

if ($productOptions['124']['quantity'] > 0){
echo "blah blah..."
}

 

so

$X = $productOptions['124']

would return an array

Array (

[optionId] => 124

[optionName] => acer aspire

[optionListId] => 28

[dateCreated] => 2010-11-04 12:48:53

[enabled] => 1

[optionQtyId] => 84

[productId] => 40

[quantity] =>7 )

then

$X['quantity']

would return 7

 

So in a loop

foreach($productOptions as $X){
   if ($X['quantity'] > 0){
      echo "ID ".$X['optionId']." has ".$X['quantity'];
   }
}

 

Hope this helps

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.