Quick Array Question


#1

Hi Team!

I’m a little rusty on my PHP, its been a while since I have used it. My problem is understanding arrays and how to loop through them.

In my code I have converted the json call to an array with the 1
$data=json_decode($contents,1);

and this is what I get with the print_r($data);

Would you guys have a quick example of how to look through this? I’m trying to echo just the messages.

Array ( [command] => get [result] => ok [found] => 6 [what] => msg [entries] => Array ( [0] => Array ( [messageid] => 49489630 [time] => 1544385136 [srccall] => KN4JCK-7 [dst] => KN4JCK-7 [message] => Test ) [1] => Array ( [messageid] => 49469985 [time] => 1544239609 [srccall] => EMAIL-2 [dst] => KN4JCK-7 [message] => Invalid Request! ) [2] => Array ( [messageid] => 49469929 [time] => 1544239367 [srccall] => SMSGTE [dst] => KN4JCK-7 [message] => @2076513925 Who is this? ) [3] => Array ( [messageid] => 49469909 [time] => 1544239243 [srccall] => SMSGTE [dst] => KN4JCK-7 [message] => Did you forget the @ prefix? Visit smsgte.wix.com/smsgte for help. ) [4] => Array ( [messageid] => 49466186 [time] => 1544212229 [srccall] => ISS [dst] => KN4JCK-7 [message] => AOS 35m29s NW N^7 NE 8m ) [5] => Array ( [messageid] => 49466117 [time] => 1544211607 [srccall] => EMAIL-2 [dst] => KN4JCK-7 [message] => Email sent to ##########) ) )

I don’t think that I am understanding how to access that elements in the array. I have this loop and have been trying different examples but nothing works.

foreach($data as $item)
{
var_dump($item). “
”;
echo “
”;
}

This loop returns

string(3) “get”
string(2) “ok”
int(6)
string(3) “msg”
array(6) { [0]=> array(5) { [“messageid”]=> string(8) “49489630” [“time”]=> string(10) “1544385136” [“srccall”]=> string(8) “KN4JCK-7” [“dst”]=> string(8) “KN4JCK-7” [“message”]=> string(4) “Test” } [1]=> array(5) { [“messageid”]=> string(8) “49469985” [“time”]=> string(10) “1544239609” [“srccall”]=> string(7) “EMAIL-2” [“dst”]=> string(8) “KN4JCK-7” [“message”]=> string(16) “Invalid Request!” } [2]=> array(5) { [“messageid”]=> string(8) “49469929” [“time”]=> string(10) “1544239367” [“srccall”]=> string(6) “SMSGTE” [“dst”]=> string(8) “KN4JCK-7” [“message”]=> string(24) “@2076513925 Who is this?” } [3]=> array(5) { [“messageid”]=> string(8) “49469909” [“time”]=> string(10) “1544239243” [“srccall”]=> string(6) “SMSGTE” [“dst”]=> string(8) “KN4JCK-7” [“message”]=> string(66) “Did you forget the @ prefix? Visit ###### for help.” } [4]=> array(5) { [“messageid”]=> string(8) “49466186” [“time”]=> string(10) “1544212229” [“srccall”]=> string(3) “ISS” [“dst”]=> string(8) “KN4JCK-7” [“message”]=> string(23) “AOS 35m29s NW N^7 NE 8m” } [5]=> array(5) { [“messageid”]=> string(8) “49466117” [“time”]=> string(10) “1544211607” [“srccall”]=> string(7) “EMAIL-2” [“dst”]=> string(8) “KN4JCK-7” [“message”]=> string(33) “Email sent to #########” } }

Here is a breakdown of th ejdon feed.

https://api.aprs.fi/api/get?name=OH7RDA&what=loc&apikey=APIKEY&format=json

{
“command”:“get”,
“result”:“ok”,
“what”:“loc”,
“found”:1,
“entries”: [
{
“name”:“OH7RDA”,
“type”:“l”,
“time”:“1267445689”,
“lasttime”:“1270580127”,
“lat”:“63.06717”,
“lng”:“27.66050”,
“symbol”:"/#",
“srccall”:“OH7RDA”,
“dstcall”:“APND12”,
“phg”:“44603”,
“comment”:"/R,W,Wn,Tn Siilinjarvi",
“path”:“WIDE2-2,qAR,OH7AA”
}
]
}

I thought this code would work but it is shooting me an error.

foreach ($data[‘entries’] as $aprs_message) {
echo $aprs_message[‘message’];
}

Notice : Use of undefined constant ‘entries’ - assumed ‘‘entries’’ in /storage/ssd5/628/906628/public_html/messages.php on line 69

Notice : Undefined index: ‘entries’ in /storage/ssd5/628/906628/public_html/messages.php on line 69

Warning : Invalid argument supplied for foreach() in /storage/ssd5/628/906628/public_html/messages.php on line 69

Thank you for any help,
Jimmy D


#2

Well, what do you wish to do with the data?

You have an array that contains an array, therefore, you would have to split the embedded array also.
Something like this would work:

foreach ($data as $key=>$value) {
   if ($key!="entries") {
      echo $key . " = " . $value . "<br>";
   } else {
      echo "ENTRIES:<br>";
      foreach ($value as $key2=>$value2) {
         echo $key2 . " = " . $value2 . "<br>";
      }
   }
}

Not tested, but, should work like this…