-2

how to get value of id, friendlyname, url and status for this multidimensional array, how to loop through array for specific value. Also how to get type and datetime for individual monitor based on the ID

Array
(
    [stat] => ok
    [offset] => 0
    [limit] => 50
    [total] => 4
    [monitors] => Array
        (
            [monitor] => Array
                (
                    [0] => Array
                        (
                            [id] => 778539790
                            [friendlyname] => Centaur
                            [url] => http://centaurdatacorp.in
                            [type] => 1
                            [subtype] => 
                            [keywordtype] => 
                            [keywordvalue] => 
                            [httpusername] => 
                            [httppassword] => 
                            [port] => 
                            [interval] => 300
                            [status] => 2
                            [alltimeuptimeratio] => 100
                            [log] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => 2
                                            [datetime] => 01/10/2017 10:16:51
                                        )

                                    [1] => Array
                                        (
                                            [type] => 98
                                            [datetime] => 01/10/2017 10:16:39
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 778539794
                            [friendlyname] => Centaur dotcom
                            [url] => http://centaurdatacorp.com
                            [type] => 1
                            [subtype] => 
                            [keywordtype] => 
                            [keywordvalue] => 
                            [httpusername] => 
                            [httppassword] => 
                            [port] => 
                            [interval] => 300
                            [status] => 2
                            [alltimeuptimeratio] => 100
                            [log] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => 2
                                            [datetime] => 01/10/2017 10:17:22
                                        )

                                    [1] => Array
                                        (
                                            [type] => 98
                                            [datetime] => 01/10/2017 10:17:10
                                        )

                                )

                        )


                )

        )

)

1 Answers1

0

I'm not sure why you're using that array structure. If you made it yourself, I suggest you remove the "monitor" array, and move the content of it into "monitors"

Anyways, Lets say your array is named $monitors

foreach($monitors['monitors'] as $k => $v) { 
    foreach($v['monitor'] as $monitor) { 

         echo "monitor id: " . $monitor['id'] . "<br>\n";
         echo "friendly name: " . $monitor['friendlyname'] . "<br>\n";
    }
}

And get date time for monitor id:

function getLogById($monitors, $id) {
    foreach($monitors['monitors'] as $k => $v) { 
        foreach($v['monitor'] as $monitor) { 
            if($monitor['id'] == $id) {
                //Depending on what you exactly want returned, do something here
                return $monitor['log'];
            }
        }
    }

    return null;
}

//and you can use this like so:
getLogById($monitors, 778539794);
Bert Bijn
  • 337
  • 1
  • 9
  • Array is returned from api of uptimerobot.com Tried using this, but getting error as Undefined index: monitor in C:\xampp\htdocs\uprobot\test.php on line 16 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\uprobot\test.php on line 16 – rakesh gowda Jan 18 '17 at 11:42
  • Working without using key value pair - foreach($monitors['monitors'] as $monitor) { foreach($monitor as $getdata){ echo "Monitor id: " . $getdata['id'] . "
    \n";
    – rakesh gowda Jan 18 '17 at 12:26