dongsuiying7773 2019-04-16 17:15
浏览 441
已采纳

foreach循环仅在循环中输出第一个值

Why foreach loop printing only the first value from the array?

$Jdata_cate = '[{"category_id":"103","name":"Martin","parent_id":0},{"category_id":"10","name":"Juan","parent_id":0},{"category_id":"9","name":"Kasi","parent_id":0}]';
    $J_Min = strtolower($Jdata_cate);
    $J_MinDecoded = json_decode($J_Min, true);

    $Ddata_cate = '[{"category_id":"55","name":"Abc","parent_id":0},{"category_id":"41","name":"Pedro","parent_id":0},{"category_id":"40","name":"Kasi","parent_id":0}]';
    $D_Min = strtolower($Ddata_cate);
    $D_MinDecoded = json_decode($D_Min, true);

    $both_arrays = array_merge((array)$J_MinDecoded, (array)$D_MinDecoded);

    $Delete_repeated = array_unique($both_arrays);

    foreach($Delete_repeated as $y=>$y_value){
            echo $y_value['name'] . '<br>';
    } 
  • 写回答

2条回答 默认 最新

  • drnl10253 2019-04-16 18:42
    关注

    try this solution:

    function array_unique_multidimensional($array, $key)
    {
        $temp_array = array();
        $i = 0;
        $key_array = array();
    
        foreach ($array as $val) {
            if (!in_array($val[$key], $key_array)) {
                $key_array[$i] = $val[$key];
                $temp_array[$i] = $val;
            }
            $i++;
        }
        return $temp_array;
    }
    
    $Jdata_cate = '[{"category_id":"103","name":"Martin","parent_id":0},{"category_id":"10","name":"Juan","parent_id":0},{"category_id":"9","name":"Kasi","parent_id":0}]';
    $J_Min = strtolower($Jdata_cate);
    $J_MinDecoded = json_decode($J_Min, true);
    
    $Ddata_cate = '[{"category_id":"55","name":"Abc","parent_id":0},{"category_id":"41","name":"Pedro","parent_id":0},{"category_id":"40","name":"Kasi","parent_id":0}]';
    $D_Min = strtolower($Ddata_cate);
    $D_MinDecoded = json_decode($D_Min, true);
    
    $both_arrays = array_merge((array)$J_MinDecoded, (array)$D_MinDecoded);
    
    $Delete_repeated = array_unique_multidimensional($both_arrays, 'name');
    
    foreach ($Delete_repeated as $y => $y_value) {
        echo $y_value['name'] . '<br>';
    }
    

    here instead of array_unique I'm using my defined function

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录
  • ¥15 关于#Django#的问题:我的静态文件呢?
  • ¥15 关于CPLEX的问题,请专家解答
  • ¥15 cocos的点击事件 怎么穿透到 原生fragment上。
  • ¥20 基于相关估计的TDOA算法中的加权最小二乘拟合法matlab仿真
  • ¥20 基于相关估计的TDOA算法中的自适应加权广义互相关法。
  • ¥15 abaqus CAE 2024软件启动问题
  • ¥20 基于相关估计的TDOA算法中的局部互相关函数滤波matlab仿真