php - PHP:-在多维数组中

标签 php multidimensional-array

我使用多维数组。

我想使用所有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/

相关文章:

javascript - 如果它是 jquery 中的超链接,则获取 td 的值

php - 将 PHP 加密/解密转换为 Node.js

mysql - PHP5-FPM 和 MYSQL

php - Laravel 5.1 嵌套复杂查询

javascript - 在 knockout.js 中访问多维数组中的特定项目

php - 如何推送到 php 中的多维数组?

php - 我如何编写一个查询来获取 mysql 中的计数

perl - 多维数据结构的 Moose 特征

perl - 如何复制和/或重新分配多维数组中的数组元素?

python - 仅沿最后两个维度获取矩阵乘积的单个操作