Jump to content

[SOLVED] extract info from a multi array


ballhogjoni

Recommended Posts

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>';
}

Link to comment
https://forums.phpfreaks.com/topic/112281-solved-extract-info-from-a-multi-array/
Share on other sites

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?

// 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 />";
}

 

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) {

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.