我有一个数组,字符串和数组有相同的名称“item”,一个实例是一个数组,另一个是数组中的字符串,我想从数组中删除整个“item”字符串< / p>
数组;
//JSON
[{
"id":"109",
"text":"dashboard",
"items":[ //to be ignored
{
"id":"1",
"text":"financial_dashboard",
"items":"109" //to be deleted
},
{
"id":"108",
"text":"project_dashboard",
"items":"109" //to be deleted
}
]
}]
//Array PHP
(
[0] => Array
(
[id] => 109
[expanded] => true
[text] => Dashboard
[items] => Array //to be ignored
(
[0] => Array
(
[id] => 1
[expanded] => true
[text] => Financial Dashboard
[items] => 109 //to be deleted
)
[1] => Array
(
[id] => 108
[expanded] => true
[text] => Project Dashboard
[items] => 109 //to be deleted
)
)
))
有什么建议吗?
答案 0 :(得分:0)
你必须知道数组是如何工作的,你可以通过delete删除这个多维数组:array [0] [0]和array [0] [1]
unset($a[0][0]);
unset($a[0][1]);
答案 1 :(得分:0)
您可以使用以下两个嵌套地图;
var arr = [{
"id":"109",
"text":"dashboard",
"items":[ //to be ignored
{
"id":"1",
"text":"financial_dashboard",
"items":"109" //to be deleted
},
{
"id":"108",
"text":"project_dashboard",
"items":"109" //to be deleted
}
]
}];
function delNestedItems(a){
return a.map(o => (o.items.map(q => (delete q.items,q)),o));
}
console.log(delNestedItems(arr))
答案 2 :(得分:0)
您可以使用像这样的递归PHP函数:
function removeItems(&$arr) {
if (!is_array($arr)) return;
if (isset($arr["items"]) && is_string($arr["items"])) unset($arr["items"]);
foreach ($arr as &$value ) removeItems($value);
}
这样称呼:
removeItems($arr);
在eval.in上看到它。
输出是:
Array
(
[0] => Array
(
[id] => 109
[text] => dashboard
[items] => Array
(
[0] => Array
(
[id] => 1
[text] => financial_dashboard
)
[1] => Array
(
[id] => 108
[text] => project_dashboard
)
)
)
)