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

8
  • 1
    define "large $arr2" Commented May 7, 2017 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 Commented May 7, 2017 at 19:14
  • Can you use a different structure for your large array? Commented May 7, 2017 at 19:15
  • isset() is faster than in_array(). See this answer: stackoverflow.com/a/13483548/1123556 Commented May 7, 2017 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? Commented May 7, 2017 at 19:18

2 Answers 2

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.

1
  • Actually good idea to encode each object in string and then use isset. But I will need to rebuild my code much(((( Commented May 7, 2017 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";
 }
1
  • 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 Commented May 10, 2017 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.