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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.