doubei2231 2015-07-06 19:40
浏览 58
已采纳

值在JSON的数组中使用一次

I have a Json array the following values:

[small, medium, small, large, medium, medium, large, small]

And I would like to get this array only:

[small, medium, large]

Well, each values use only once in the second array from the first one.

        foreach($json_array as $json_arr){ 
            if (!isset($size_array)) {
                $size_array = array();
            }   

            foreach($size_array as $s_a) {
                if ($s_a != $json_arr['size']) {
                    $x = true;
                } 
            }

            $size_array[] = $json_arr['size'];

        }               

        echo "<br><br><br>";
        foreach($size_array as $s_a) {
            echo $s_a;
        }


[{"size":"small"},{"size":"small"},{"size":"medium"},{"size":"medium"},{"size":"large"},{"size":"small"},{"size":"large"},{"size":"large"},{"size":"large"}]
  • 写回答

3条回答 默认 最新

  • dougong5817 2015-07-06 20:00
    关注

    Your given json format is not correct. I created an example for your better understanding :-

    <?php 
    $json = '{"0":"small","1":"medium","2":"small","3":"large","4":"medium","5":"medium","6":"large","7":"small"}';
    $new_array = json_decode($json);
    $common_array = array();
    foreach($new_array as $new_arr){
        $common_array[] = $new_arr;
    }
    echo "<pre/>";print_r($common_array);
    echo "<pre/>";print_r(array_unique($common_array));
    ?>
    

    Output:-https://eval.in/393669

    Note:- I have taken your json format. modified it to be correct and make example for that. It will be easy to understand i hope. thanks.

    Based on your latest json format edit, here the link, which ave proper answer for that:-

    https://eval.in/393680

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

报告相同问题?

悬赏问题

  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导