3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 5.6.0 - 5.6.30, 7.0.0 - 7.3.0beta1
Warning: array_merge(): Argument #1 is not an array in /in/sGqgc on line 55 <pre>John InazuArray ( [0] => )
Output for hhvm-3.22.0
Warning: Invalid operand type was used: array_merge expects array(s) or collection(s) in /in/sGqgc on line -1 <pre>John InazuArray ( [0] => )
Output for hhvm-3.21.3
Warning: Invalid operand type was used: array_merge expects array(s) or collection(s) in /in/sGqgc on line 55 <pre>John InazuArray ( [0] => )
Output for hhvm-3.18.5
Warning: Invalid operand type was used: doTheThings expects array(s) or collection(s) in /in/sGqgc on line 55 <pre>John InazuArray ( [0] => )