0

Have a problem with checking array inside of large array of objects. For example I have

$arr1 = array("a"=>12,"b"=5,"c"=>16);

and I need to check if this array exists inside array like this:

 $arr2 = array( array( "a"=>12,"b"=5,"c"=>16), 
                array("d"=>1,"g"=5,"c"=>16), 
                array("a"=>12,"c"=5,"e"=>3) );

I have tried in_array(), and it works, but takes a lot of time if I have large $arr2. Thank you in advance. P.S. works with PHP 5.6

Martin
  • 22,212
  • 11
  • 70
  • 132
D.Kurapin
  • 103
  • 11
  • 1
    define "large $arr2" – Martin May 07 '17 at 19:10
  • for example $arr2 have at least 7500 objects and each object contains 15 key-value pairs. So, I need to check: do one of objects of $arr2 exists all pairs of $arr1 – D.Kurapin May 07 '17 at 19:14
  • Can you use a different structure for your large array? – trincot May 07 '17 at 19:15
  • ```isset()``` is faster than ```in_array()```. See this answer: http://stackoverflow.com/a/13483548/1123556 – Etienne Martin May 07 '17 at 19:16
  • 7500 elements aren' t a lot to me. How much your "lot of time" is? Is there any order in the array you can leverage to make the search faster? – Eineki May 07 '17 at 19:18
  • Can you use associative array with keys somehow? Some other answers to some performance related issues indicate that ```in_array()```, ```array_search()``` takes almost same time. – arshovon May 07 '17 at 19:19
  • trincot. It is difficult to start from scratch – D.Kurapin May 07 '17 at 19:27
  • Guys I was not quite clear in my question. I have another $someBigArr, which have same structure with $arr2 . So that I make: ```foreach($someBigArr as $arr1) { if(!in_array($arr1,$arr2)) {echo "not exists"}} ``` – D.Kurapin May 07 '17 at 19:28

2 Answers2

1

Instead of your current $arr structure, you'd better have an associative array, where the keys uniquely identify the value.

If you need to search that array several times, you might still save time if you would spend some time to create such associative array from your existing array:

$arr2 = array( 
    array( "a"=>12,"b"=>5,"c"=>16), 
    array("d"=>1,"g"=>5,"c"=>16), 
    array("a"=>12,"c"=>5,"e"=>3) 
);

// use key/value pairs, with as key the JSON-encoding of the value
// Note: this will take some time for very large arrays:
foreach ($arr2 as $sub) {
    ksort($sub); // need to sort the keys to make sure we can find a match when needed
    $hash[json_encode($sub)] = $sub;
}

// function to see whether the value is in the array: this will be fast!
function inHash($hash, $sub) {
    ksort($sub);
    return isset($hash[json_encode($sub)]);
}

// test
if (inHash($hash, array("d"=>1,"g"=>5,"c"=>16))) {
    echo "found";
} else {
    echo "not found";
}

Of course, if you could create the original immediately with such keys, then you don't have the overhead of creating it afterwards.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Actually good idea to encode each object in string and then use isset. But I will need to rebuild my code much(((( – D.Kurapin May 07 '17 at 20:10
1

@D.Kurapin simply try with == or === operator(according to you) with foreach() like below:

<?php
$arr1 = array("a"=>12,"b"=>5,"c"=>16);
 $arr2 = array( array( "a"=>12,"b"=>5,"c"=>16),
                array("d"=>1,"g"=>5,"c"=>16),
                array("a"=>12,"c"=>5,"e"=>3) );
 $isExist = false;
 foreach ($arr2 as $key => $value) {
    if($value === $arr1){
        $isExist = true;
        break;
    }
 }
 if($isExist){
    echo "yes found in the array";
 }
 else{
    echo "sorry did not find in the array";
 }
lazyCoder
  • 2,544
  • 3
  • 22
  • 41
  • i think what ever you use for your case the comparison time definitely needed, because each array must be checked to know the existence of the array – lazyCoder May 10 '17 at 05:40