使用 PHP 从 json 对象中删除数据后出现错误格式
Wrong format is coming after delete data from json object using PHP
我正在根据多个索引值从数组中删除数据并重新编制索引,但我仍然得到错误的格式。我的代码如下:
$val = '[{"label":"","value":""},{"label":"Rs 100 to Rs 499","value":"162"},{"label":"Rs 500 to Rs
999","value":"140"},{"label":"Rs 1000 to Rs 1999","value":"141"},{"label":"Rs 2000 to Rs
2999","value":"142"},{"label":"Rs 3000 to Rs 3999","value":"143"},{"label":"Rs 4000 to Rs
4999","value":"144"},{"label":"Rs 5000 to Rs 5999","value":"145"},{"label":"Rs 10000 Above","value":"146"},{"label":"Rs
20000 Above","value":"147"},{"label":"Rs 30000 Above","value":"148"},{"label":"Rs 50000
Above","value":"149"},{"label":"Rs 70000 Above","value":"150"},{"label":"Rs 80000 Above","value":"151"},{"label":"Rs
100000 Above","value":"152"},{"label":"Rs 6000 to Rs 6999","value":"153"},{"label":"Rs 7000 to Rs
7999","value":"154"},{"label":"Rs 8000 to Rs 8999","value":"155"},{"label":"Rs 9000 to Rs 9999","value":"156"}]';
$val = json_decode($val, true);
// Below are the index values to be deleted.
$keyArr = [0, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
foreach ($keyArr as $i) {
unset($val[$i]);
}
array_values($val);
echo json_encode($val);
exit;
这里我得到如下输出。
{"1":{"label":"Rs 100 to Rs 499","value":"162"},"3":{"label":"Rs 1000 to Rs 1999","value":"141"},"4":{"label":"Rs 2000
to Rs 2999","value":"142"},"5":{"label":"Rs 3000 to Rs 3999","value":"143"}}
但我的预期结果应该如下所示。
[{"label":"Rs 100 to Rs 499","value":"162"},{"label":"Rs 1000 to Rs 1999","value":"141"},{"label":"Rs 2000
to Rs 2999","value":"142"},{"label":"Rs 3000 to Rs 3999","value":"143"}]
正如我所见,您没有将 array_value() 输出分配给您的实际变量(即 $val)
$val='[{"label":"","value":""},{"label":"Rs 100 to Rs 499","value":"162"},{"label":"Rs 500 to Rs
999","value":"140"},{"label":"Rs 1000 to Rs 1999","value":"141"},{"label":"Rs 2000 to Rs
2999","value":"142"},{"label":"Rs 3000 to Rs 3999","value":"143"},{"label":"Rs 4000 to Rs
4999","value":"144"},{"label":"Rs 5000 to Rs 5999","value":"145"},{"label":"Rs 10000 Above","value":"146"},{"label":"Rs
20000 Above","value":"147"},{"label":"Rs 30000 Above","value":"148"},{"label":"Rs 50000
Above","value":"149"},{"label":"Rs 70000 Above","value":"150"},{"label":"Rs 80000 Above","value":"151"},{"label":"Rs
100000 Above","value":"152"},{"label":"Rs 6000 to Rs 6999","value":"153"},{"label":"Rs 7000 to Rs
7999","value":"154"},{"label":"Rs 8000 to Rs 8999","value":"155"},{"label":"Rs 9000 to Rs 9999","value":"156"}]';
$val=json_decode($val,True);
//Below are the index values to be deleted.
$keyArr=[0,2,6,7,8,9,10,11,12,13,14,15,16,17,18];
foreach ($keyArr as $i) {
unset($val[$i]);
}
$val= array_values($val);
echo json_encode($val);exit;
我想回答备选方案,使用 in_array
检查未删除的键和仅分配给该值的新变量 ...
$val='[{"label":"","value":""},{"label":"Rs 100 to Rs 499","value":"162"},{"label":"Rs 500 to Rs
999","value":"140"},{"label":"Rs 1000 to Rs 1999","value":"141"},{"label":"Rs 2000 to Rs
2999","value":"142"},{"label":"Rs 3000 to Rs 3999","value":"143"},{"label":"Rs 4000 to Rs
4999","value":"144"},{"label":"Rs 5000 to Rs 5999","value":"145"},{"label":"Rs 10000 Above","value":"146"},{"label":"Rs
20000 Above","value":"147"},{"label":"Rs 30000 Above","value":"148"},{"label":"Rs 50000
Above","value":"149"},{"label":"Rs 70000 Above","value":"150"},{"label":"Rs 80000 Above","value":"151"},{"label":"Rs
100000 Above","value":"152"},{"label":"Rs 6000 to Rs 6999","value":"153"},{"label":"Rs 7000 to Rs
7999","value":"154"},{"label":"Rs 8000 to Rs 8999","value":"155"},{"label":"Rs 9000 to Rs 9999","value":"156"}]';
$val=json_decode($val,True);
//Below are the index values to be deleted.
$keyArr=[0,2,6,7,8,9,10,11,12,13,14,15,16,17,18];
$need = [];
foreach ($val as $k=>$i) {
if(in_array($k,$keyArr) == false) $need[] = $i;
}
echo json_encode($need);exit;
array_values方法returns新数组所以你需要先把它赋给一个变量。
foreach ($keyArr as $i) {
unset($val[$i]);
}
$newArr = array_values($val);
echo json_encode($newArr);exit;
我正在根据多个索引值从数组中删除数据并重新编制索引,但我仍然得到错误的格式。我的代码如下:
$val = '[{"label":"","value":""},{"label":"Rs 100 to Rs 499","value":"162"},{"label":"Rs 500 to Rs
999","value":"140"},{"label":"Rs 1000 to Rs 1999","value":"141"},{"label":"Rs 2000 to Rs
2999","value":"142"},{"label":"Rs 3000 to Rs 3999","value":"143"},{"label":"Rs 4000 to Rs
4999","value":"144"},{"label":"Rs 5000 to Rs 5999","value":"145"},{"label":"Rs 10000 Above","value":"146"},{"label":"Rs
20000 Above","value":"147"},{"label":"Rs 30000 Above","value":"148"},{"label":"Rs 50000
Above","value":"149"},{"label":"Rs 70000 Above","value":"150"},{"label":"Rs 80000 Above","value":"151"},{"label":"Rs
100000 Above","value":"152"},{"label":"Rs 6000 to Rs 6999","value":"153"},{"label":"Rs 7000 to Rs
7999","value":"154"},{"label":"Rs 8000 to Rs 8999","value":"155"},{"label":"Rs 9000 to Rs 9999","value":"156"}]';
$val = json_decode($val, true);
// Below are the index values to be deleted.
$keyArr = [0, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
foreach ($keyArr as $i) {
unset($val[$i]);
}
array_values($val);
echo json_encode($val);
exit;
这里我得到如下输出。
{"1":{"label":"Rs 100 to Rs 499","value":"162"},"3":{"label":"Rs 1000 to Rs 1999","value":"141"},"4":{"label":"Rs 2000
to Rs 2999","value":"142"},"5":{"label":"Rs 3000 to Rs 3999","value":"143"}}
但我的预期结果应该如下所示。
[{"label":"Rs 100 to Rs 499","value":"162"},{"label":"Rs 1000 to Rs 1999","value":"141"},{"label":"Rs 2000
to Rs 2999","value":"142"},{"label":"Rs 3000 to Rs 3999","value":"143"}]
正如我所见,您没有将 array_value() 输出分配给您的实际变量(即 $val)
$val='[{"label":"","value":""},{"label":"Rs 100 to Rs 499","value":"162"},{"label":"Rs 500 to Rs
999","value":"140"},{"label":"Rs 1000 to Rs 1999","value":"141"},{"label":"Rs 2000 to Rs
2999","value":"142"},{"label":"Rs 3000 to Rs 3999","value":"143"},{"label":"Rs 4000 to Rs
4999","value":"144"},{"label":"Rs 5000 to Rs 5999","value":"145"},{"label":"Rs 10000 Above","value":"146"},{"label":"Rs
20000 Above","value":"147"},{"label":"Rs 30000 Above","value":"148"},{"label":"Rs 50000
Above","value":"149"},{"label":"Rs 70000 Above","value":"150"},{"label":"Rs 80000 Above","value":"151"},{"label":"Rs
100000 Above","value":"152"},{"label":"Rs 6000 to Rs 6999","value":"153"},{"label":"Rs 7000 to Rs
7999","value":"154"},{"label":"Rs 8000 to Rs 8999","value":"155"},{"label":"Rs 9000 to Rs 9999","value":"156"}]';
$val=json_decode($val,True);
//Below are the index values to be deleted.
$keyArr=[0,2,6,7,8,9,10,11,12,13,14,15,16,17,18];
foreach ($keyArr as $i) {
unset($val[$i]);
}
$val= array_values($val);
echo json_encode($val);exit;
我想回答备选方案,使用 in_array
检查未删除的键和仅分配给该值的新变量 ...
$val='[{"label":"","value":""},{"label":"Rs 100 to Rs 499","value":"162"},{"label":"Rs 500 to Rs
999","value":"140"},{"label":"Rs 1000 to Rs 1999","value":"141"},{"label":"Rs 2000 to Rs
2999","value":"142"},{"label":"Rs 3000 to Rs 3999","value":"143"},{"label":"Rs 4000 to Rs
4999","value":"144"},{"label":"Rs 5000 to Rs 5999","value":"145"},{"label":"Rs 10000 Above","value":"146"},{"label":"Rs
20000 Above","value":"147"},{"label":"Rs 30000 Above","value":"148"},{"label":"Rs 50000
Above","value":"149"},{"label":"Rs 70000 Above","value":"150"},{"label":"Rs 80000 Above","value":"151"},{"label":"Rs
100000 Above","value":"152"},{"label":"Rs 6000 to Rs 6999","value":"153"},{"label":"Rs 7000 to Rs
7999","value":"154"},{"label":"Rs 8000 to Rs 8999","value":"155"},{"label":"Rs 9000 to Rs 9999","value":"156"}]';
$val=json_decode($val,True);
//Below are the index values to be deleted.
$keyArr=[0,2,6,7,8,9,10,11,12,13,14,15,16,17,18];
$need = [];
foreach ($val as $k=>$i) {
if(in_array($k,$keyArr) == false) $need[] = $i;
}
echo json_encode($need);exit;
array_values方法returns新数组所以你需要先把它赋给一个变量。
foreach ($keyArr as $i) {
unset($val[$i]);
}
$newArr = array_values($val);
echo json_encode($newArr);exit;