Jump to content

Looping Arrays That Match Values In Multi-dimensional Array


Recommended Posts

Hi - I wonder if anyone can shed some light in my tunnel.I have a wsdl feed which I call with soap and all the values are in xml arrays with other multi-dimensional arrays nested in them.eg.

[products] => Array		(			[0] => stdClass Object				(					[product_id] => 1000					[product_title] => some product					[product_description] => some prod info					[features] => Array						  (							[0] => stdClass Object								(									[feature_id] => 422									[feature_detail] => 								)							[1] => stdClass Object								(									[feature_id] => 476									[feature_detail] => 								)							)						)

What I am trying to do is loop through the main [product] array where all the [feature_id] array values are equal to value 476.This 'kind of' works when I do this

foreach($response->products as $product) {foreach($product->features as $feature) {	if ($feature->feature_id =='476'){echo "<pre>"; print_r($response); echo "</pre>";}}}

But with the error message Warning: Invalid argument supplied for foreach() - (I think) because not every array had this value so I end up with blanks. Yet if I do the following it works perfectly:

foreach($response->products as $product) {	if ($product->product_name =='specific product'){foreach($product->features as $feature) {	if ($feature->feature_id =='476'){

because I know that those specific products have feature_id 476 in it.from what I have read on the net from php.net, w3schools and hundreds of forums etc - I think I need to use some sort of for loop on the [features], but have not had any success implementing it and I have just ended up confusing myself. All my experience with php has been with mysql and all this soapy business is new to me and I am frustrated I can't just use a where clause!!Can anyone give me some pointers on where to go from here?Thanks in advance.

Link to comment
Share on other sites

Many thanks for the quick response JSGWorks like a charm - I wish I had come here sooner!!Many many thanksp.s.I know your really busy helping out all us newbs, but do you know why it doesn't work without that extra if statement?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...