2016-09-19 86 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 .. –