1

I have Json:

[{"name":"firstname","value":"wwwwww"},{"name":"lastname","value":"w"},{"name":"age","value":"99"},{"name":"sex","value":"male"}] 

How to get array like this:

array(
    'firstname' => 'name',
    'lastname' => 'surname',
    'age' => '99',
    'sex' => 'male'
    )
0

4 Answers 4

4
<?php
echo '<pre>';
    $x=json_decode('[{"name":"firstname","value":"wwwwww"},{"name":"lastname","value":"w"},{"name":"age","value":"99"},{"name":"sex","value":"male"}]',true);
$out=array();
foreach ($x as $y){

$out[$y['name']]=$y['value'];   
}

print_r($out);

Live: http://codepad.viper-7.com/kaWRyt

Sign up to request clarification or add additional context in comments.

Comments

3

Assuming this is the only JSON you have, let's store it in $json for the sake of the example:

$json = '[{"name":"firstname","value":"wwwwww"},{"name":"lastname","value":"w"},{"name":"age","value":"99"},{"name":"sex","value":"male"}]';

First, json_decode your JSON so we can use PHP to process it:

$data = json_decode($json);

Then create an array to store your processed data an iterate through it, retrieving and assigned values to the array to achieve your desire structure:

$processed_data = array();
foreach($data as $data_field) {
    $processed_data[$data_field->name] = $data_field->value;
}

Here's a var_dump of the resulting array:

array(4) {
  ["firstname"]=>
  string(6) "wwwwww"
  ["lastname"]=>
  string(1) "w"
  ["age"]=>
  string(2) "99"
  ["sex"]=>
  string(4) "male"
}

Comments

2

Easy. Just use json_decode & then roll through the array with foreach like so:

// JSON string as set in your example.
$string = '[{"name":"first name","value":"wwwwww"},{"name":"last name","value":"w"},{"name":"age","value":"99"},{"name":"sex","value":"male"}]';

// Use `json_decode` to decode the JSON with the `true` setting to set output to an array.
$array = json_decode($string, true);

// Now roll through the array & set the final values.
$final_values = array();
foreach ($array as $key => $value) {
  $final_values[$value['name']] = $value['value'];
}

// Dump the final values to check the reordering.
echo '<pre>';
print_r($final_values);
echo '</pre>';

The output is:

Array
(
    [first name] => wwwwww
    [last name] => w
    [age] => 99
    [sex] => male
)

Comments

1

Pretty simple loop after decoding using json_decode().

<?php 

$json = '[{"name":"firstname","value":"wwwwww"},{"name":"lastname","value":"w"},{"name":"age","value":"99"},{"name":"sex","value":"male"}]';

$data = json_decode($json, true);

$d='';
foreach($data as $item){
    $d[$item['name']] = $item['value']; 
}

print_r($data);
print_r($d);

?>

The first $data array warrants this return:

Array
(
    [0] => Array
        (
            [name] => firstname
            [value] => wwwwww
        )

    [1] => Array
        (
            [name] => lastname
            [value] => w
        )

    [2] => Array
        (
            [name] => age
            [value] => 99
        )

    [3] => Array
        (
            [name] => sex
            [value] => male
        )

)

While the second array ($d) that is created in the foreach loop warrants the return you want:

Array
(
    [firstname] => wwwwww
    [lastname] => w
    [age] => 99
    [sex] => male
)

Example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.