3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[ {"recipient_name":"John D", "phone_number":"123456"}, {"recipient_name":"Doe J", "phone_number":"654321"}, {"recipient_name":"Jon Do", "phone_number":"112233"} ]'; $myLargerArray = json_decode($json, true); $myObjArray = (object) ['message_recipients' => array()]; $size = count($myLargerArray); $myObjArray->message_recipients = []; for ($j = 0; $j < $size; $j++) { $myRecipientsObj = new stdClass; $myRecipientsObj->recipient_name = $myLargerArray[$j]['recipient_name']; $myRecipientsObj->phone_number = $myLargerArray[$j]['phone_number']; // var_dump($myRecipientsObj); // This outputs the correct data added from [$j] $myObjArray->message_recipients[] = $myRecipientsObj; } var_dump($myObjArray->message_recipients); // The output shows array elements are being overwritten at each loop iteration
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
array(3) { [0]=> object(stdClass)#2 (2) { ["recipient_name"]=> string(6) "John D" ["phone_number"]=> string(6) "123456" } [1]=> object(stdClass)#3 (2) { ["recipient_name"]=> string(5) "Doe J" ["phone_number"]=> string(6) "654321" } [2]=> object(stdClass)#4 (2) { ["recipient_name"]=> string(6) "Jon Do" ["phone_number"]=> string(6) "112233" } }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/g9Nmr on line 8
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/g9Nmr on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/g9Nmr on line 8
Process exited with code 255.

preferences:
301.78 ms | 401 KiB | 459 Q