#php #arrays #json
#php #массивы #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
элемент JSON из attendance_data
, идентификатор которого не равен 61. Показывать только attendance_data where id = 61
список элементов JSON only. Я пытался, потому что не смог сбросить элемент JSON. Пожалуйста, предоставьте решение.
Заранее спасибо.
ЧТО я ПРОБОВАЛ
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;
}
Комментарии:
1. Сначала, пожалуйста, покажите нам, что вы пробовали
2.
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; }
@RiggsFolly3. измените $data [0] на $ value в вашем коде
Ответ №1:
Это сделает это
$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 amp;$a) {
$json = json_decode($a['attendance_data']);
foreach( $json as $ix => amp;$j ) {
if ( isset($j->id) amp;amp; $j->id != 61 ) {
unset($json[$ix]);
}
}
$a['attendance_data'] = json_encode($json);
}
print_r($arr);
Результат
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
)
)
Ответ №2:
вы можете использовать access by amp;, но не забудьте отменить настройку элемента, чтобы избежать ошибок в будущем 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 amp;$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);