I wrote a code for getting data via SOAP using NuSOAP, then parse it and convert it to a php data structure. I am using a foreach loop like : foreach ($data['item']['optstats'] as $x) { echo $x['ProgramID']; } And data structure is like : Array ( [item] => Array ( [optstats] => Array ( [0] => Array ( [Date] => 05/23/2007 [Campaign] => aa [ProgramID] => 12 [Info] => 12 [Amount] => LEAD N/A [TransactionID] => LEAD N/A [Commission] => $ 10 ) [1] => Array ( [Date] => 05/22/2007 [Campaign] => qwqw [ProgramID] => 5121 [Info] => 3421 [Amount] => LEAD N/A [TransactionID] => LEAD N/A [Commission] => $ 1.00 ) It works fine as long as there is more than one row in that array. If it has only 1, the output is bogus data. How do I fix this ? Thanks,
I guess you need one more foreach - ProgramID key is for the third array, not the second as shown in your eg.
alemcherry is right from what I see your code should look like this: foreach ($data as $x) { $ar = $x['item']['optstats']; foreach ($ar as $y) { echo $y['ProgramID']; } } PHP:
Thanks. If I do that I get an error : Warning: Invalid argument supplied for foreach() . If two foreach loops are required, why would it work when there are more than one entries ?
Before this, I was using : foreach ($data['item']['optstats'] as $x) { echo $x['ProgramID']; } Edited the first post.
$i=0; before foreach, and $i++; at the end inside the foreach loop. Then echo '$i: '.$x['ProgramID'].'<br />'; and look at the output or so. From what I'm getting you just aren't getting anything showing up, which may mean that what you're trying to print is blank, but exists. This way at least you get a null placeholder and you can tell if it's showing an empty result or no results at all.