ballhogjoni Posted June 28, 2008 Share Posted June 28, 2008 How would I extract ID and transactionID from the last array in this example? The multi array comes from a $result variable from a thrid party xml file or database. The code is: $result = $client->send($msg,'http://xxxxxxxxxxxxxxxxxxxxxxxxxx); Array ( [getthisinfo] => Array ( [Cards] => Array ( [Card] => Array ( [0] => Array ( [iD] => 12 [TransactionID] => 100228873 ) ) ) ) ) This is the code I have and it only gets the info from the first array. foreach($result as $key=>$val){ echo '<tr><td>'.$key.'</td><td>'.$val.'</td></tr>'; } Quote Link to comment https://forums.phpfreaks.com/topic/112281-solved-extract-info-from-a-multi-array/ Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 $result['getthisinfo']['cards']['card'][0]['ID'] $result['getthisinfo']['cards']['card'][0]['TransactionID'] Quote Link to comment https://forums.phpfreaks.com/topic/112281-solved-extract-info-from-a-multi-array/#findComment-576446 Share on other sites More sharing options...
ballhogjoni Posted June 28, 2008 Author Share Posted June 28, 2008 Thx, let me take it one step further and ask; How would I plug that into a foreach loop and print all the data? You said "$result['getthisinfo']['cards']['card'][0]['ID'] $result['getthisinfo']['cards']['card'][0]['TransactionID']" which makes sense for the \[0] index, but what if I had 10 or 20 indexes? Quote Link to comment https://forums.phpfreaks.com/topic/112281-solved-extract-info-from-a-multi-array/#findComment-576456 Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 // example 1: foreach($result['getthisinfo']['cards']['card'] as $list) { echo "ID: {$list['ID']} TransactionID: {$list['TransactionID']} <br />"; } // example 2: foreach($result['getthisinfo']['cards']['card'] as $list) { foreach ($list as $key => $val) { echo "$key : $val "; } echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/112281-solved-extract-info-from-a-multi-array/#findComment-576462 Share on other sites More sharing options...
ballhogjoni Posted June 28, 2008 Author Share Posted June 28, 2008 Your the man!! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/112281-solved-extract-info-from-a-multi-array/#findComment-576467 Share on other sites More sharing options...
ballhogjoni Posted June 28, 2008 Author Share Posted June 28, 2008 when I try your example one I get an error: Warning: Invalid argument supplied for foreach() in /home/xxxxxxxx/public_html/xxxxxxxxx/api/xxxxxxxxxxx.php on line 63 What would this be? Line 63 is foreach($result['getthisinfo']['cards']['card'] as $list) { Quote Link to comment https://forums.phpfreaks.com/topic/112281-solved-extract-info-from-a-multi-array/#findComment-576477 Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 that means that $result['getthisinfo']['cards']['card'] doesn't exist. So..maybe you spelled one of the elements wrong? Whatever code that produced $result isn't producing that multi-dim array as you expect? Quote Link to comment https://forums.phpfreaks.com/topic/112281-solved-extract-info-from-a-multi-array/#findComment-576481 Share on other sites More sharing options...
ballhogjoni Posted June 28, 2008 Author Share Posted June 28, 2008 Aw, cAsE sensitive. That fixed it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/112281-solved-extract-info-from-a-multi-array/#findComment-576487 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.