2016-09-19 87 views
2

我使用两个多维数组。PHP: - 在多维数组

我想要删除与所有删除元素相同的值,但不使用任何函数。

查看下面我的代码多维数组。

<?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>"; 
?> 

OUT PUT: -

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 
         ) 
       ) 
     ) 
) 

请给我建议。

+0

你有任何代码与你的试用来解决这个问题吗? – rokas

+0

是的,我尝试了很多代码,但我无法获得完美的结果@rokas。 –

+0

只是使用一个foreach(可能是两个)然后使用一个如果在数组中,然后取消设置 – Ghost

回答

1
foreach ($b as $j => $inner) { 
    foreach ($inner['value'] as $k => $value) { 
     if (in_array($value['value'], $a)) {  
      unset($b[$j]['value'][$k]); 
     } 
    } 
} 

您需要使用正确的密钥在顶层数组($b)上取消设置。

+0

谢谢你的帮助,但不能工作@JanHolas。 –

+1

你必须复制它错了,我只是测试它,它的工作,这里是完整的代码:http://sandbox.onlinephpfunctions.com/code/a73bc763741de2524098c6cf032068ec6f4beccd –

+0

亚亚,我的错误对不起..它工作正常..非常感谢@JanHolas .. –