-1

I have a one string.

$a = '[{"size":"6Y","quantity":15}]';

I want to store 6Y and 15 in array. Please help me.

student
  • 43
  • 6

4 Answers4

1

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
)
Pupil
  • 23,834
  • 6
  • 44
  • 66
1

Use call_user_func_array with array_merge

  1. Json decode your string
  2. Flatten the array

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
)
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

Try this, use json_decode:

<?php
$a = '[{"size":"6Y","quantity":15}]';
$a = substr($a, 1, -1);
print_r((array)json_decode($a));
?>
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
0
$a = '[{"size":"6Y","quantity":15}]';
$v = json_decode($a);
print_r(array_values($v));
xims
  • 1,570
  • 17
  • 22