PHP:-在多维数组中
我使用两个多维数组 我想删除所有删除元素的相同值,但不使用任何函数 请参见下面我的多维数组代码PHP:-在多维数组中,php,multidimensional-array,Php,Multidimensional Array,我使用两个多维数组 我想删除所有删除元素的相同值,但不使用任何函数 请参见下面我的多维数组代码 <?php $a=array("0" => "test_3","1" => "test_4"); $b=array('test'=>array("label"=>"TEST","value"=>array( "0"=>array("value"=>"test_1","label"=>"[
<?php
$a=array("0" => "test_3","1" => "test_4");
$b=array('test'=>array("label"=>"TEST","value"=>array(
"0"=>array("value"=>"test_1","label"=>"[test] Services_1"),
"1"=>array("value"=>"test_2","label"=>"[test] Services_2"),
"2"=>array("value"=>"test_3","label"=>"[test] Services_3"),
"3"=>array("value"=>"test_4","label"=>"[test] Services_4"),
)
),
'test1'=>array("label"=>"TEST","value"=>array(
"0"=>array("value"=>"test_11","label"=>"[test] Services_11"),
"1"=>array("value"=>"test_12","label"=>"[test] Services_12"),
"2"=>array("value"=>"test_13","label"=>"[test] Services_13"),
"3"=>array("value"=>"test_14","label"=>"[test] Services_14"),
)
)
);
echo "<pre>";
print_r($a);
print_r($b);
foreach($a as $val)
{
$search =$val;
$result = array_map(function ($value) use ($search) {
//print_r($value);
if(($key = array_search($search, $value['value'])) !== false) {
unset($value['value'][$key]);
}
}, $b);
print_r($result);
}
echo "</pre>";
?>
在这里,我只想这样
foreach ($b as $j => $inner) {
foreach ($inner['value'] as $k => $value) {
if (in_array($value['value'], $a)) {
unset($b[$j]['value'][$k]);
}
}
}
请给我一个建议
您需要在数组的顶层(
$b
)使用具有正确键的unset 你的试用版中有解决问题的代码吗?是的,我为此尝试了很多代码,但我无法得到完美的结果@rokas。只需使用foreach(可能两个),然后使用if-in数组,然后取消设置Hey Ghost谢谢,我已经使用foreach-then-if和unset,检查我更新的问题,但我找不到我的真实结果。你一定是复制错了,我刚刚测试了它,它正在工作,下面是完整的代码:
Array
(
[test] => Array
(
[label] => TEST
[value] => Array
(
[0] => Array
(
[value] => test_1
[label] => [test] Services_1
)
[1] => Array
(
[value] => test_2
[label] => [test] Services_2
)
)
)
[test1] => Array
(
[label] => TEST
[value] => Array
(
[0] => Array
(
[value] => test_11
[label] => [test] Services_11
)
[1] => Array
(
[value] => test_12
[label] => [test] Services_12
)
[2] => Array
(
[value] => test_13
[label] => [test] Services_13
)
[3] => Array
(
[value] => test_14
[label] => [test] Services_14
)
)
)
)
foreach ($b as $j => $inner) {
foreach ($inner['value'] as $k => $value) {
if (in_array($value['value'], $a)) {
unset($b[$j]['value'][$k]);
}
}
}