PiraTa
PiraTa

Reputation: 485

JSON to Array PHP

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'
    )

Upvotes: 1

Views: 147

Answers (4)

Giacomo1968
Giacomo1968

Reputation: 26014

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
)

Upvotes: 2

Darren
Darren

Reputation: 13128

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

Upvotes: 1

user557846
user557846

Reputation:

<?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

Upvotes: 4

Josh Davenport-Smith
Josh Davenport-Smith

Reputation: 5511

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"
}

Upvotes: 3

Related Questions