aurelSon
aurelSon

Reputation: 73

With Php in a JSON array how to retrieve the value of an item according to another associated value?

I use json_decode Php function to get a JSON array of countries datas.

{
    "Countries":
    [
        {
            "Code": "AD",
            "Name": "Andorre"
        },
        {
            "Code": "AE",
            "Name": "Émirats Arabes Unis"
        },
        {
            "Code": "AF",
            "Name": "Afghanistan"
        },
        {
            "Code": "AG",
            "Name": "Antigua-Et-Barbuda"
        },

If I want to retrieve the Code of the 1st element I can do:

$result = json_decode($sXML);

$final = $result->Countries[0]->Name;

And $final will have the value of 'Andorre'. But what if I want to retrieve the same value 'Andorre' using its correspoding Code ?

Is it possible to do it ? I know there is an option for the json_function() to obtain an associative array instead of a JSON array, but how would you use it to get the value 'Andorre' using its Code ?

Thank you

Upvotes: 3

Views: 59

Answers (3)

Sahil Gulati
Sahil Gulati

Reputation: 15141

Here we are using two function for achieving this array_column and array_combine to retrieve the expected output.

Try this code snippet here

<?php
ini_set('display_errors', 1);
$string='{
    "Countries":
    [
        {
            "Code": "AD",
            "Name": "Andorre"
        },
        {
            "Code": "AE",
            "Name": "Émirats Arabes Unis"
        },
        {
            "Code": "AF",
            "Name": "Afghanistan"
        },
        {
            "Code": "AG",
            "Name": "Antigua-Et-Barbuda"
        }
    ]
    }';
$array=json_decode($string,true);
$codes=  array_column($array["Countries"], "Code");//Retrieving column code
$names=  array_column($array["Countries"], "Name");//Retrieving column name
$data=  array_combine($codes, $names); //combining array with code as keys and names as values.
print_r($data);`

Output:

Array
(
    [AD] => Andorre
    [AE] => Émirats Arabes Unis
    [AF] => Afghanistan
    [AG] => Antigua-Et-Barbuda
)

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 99001

I guess you can use something like:

function search_json($obj, $field, $value) {
    foreach($obj as $item) {
        foreach($item as $child) {
            if(isset($child->$field) && $child->$field == $value) {
                return $child->Name;
            }
        }
    }
    return null;
}

print_r(search_json($result, "Code", "AD"));
# Andorre

Upvotes: 0

ewcz
ewcz

Reputation: 13097

<?php

$s = '{
    "Countries":
    [
        {
            "Code": "AD",
            "Name": "Andorre"
        },
        {
            "Code": "AE",
            "Name": "Émirats Arabes Unis"
        },
        {
            "Code": "AF",
            "Name": "Afghanistan"
        },
        {
            "Code": "AG",
            "Name": "Antigua-Et-Barbuda"
        }
    ]
}';

$arr = json_decode($s, true);
print_r(array_column($arr['Countries'], "Name", "Code"));

?>

yields

Array
(
    [AD] => Andorre
    [AE] => Émirats Arabes Unis
    [AF] => Afghanistan
    [AG] => Antigua-Et-Barbuda
)

Upvotes: 2

Related Questions