从数组取消设置JSON数组项

时间:2019-03-14 09:20:59

标签: php arrays json

这是我的阵列

            Array
        (
            [0] => Array
                (
                    [web_id] => 5
                    [attendance_data] => [{"id":"61","web_student_name":"Child One","attendance":false,"image":"img/reduser.png","$$hashKey":"object:361"},
{"id":"62","web_student_name":"Child Two","attendance":true,"image":"img/greenuser.png","$$hashKey":"object:362"}]
                    [class_id] => 20
                    [date] => 2019-03-11
                    [user] => A:5
                    [created_date] => 2019-03-11 03:54:50
                    [updated_date] => 2019-03-11 05:53:50
                )

            [1] => Array
                (
                    [web_id] => 6
                    [attendance_data] => [{"id":"61","web_student_name":"Child One","attendance":false,"image":"img\/reduser.png","$$hashKey":"object:361"}]
                    [class_id] => 20
                    [date] => 2019-03-08
                    [user] => A:5
                    [created_date] => 2019-03-11 05:53:27
                    [updated_date] => 2019-03-11 05:53:27
                )

            [2] => Array
                (
                    [web_id] => 15
                    [attendance_data] => [{"id":"61","web_student_name":"Child One","attendance":true,"image":"img/greenuser.png","$$hashKey":"object:198"},{"id":"82","web_student_name":"Child Three","attendance":false,"image":"img/reduser.png","$$hashKey":"object:199"},{"id":"62","web_student_name":"Child Two","attendance":true,"image":"img/greenuser.png","$$hashKey":"object:200"}]
                    [class_id] => 20
                    [date] => 2019-03-04
                    [user] => A:5
                    [created_date] => 2019-03-14 01:42:40
                    [updated_date] => 2019-03-14 01:42:40
                )
    )

我想unset的{​​{1}}的ID不等于61的JSON项。仅显示attendance_data的JSON项列表。我尝试无法取消设置JSON项目。请提供解决方案。 预先感谢。

我尝试过的东西

attendance_data where id = 61

2 个答案:

答案 0 :(得分:1)

这可以做到

Array
(
    [0] => Array
        (
            [web_id] => 5
            [attendance_data] => [{"id":"61","web_student_name":"Child One","attendance":false,"image":"img\/reduser.png","$$hashKey":"object:361"}]
            [class_id] => 20
            [date] => 2019-03-11
            [user] => A:5
            [created_date] => 2019-03-11 03:54:50
            [updated_date] => 2019-03-11 05:53:50
        )

    [1] => Array
        (
            [web_id] => 6
            [attendance_data] => [{"id":"61","web_student_name":"Child One","attendance":false,"image":"img\/reduser.png","$$hashKey":"object:361"}]
            [class_id] => 20
            [date] => 2019-03-08
            [user] => A:5
            [created_date] => 2019-03-11 05:53:27
            [updated_date] => 2019-03-11 05:53:27
        )

)

结果

var tbl = document.getElementById("entries");
$(tbl).on('mousedown', function (e) { 
     e.preventDefault();
     if (e.which == 2) {
         var table = window.table_entries;
         var data = table.dataTable.row($(e.detail)).data();
         window.open("/plugin/Changes/@Model.Revision/" + data.BuildId, '_blank');
     }
});

答案 1 :(得分:0)

您可以通过&使用访问权限,但不要忘记取消设置该项目,以免将来出现+ array_filter错误:

<?php
$data = [
    [
        'attendance_data' => '[{"id":"61","web_student_name":"Child One","attendance":false,"image":"img/reduser.png","$$hashKey":"object:361"},{"id":"82","web_student_name":"Child Three","attendance":false,"image":"img/reduser.png","$$hashKey":"object:199"},{"id":"62","web_student_name":"Child Two","attendance":true,"image":"img/greenuser.png","$$hashKey":"object:200"}]',
    ],
    [
        'attendance_data' => '[{"id":"61","web_student_name":"Child One","attendance":false,"image":"img/reduser.png","$$hashKey":"object:361"},{"id":"82","web_student_name":"Child Three","attendance":false,"image":"img/reduser.png","$$hashKey":"object:199"},{"id":"62","web_student_name":"Child Two","attendance":true,"image":"img/greenuser.png","$$hashKey":"object:200"}]',
    ],
];
foreach ($data as &$item) {
    $item['attendance_data'] = json_encode(
        array_filter(
            json_decode($item['attendance_data'], true), 
            function($item) {
                $id = $item['id'] ?? null;
                return $id == 61;
            })
    );
}
unset($item); // don't forget this :)

var_dump($data);