0

THIS IS MY ARRAY

            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
                )
    )

I want to unset the JSON item of attendance_data where id is not equal to 61. Only show the JSON item list of attendance_data where id = 61 only. I tried for not able to unset JSON item. Please provide the solution. Thanks in advance.

WHAT I TRIED

foreach($data as $key => $value) { 
    $json_arr = json_decode($data[0]['attendance_data'], true);
    $arr_index = array();
     foreach ($json_arr as $key1 => $value1) { 
        if ($value1['id'] != $childid) { 
            $arr_index[] = $key1; 
        } 
     } 
     foreach ($arr_index as $i) { 
        unset($json_arr[$i]); 
     } 
     $json_arr = array_values($json_arr);
     $json_arr_en = json_encode($json_arr);
     $data[$key1]['attendance_data'] = $json_arr_en; 
}
3
  • 2
    First, please show us what you have tried Commented Mar 14, 2019 at 9:24
  • foreach($data as $key => $value) { $json_arr = json_decode($data[0]['attendance_data'], true); $arr_index = array(); foreach ($json_arr as $key1 => $value1) { if ($value1['id'] != $childid) { $arr_index[] = $key1; } } foreach ($arr_index as $i) { unset($json_arr[$i]); } $json_arr = array_values($json_arr); $json_arr_en = json_encode($json_arr); $data[$key1]['attendance_data'] = $json_arr_en; } @RiggsFolly Commented Mar 14, 2019 at 9:36
  • change $data[0] to $value in your code Commented Mar 14, 2019 at 9:54

2 Answers 2

1

This will do that

$arr = [ [  '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'
          ],
         [  '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'
         ]
        ];

foreach($arr as &$a) {
    $json = json_decode($a['attendance_data']);
    foreach( $json as $ix => &$j ) {
        if ( isset($j->id) && $j->id != 61 ) {
            unset($json[$ix]);
        }   
    }
    $a['attendance_data'] = json_encode($json);
}

print_r($arr);

RESULT

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
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

you can use access by & but don't forget to unset the item to avoid errors in future + 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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.