0

I try to search array in array, but function doesn't work:

$a = [];
$b = [];
array_push($a,"1");
array_push($a,"2");
array_push($b,"2");
dd(array_search($b,$a));

How I can search array values in other array?

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • array_search($b[0],$a)); – Sfili_81 Oct 11 '19 at 12:18
  • What is your purpose of the search? – Vantiya Oct 11 '19 at 12:21
  • I would use what was advised above but i want to make seach array values in array, not string in array – Ykio Izumi Oct 11 '19 at 12:22
  • You can't search it like that using buit in php functions. You have to write your own code for that. but if you want to get array from $a that match from $b you can do it. – Vantiya Oct 11 '19 at 12:25
  • https://www.php.net/manual/ru/function.array-search.php At this link Indicated that that needle can as an array and a string. Because i wrote this question here – Ykio Izumi Oct 11 '19 at 12:27
  • 2
    Possible duplicate of [Checking to see if one array's elements are in another array in PHP](https://stackoverflow.com/questions/523796/checking-to-see-if-one-arrays-elements-are-in-another-array-in-php) – sepehr Oct 11 '19 at 13:09

1 Answers1

0

You can't simply get that searched by built in function but if you intent to get array that match from $b into $a you can do it by using

print_r( array_intersect($a, $b) );

Input

$a = [];
        $b = [];
        array_push($a,"1");
        array_push($a,"2");
        array_push($a, "3");
        array_push($b,"2");
        array_push($b, '1');
        print_r( array_intersect($a, $b) );

        echo !empty(array_intersect($a, $b));

output

Array
(
    [0] => 1
    [1] => 2
)
Vantiya
  • 612
  • 1
  • 4
  • 11