1

For example,I want to check if every element in "search_this" array can be finded in "all" array (it means the all array has the same value element).

  $search_this = Array
(
(0) => Array
    (
        (id) => '1',
        (name) => 'a'
    ),

(1) => Array
    (
        (id) => '2',
        (name) => 'b'
    ),

(2) => Array
    (
        (id) => '3',
        (name) => 'c'
    )
);

$all = Array
(
(0) => Array
    (
        (id) => '1',
        (name) => 'a'
    ),

(1) => Array
    (
        (id) => '2',
        (name) => 'd'
    ),

(2) => Array
    (
        (id) => '4',
        (name) => 'c'
    )
);

like this ↑ , only one element can be find in "all" array ,not all. thus the result is false.

is there any function in php which can do this?

chii
  • 2,111
  • 3
  • 17
  • 21

2 Answers2

0
$inArray = array();
foreach ($search_this as $search){
    if(in_array($search, $all)){
        $inArray[]=$search;
    }
}
print_r($inArray);

result of printing

Array ( [0] => Array ( [id] => 1 [name] => a ) )

Zeljka
  • 376
  • 1
  • 10
0

@chii you can directly check with == operator like:

<?php
  $result = $search_this == $all; //return true or false, check only values of the element not data type of element
  $result = $search_this === $all; //return true or false, check both values and data type of the element
lazyCoder
  • 2,544
  • 3
  • 22
  • 41
  • thank you, I used it and it worked well . but I found the == operator also check the array key, accutualy i have the two array with different key (but the same value),so i did someting to let the key be the same and it worked. – chii Mar 29 '17 at 08:15
  • == check both key and value but it does not check the data type of key and value of an array but === also check the data type of key and value – lazyCoder Mar 29 '17 at 11:18