【问题标题】:Laravel, how to merge Many array with array itself from foreach?Laravel,如何将许多数组与来自foreach的数组本身合并?
【发布时间】:2021-01-13 17:59:36
【问题描述】:

当我尝试时:

echo $response;

输出是 LIKE(类似):

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

不,当我尝试回显 $response['identity'] 时,输出仅显示第一个数组,我对 $response 的所有操作仅适用于第一个数组,我无法对第二个数组执行任何操作数组。

我想要的是将所有数组输出合并到 1 个数组中,如下所示:

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

这里是我的控制器

谢谢!非常需要你的帮助^^

【问题讨论】:

  • 你不需要合并 .您需要遍历$response 并执行任务,因为如果合并则它将与(as you mention above merge code) 之前相同
  • btw $response 的输出是字符串,当我尝试转换为像 $test = json_decode($response, true); 这样的数组时dd($测试);然后,它只执行只转储第一个数组的第一个数组
  • 我认为echo $response; 不会输出数组。你如何构建“数组”?
  • 它不是一个数组,而是一个 json_encoded
  • @executable 当我尝试 dd($response);不做json_decode,输出还是只有第一个数组

标签: php arrays json laravel


【解决方案1】:

问题在于您的 json 语法。因为你应该写

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

代替:

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

请注意前面的括号和荣誉。在您的 json 示例中,“标识”必须在一个数组中定义,最后将它传递到我建议 foreach 迭代的循环中。但不必使用循环,您可以使用其键访问数组的每个索引。 json示例和代码:

<?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]);
?>

【讨论】:

  • 好的,感谢您的回复,我可以做到,但我无法从该数组的元素中获取值,当我尝试 dd($json); 时它仍然显示第一个数组;但是当 print_r 或 var_dump ($json) 时,它会显示所有的数组数据
  • @MuhammadRobbiZulfikar 我测试了我的代码,一切正常。它显示每个循环“名称”参数。但我不明白你的意思。请为其他人w8。也许他们可以解决你的问题
【解决方案2】:

为什么你需要这样做, 您可以像这样将所有“结果”合并到一个数组中

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

      $allResult = array_merge($allResult,$array['identity']['result']);
 }

【讨论】:

  • 但它不是动态的,当 $response 显示 5 数组时,我想我不能使用此代码,顺便感谢您的回复!
  • 如果你需要动态它使用``` $allResult =[]; foreach($arrayes as $array){ $allResult = array_merge($allResult,$array['identity']['result']); } ```
猜你喜欢
  • 2016-04-02
  • 2015-04-24
  • 1970-01-01
  • 1970-01-01
  • 2020-06-11
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
相关资源
最近更新 更多