I am trying to access data from an array nested within a multidimensional array in PHP. I can get the first array level and the second, but not the third.
Here is the output of a single result in my call to the api: http://54.86.204.230/process/pe_trxngetONE.php
My code is pulling all results and trying to loop through to get FullAddress under Property->Address. I thought I could do something simple like $array['Property']['Address']['FullAdress'], but that returns nothing.
Here is my code:
$cSessionTrxn = curl_init(); curl_setopt($cSessionTrxn,CURLOPT_URL,$pe_getTrxn); curl_setopt($cSessionTrxn,CURLOPT_RETURNTRANSFER,true); curl_setopt($cSessionTrxn,CURLOPT_HEADER, false); $resultTrxn=curl_exec($cSessionTrxn); curl_close($cSessionTrxn);
$itemlist = json_decode($resultTrxn,true); foreach ($resultarrayTrxn as $rowTrxn){ $resultIDTrxn=$rowTrxn['ID']; $mlsTrxn=$rowTrxn['MLS']; $transactionTypeTrxn=$rowTrxn['Type']; $participantsTrxn=$rowTrxn['Participants']; foreach($rowTrxn['Property']
as $property) { $fulladdresstrxn = $property['Address']['FullAddress']; } $displayresultTrxn='
<tr class="' . (++$count%2 ? " odd " : "even ") . '">'; $displayresultTrxn.='
<td><a href="'.$baseurl.'/process/refreshSession.php?id='.$resultIDTrxn.'" class="selectclick" id="'.$resultIDTrxn.'">'.$resultIDTrxn.$fulladdresstrxn.'</a>
</td>'; $displayresultTrxn.='
<td>'.$mlsTrxn.'</td>'; $displayresultTrxn.='
<td>Submitted</td>'; $displayresultTrxn.='
<td>'.$transactionTypeTrxn.'</td>'; $displayresultTrxn.='
<td>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-default" style="width: 0%">0%</div>
</div>
</td>'; //$displayresultTrxn.='
<td>tot:'.$formcounter.',dn: '.$formitems.'</td>';/*test percentdone*/ $displayresultTrxn.='
<td>0</td>'; $displayresultTrxn.='
</tr>'; echo $displayresultTrxn; }
And I tried this to just display everything, but it stops echoing after the 2nd level:
foreach ($resultarrayTrxn as $key => $value){
if(is_array($value)){
foreach($value as $subkey => $subvalue){
if(is_array($subvalue)){
foreach($subvalue as $subsubkey => $subsubvalue){
if(isset($subsubvalue)){
if(is_array($subsubvalue)){
foreach($subsubvalue as $subsubsubkey => $subsubsubvalue){
if(($subsubsubkey)){
foreach($subsubsubvalue as $subsubsubsubkey => $subsubsubsubvalue){
if(is_array($subsubsubsubvalue)){
foreach($subsubsubsubvalue as $subsubsubsubsubkey => $subsubsubsubsubvalue)
{echo "6- ".$subsubsubsubsubkey.": ".$subsubsubsubsubvalue."<br />";}
} else {
echo "5- ".$subsubsubsubkey.": ".$subsubsubsubvalue."<br />";}}
} else
{
echo "4- ".$subsubsubkey.": ".$subsubsubvalue."<br />";
echo "4- ".$subsubsubkey['FullAddress'].": ".$subsubsubvalue."<br />";
}
}
}
} else {
echo "3- ".$subsubkey.": ".$subsubvalue."<br />";}}
} else {
echo "2- ".$subkey.": ".$subvalue."<br />";}}
} else {
echo "1- ".$key.": ".$value."<br />";
}
echo "<br>..............................................................<br>";
}
Thank you for any advice or direction!! I appreciate it so much. Stuck here. :(
</div>
Oh you can have as much dimension as you want!
For example the following code displays 1:
$array[0][0][0][0] = 1;
echo $array[0][0][0][0];
To do what you want, it would be much better to use a recursive fonction:
function displayArray($array, $id) {
echo $id.'-';
foreach ($array as $key => $value) {
if(is_array($value)) {
displayArray($value, $id+1);
} else {
echo $key .': '.$value;
}
echo '<br />';
}
}
And then :
displayArray($resultarrayTrxn,1);
EDIT :
For just testing, it is much easier to use :
echo '<pre>';
print_r($my_array);
echo '</pre>';
EDIT :
For your code, can you show us how you define $resultarrayTrxn ? Can you "print_r" this array?
EDIT :
I have corrected my code.
Hope it helps