我使用多维数组。
我想使用所有remove元素删除相同的值,但不使用任何功能。
请参阅下面的我的多维数组代码。
<?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>";
?>
输出:-
Array
(
[0] => test_3
[1] => test_4
)
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
)
)
)
)
在这里,我只想这样。
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]);
}
}
}
您需要使用正确键在数组的顶层(
$b
)上使用unset。
关于php - PHP:-在多维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39566940/