3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array1 = "144|aku|1!!!122|dia|2"; $array2 = "144|aku|1!!!211|dia|1"; $array1 = explode("!!!", $array1); $array2 = explode("!!!", $array2); Foreach($array1 as $val){ List($id, $name, $amount) = explode("|", $val); $result[$id] = ['name' => $name, 'amount' => $amount]; } Foreach($array2 as $val){ List($id, $name, $amount) = explode("|", $val); If(isset($result[$id])){ $result[$id]['amount'] += $amount; }Else{ $result[$id] = ['name' => $name, 'amount' => $amount]; } } Echo "Json string:\n"; $json = json_encode($result); Var_dump($json); Echo "\n\nback to array: \n"; Var_dump(json_decode($json, true));
Output for git.master, git.master_jit, rfc.property-hooks
Json string: string(101) "{"144":{"name":"aku","amount":2},"122":{"name":"dia","amount":"2"},"211":{"name":"dia","amount":"1"}}" back to array: array(3) { [144]=> array(2) { ["name"]=> string(3) "aku" ["amount"]=> int(2) } [122]=> array(2) { ["name"]=> string(3) "dia" ["amount"]=> string(1) "2" } [211]=> array(2) { ["name"]=> string(3) "dia" ["amount"]=> string(1) "1" } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
37.35 ms | 406 KiB | 5 Q