Thursday, 27 December 2018

Which method should I use to extract the one attribute from a huge response?

I have a JSON response shown as below. an example of the print_r result is shown below

(
[0] => stdClass Object
(
    [name] => Venezuela (Bolivarian Republic of)
    [topLevelDomain] => Array
        (
            [0] => .ve
        )

    [alpha2Code] => VE
    [alpha3Code] => VEN
    [callingCodes] => Array
        (
            [0] => 58
        )

    [capital] => Caracas
    [cioc] => VEN
),
[1] => stdClass Object
(
    [name] => Venezuela (Bolivarian Republic of)
    [topLevelDomain] => Array
        (
            [0] => .ve
        )

    [alpha2Code] => VE
    [alpha3Code] => VEN
    [callingCodes] => Array
        (
            [0] => 58
        )

    [capital] => Caracas
    [cioc] => VEN
),
[2] => stdClass Object
(
    [name] => Venezuela (Bolivarian Republic of)
    [topLevelDomain] => Array
        (
            [0] => .ve
        )

    [alpha2Code] => VE
    [alpha3Code] => VEN
    [callingCodes] => Array
        (
            [0] => 58
        )

    [capital] => Caracas
    [cioc] => VEN
),
....
)

I want to extract only names from the response.

should I use a loop through the array and extract every name from every object in the array and push it in the array or Should I use the following code?

$language = array_map(function($object)
{
    return $object->name; 
}, $jsonReponse); 

Which would be the best choice and why?



from Which method should I use to extract the one attribute from a huge response?

No comments:

Post a Comment