3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dbjunk = "Itemid:Quantity:Price:name, itemid2:quantity2:price2:name2"; $parsed = parseItems($dbjunk); foreach($parsed as $p){ list($itemID, $Quantity, $Price, $name) = array_values($p); var_dump($itemID, $Quantity, $Price, $name); } function parseItems($dbjunk){ $cart = array(); $items = explode(",",$dbjunk); foreach($items as $i){ $chunks = explode(":", $i); $cart[] = array( "ItemID" => $chunks[0] , "Quantity" => $chunks[1] , "Price" => $chunks[2] , "name" => $chunks[3] ); } return $cart; }
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 8.0.0 - 8.0.12, 8.1rc1 - rc3
string(6) "Itemid" string(8) "Quantity" string(5) "Price" string(4) "name" string(8) " itemid2" string(9) "quantity2" string(6) "price2" string(5) "name2"

preferences:
63.08 ms | 461 KiB | 5 Q