I have a one string.
$a = '[{"size":"6Y","quantity":15}]';
I want to store 6Y and 15 in array. Please help me.
I have a one string.
$a = '[{"size":"6Y","quantity":15}]';
I want to store 6Y and 15 in array. Please help me.
Use json_decode()
You have a json encoded string.
And need to decode it and assign size and quantity to an array.
Working example:
<?php
$a = '[{"size":"6Y","quantity":15}]';
$b = json_decode($a, TRUE);
$c = array();
if (! empty($b[0])) {
foreach ($b[0] as $k => $v) {
$c[$k] = $v;
}
}
echo '<pre>';print_r($c);echo '</pre>';
?>
Output:
Array
(
[size] => 6Y
[quantity] => 15
)
Use call_user_func_array with array_merge
like this:
<?php
$a = '[{"size":"6Y","quantity":15}]';
$b = call_user_func_array('array_merge', json_decode($a,true));
print_r($b);
Output:
Array
(
[size] => 6Y
[quantity] => 15
)
Try this, use json_decode:
<?php
$a = '[{"size":"6Y","quantity":15}]';
$a = substr($a, 1, -1);
print_r((array)json_decode($a));
?>
$a = '[{"size":"6Y","quantity":15}]';
$v = json_decode($a);
print_r(array_values($v));