1

when i try:

echo $response;

the output is LIKE (similiar):

{
    "identity": {
        "result": [
            {
                "name" = "Dilan",
                "place_id" = 1
            },
            {
                "name" = "Milea",
                "place_id" = 1
            }
        ]
    }
},
{
    "identity": {
        "result": [
            {
                "name" = "Ariel",
                "place_id" = 2
            },
            {
                "name" = "Noah",
                "place_id" = 2
            }
        ]
    }
}

nah when i try to echo $response['identity'], the output is shows the first array only, all my operation to $response is only applied for first array only, i can't do any operation to the second array.

what i want is merged all array output into 1 array, LIKE this below:

{
    {
        "identity": {
            "result": [
                {
                    "name" = "Dilan",
                    "place_id" = 1
                },
                {
                    "name" = "Milea",
                    "place_id" = 1
                }
            ]
        }
    },
    {
        "identity": {
            "result": [
                {
                    "name" = "Ariel",
                    "place_id" = 2
                },
                {
                    "name" = "Noah",
                    "place_id" = 2
                }
            ]
        }
    }
}

here my controller

enter image description here

thank you! your help is very needed^^

13
  • you don't need to merge . you need to loop over $response and perform task because if merge then it will be same as before (as you mention above merge code) Commented Sep 28, 2020 at 7:02
  • btw the output of $response is string, when i try to convert to array like $test = json_decode($response, true); dd($test); then, its only execute the first array which only dump the first array only Commented Sep 28, 2020 at 7:08
  • I don't think doing echo $response; will output an array. How do you build the "array" ? Commented Sep 28, 2020 at 7:08
  • its not a array but a json_encoded Commented Sep 28, 2020 at 7:09
  • @executable when i try dd($response); without doing json_decode, the output is still the first array only Commented Sep 28, 2020 at 7:10

2 Answers 2

1

why yo need to do this, you can marge all "result" in one array like this

$allResult =[]; 
foreach($arrayes as $array){

      $allResult = array_merge($allResult,$array['identity']['result']);
 }
Sign up to request clarification or add additional context in comments.

2 Comments

but its not dynamic, when the $response is showing 5 array, i think i can't use this code, btw thanks for response!
if you need to dynamic it use ``` $allResult =[]; foreach($arrayes as $array){ $allResult = array_merge($allResult,$array['identity']['result']); } ```
1

the problem is about your json syntax. because you should write

[
    {"identity": {
        ...
    }}
]

instead of:

{
    {"identity": {
        ...
    }}
}

please pay attention to first brackets and accolades. in your json sample "indentifies" must define in an array and at last pass it into a loop that my suggest is foreach iterates. but it's not neccesary to use loop and you can access to every indices of your array with its key. json sample and code:

<?php

// json
$json = '[{
    "identity": {
        "result": [{
            "name": "Dilan",
            "place_id": 1
        }, {
            "name": "Milea",
            "place_id": 1
        }]
    }
}, {
    "identity": {
        "result": [{
            "name": "Ariel",
            "place_id": 2
        }, {
            "name": "Noah",
            "place_id": 2
        }]
    }
}]';

// decode json
$json = json_decode($json, true);

// pass your json to foreach
foreach($json as $json_index){

    // print every indice to process it
    print_r($json_index);
    
    // or getting your result parameters
    foreach($json_index['identity']['result'] as $result){
        echo($result['name'] . ' = ' . $result['place_id']);
        echo PHP_EOL;
    }

}



// or



print_r($json[1]);
?>

2 Comments

ok thanks for that response, i can do that, but i cant get the value from an element of that array, its still shows the first array when i try to dd($json); but when print_r or var_dump ($json), its shows all of array data
@MuhammadRobbiZulfikar i tested my code and it was ok. it show every each loop "name" param. but i don't got your mean. please w8 for others. maybe them can solve your problem

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.