簡體   English   中英

檢查任何 2 個具有不同維度的數組中的任何值在 php 中是否相同

[英]Check if any value in any 2 arrays with different dimension is same in php

我有 2 個數組 -

$arr1 =

Array
(
    [0] => Array
        (
            [id] => 1
            [sender_email] => test1@test.com
            [to_email] => test@test.com
            [description] => 5390BF675E1464F32202B
            [created_at] => 2020-01-21 04:50:21
            [status] => 1
        )

    [1] => Array
        (
            [id] => 30
            [sender_email] => abcd@gmail.com
            [to_email] => test@test.com
            [description] => 729237A55E2EDCB80B18F
            [created_at] => 2020-01-27 12:51:34
            [status] => 1
        )
[2] => Array
        (
            [id] => 31
            [sender_email] => abc@gmail.com
            [to_email] => test@test.com
            [description] => 729237A55E2EDCB80B18F
            [created_at] => 2020-01-27 12:51:34
            [status] => 1
        )

)

$arr2 =
Array
(
    [0] => test1@test.com
    [1] =>  abb@abb.com
    [2] =>  abc@gmail.com
)

我試圖從 $arr2 中找到 $arr1 中也存在的匹配值。

我試過

if(array_intersect($arr2, $arr1)) {
            die('wrong');
        }

但它顯示錯誤,如 -

 Notice: Array to string conversion in

我認為是由於結構不同。 如何做到這一點? 如果我可以在一個數組中獲取所有匹配的值,那將非常有幫助。 列名將始終相同,但我要求不要將其包含在代碼中。

這應該有效:

$matches = []; // array which will contains matches
foreach($arr1 as &$el){ // loop through the elements
   if(array_intersect($arr2,$el)) array_push($matches, $el); //and if there is at least one elements as intersect of the two arrays, add it
}

這個遞歸函數循環遍歷每個深度的所有元素並搜索$needle的元素。 returns an array with all matched values

function array_search_recursive( $needle, $haystack, $strict = false, &$matches = array() )  {
  foreach( $haystack as $value )  {
    if( is_array( $value ) )  {
      array_search_recursive( $needle, $value, $strict, $matches );
    } else {
      if( in_array( $value, $needle, $strict ) ) {
        $matches[] = $value;
      }
    }
  }
  return $matches;
}

// Parameters
// $needle: The array containing the searched values
// $haystack: The array to search in
// $strict: If it is set to true, it will also perform a strict type comparison
// $matches: Is only needed for recursion

用法:

$haystack = array(
  array(
    'id' => 1,
    'email' => 'test@mail.de'
  ),
  array(
    'id' => 2,
    'email' => 'mymail@web.de'
  ),
);

$needle = array( 'mymail@web.de', 1, 'lala@web.de' );

$matches = array_search_recursive( $needle, $haystack );

value of $matchesvalue of $matches

Array
(
    [0] => 1
    [1] => mymail@web.de
)

array_intersect()函數比較兩個(或多個)數組的值,並返回匹配項。

此函數比較兩個或多個數組的值,並返回一個數組,該數組包含 array1 中存在於 array2、array3 等中的條目。

$arr1 = array("alpha","omega","bravo","charlie","delta","foxfrot");
$arr2 = array("alpha","gamma","bravo","x-ray","charlie","delta","halo","eagle","foxfrot");  

$result = array_intersect($arr1, $arr2);
print_r($result);

比較三個數組的值,並返回匹配項:

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");

$result=array_intersect($a1,$a2,$a3);
print_r($result);

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM