Convert a PHP object to an array that I can loop through
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi

How can I convert the following output to a PHP array AND loop through printing the keys and values via PHP?

`
array {

[1557860400]=> array { ["timestamp"]=> "2019-05-14 21:00:00", ["time"]=> 1557860400, ["height"]=> 8030, ["width"]=> 8075.5, ["diameter"]=> 7990.5, ["circumference"]=> 7989 }

[1557864000]=> array { ["timestamp"]=> "2019-05-14 22:00:00", ["time"]=> 1557864000, ["height"]=> 7990.5, ["width"]=> 8074, ["diameter"]=> 7920, ["circumference"]=> 7698 }

[1557954000]=> array { ["timestamp"]=> "2019-05-15 23:00:00", ["time"]=> 1557954000, ["height"]=> 8005.5, ["width"]=> 8270.5, ["diameter"]=> 8163.5, ["circumference"]=> 8000 }

}
`

Thanks

Is this output correct? Or your wanted output?
danielnaranjo 4 months ago
I wanted you take the output and create a code to loop through it
remotedevske 4 months ago
awarded to danielnaranjo

Crowdsource coding tasks.

1 Solution

Winning solution

`

$data = array (

array (

"timestamp"=> "2019-05-14 21:00:00",

"time"=> 1557860400,

"height"=> 8030,

"width"=> 8075.5,

"diameter"=> 7990.5,

"circumference"=> 7989

),

array (

"timestamp"=> "2019-05-14 22:00:00",

"time"=> 1557864000,

"height"=> 7990.5,

"width"=> 8074,

"diameter"=> 7920,

"circumference"=> 7698

),

array (
"timestamp"=> "2019-05-15 23:00:00",

"time"=> 1557954000,

"height"=> 8005.5,

"width"=> 8270.5,

"diameter"=> 8163.5,

"circumference"=> 8000

)

);

/* echo json_encode($data); DEBUG */

foreach( json_encode($data) as $output) {

echo $output->circumference;

}

`

I tried it... Warning: Invalid argument supplied for foreach() in ...\htdocs\loop1.php on line 78
remotedevske 4 months ago
I tried it... Warning: Invalid argument supplied for foreach() in ...\htdocs\loop1.php on line 78. How did you convert to the cleaner format?
remotedevske 4 months ago
You're right! `
foreach( $data as $output) {
echo "circumference: ".$output['circumference'].", ";
}
danielnaranjo 4 months ago