3v4l.org

run code in 300+ PHP versions simultaneously
<?php $document = new stdClass(); $document->authors = [ [ 'id' => "18141", "userId" => null, "image" => '', 'firstName' => 'John', 'lastName' => 'Inazu', 'publicEmail' => null, 'publicWebsite' => null, 'publicFacebook' => null, 'publicTwitterUsername' => null, 'publicEndnote' => null, 'byline' => 'John Inazu', 'extraData' => null ] ]; doTheThings($document); function doTheThings($document) { $authors = array(); $authorEmails = array(); $authorExtra = array(); $bylineExtra = array(); $documentAuthors = array(); foreach($document->authors as $author) { $documentAuthors []= (array)$author; } foreach($documentAuthors as $key => $author) { $temp = trim(str_replace(array($author['firstName'], $author['lastName']), '', $author['byline'])); if(in_array($temp, $bylineExtra)) { foreach($bylineExtra as $key2 => $value) { if($value == $temp) { $documentAuthors[$key2]['byline'] = trim(str_replace($temp, '', $documentAuthors[$key2]['byline'])); } } } $bylineExtra[$key] = $temp; } foreach($documentAuthors as $key => $author) { if(!empty($author['byline'])) { $authors []= $author['byline']; } else { $authors []= $author['firstName'].' '.$author['lastName']; } $authorEmails []= $author['publicEmail']; //lump all our other author data plus our "extra" author data together $tmp = $author; unset($tmp['firstName']); unset($tmp['lastName']); unset($tmp['publicEmail']); unset($tmp['byline']); unset($tmp['extraData']); $author['extraData'] = array_merge(json_decode(json_encode($author['extraData']), true), $tmp); $authorExtra []= $author['extraData']; } $authorName = implode(', ',$authors); $authorEmails = implode(', ',$authorEmails); echo '<pre>'; print_r($authorName); print_r($authorEmails); print_r($authorExtra); }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught TypeError: array_merge(): Argument #1 must be of type array, null given in /in/sGqgc:55 Stack trace: #0 /in/sGqgc(55): array_merge(NULL, Array) #1 /in/sGqgc(19): doTheThings(Object(stdClass)) #2 {main} thrown in /in/sGqgc on line 55
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught TypeError: array_merge(): Argument #1 must be of type array, null given in /in/sGqgc:55 Stack trace: #0 /in/sGqgc(55): array_merge(NULL, Array) #1 /in/sGqgc(19): doTheThings(Object(stdClass)) #2 {main} thrown in /in/sGqgc on line 55
Process exited with code 255.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: array_merge(): Expected parameter 1 to be an array, null given in /in/sGqgc on line 55 <pre>John InazuArray ( [0] => )
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33
Warning: array_merge(): Argument #1 is not an array in /in/sGqgc on line 55 <pre>John InazuArray ( [0] => )

preferences:
162.59 ms | 402 KiB | 288 Q