On my server side I receive following format of request:
"medium x 1, small x 2"
How can I transform these data in this format:
array(0:{"medium"=>1},1:{"small"=>2})
Thanks!!
On my server side I receive following format of request:
"medium x 1, small x 2"
How can I transform these data in this format:
array(0:{"medium"=>1},1:{"small"=>2})
Thanks!!
It's simple, harnessing explode() and looping with foreach() {...}:
$str = "medium x 1, small x 2";
$a = explode(",", $str);
$data = array();
foreach($a as $s) {
list($size, $quantity) = explode('x', $s);
$data[] = array($size => $quantity);
}
// show it
print(json_encode($data));
Which returns:
[{"medium ":" 1"},{" small ":" 2"}]