0

I have a json object that looks something like this (truncated for readability):

{"value":["Afghanistan","Albania","Algeria","American Samoa","Andorra","Angola","Anguilla","Antarctica","Antigua and Barbuda"
],"order":[0,1,2,3,4,5,6,7,8,9
]}

How can I, using PHP, create a key->value array that I can use in a select loop?

I know how to do the select from a PHP array, but the weird setup of this JSON has me stumped. I need to use the "value" part of the JSON object as the value and the "order" as the key.

Any pointers or ideas would be appreciated.

1
  • @Rizier123, you failed to read the question fully. Decoding the JSON is only part of the question here. Commented Dec 10, 2015 at 14:02

3 Answers 3

3

you can use json_decode :

$arr = json_decode($json, true);

assuming $json is your string up here

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

1 Comment

this is an incomplete answer.
2

This should help you

// your JSON string
$json = '{"value":["Afghanistan","Albania","Algeria","American Samoa","Andorra","Angola","Anguilla","Antarctica","Antigua and Barbuda"],"order":[0,1,2,3,4,5,6,7,8,9]}';

// decode the JSON string into a usable data structure in PHP
$data = json_decode($json);

// create an empty array to store your result
$map = array();

// iterate over the $data
// map each `order` to each `value`
foreach ($data->order as $idx => $key) {
  $map[$key] = $data->value[$idx];
}

// display the result
var_dump($map);

Output

array(10) {
  [0]=>
  string(11) "Afghanistan"
  [1]=>
  string(7) "Albania"
  [2]=>
  string(7) "Algeria"
  [3]=>
  string(14) "American Samoa"
  [4]=>
  string(7) "Andorra"
  [5]=>
  string(6) "Angola"
  [6]=>
  string(8) "Anguilla"
  [7]=>
  string(10) "Antarctica"
  [8]=>
  string(19) "Antigua and Barbuda"
  [9]=>
  NULL
}

You'll note that key:9 (the 10th key) is NULL because you only have 9 values.

Comments

0

Try this:

$json = '{"value":["Afghanistan","Albania","Algeria","American Samoa","Andorra","Angola","Anguilla","Antarctica","Antigua", "Barbuda"],"order":[0,1,2,3,4,5,6,7,8,9]}';
// create a temp array to access the JSON-Data
$array = json_decode($json, true);
// Combine the two "parts" of the JSON-Data as keys and values of the new array
$array = array_combine($array['order'], $array['value']);

print_r($array);

Output will be:

Array
(
    [0] => Afghanistan
    [1] => Albania
    [2] => Algeria
    [3] => American Samoa
    [4] => Andorra
    [5] => Angola
    [6] => Anguilla
    [7] => Antarctica
    [8] => Antigua
    [9] => Barbuda
)

(!) My solution need the same number of Kys and Values. Your JSON is "wrong" for my solution. I corrected this ("Antigua and Barbuda" --> "Antigua", "Barbuda") Have a look at @naomik post

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.