1

I have array1 like this:

Array
(
[0] => 123
[1] => 456
[2] => 789
)

And array 2 like this

 Array
 (

 [0] => Array
    (
        [0] => some text
        [1] => 888
        [2] => some
        [3] => text
    )
  [1] => Array
    (
        [0] => some text
        [1] => 123
        [2] => some
        [3] => text
    )
  [2] => Array
    (
        [0] => some text
        [1] => 999
        [2] => some
        [3] => text
    )   
   [3] => Array
    (
        [0] => some text
        [1] => Array
            (
                [1] => 456
                [2] => 789
            )
        [2] => some
        [3] => text
    ) 
   [4] => Array
    (
        [0] => some text
        [1] => 123
        [2] => some
        [3] => text
    )   
  )

I am checking only 1. column of second array and finding values that match values from first array. This is my code:

 $test=array();

 $xcol = array_column($array2, 1);
 foreach( $array1 as $key => $value ) {
 if( ($foundKey = array_keys($xcol, $value)) !== false ) {
 $rrt=$foundKey;
 foreach($rrt as $rte){
  $test[]=$array2[$rte];
}
}
}

echo "<pre>";
print_r($test);
echo "</pre>";

It is working and giving me proper results but it does not check for all levels. Can anybody please point me what am I doing wrong? My output is:

Array
 (

  [0] => Array
    (
        [0] => some text
        [1] => 123
        [2] => some
        [3] => text
    )
   [1] => Array
    (
        [0] => some text
        [1] => 123
        [2] => some
        [3] => text
    )   
   )

And desired output is:

Array
 (

  [0] => Array
    (
        [0] => some text
        [1] => 123
        [2] => some
        [3] => text
    )
   [1] => Array
    (
        [0] => some text
        [1] => Array
            (
                [1] => 456
                [2] => 789
            )
        [2] => some
        [3] => text
    ) 
   [2] => Array
    (
        [0] => some text
        [1] => 123
        [2] => some
        [3] => text
    )   
   )
5
  • you could use the recursiveArrayIterator Commented Dec 28, 2016 at 14:20
  • php.net/manual/en/function.is-array.php Commented Dec 28, 2016 at 14:20
  • @mister martin, can you give me some hint how would I use than array_key search to find value and still output everything else sub array contain? I have never used recursiveArrayIterator before so I am a bit confused Commented Dec 28, 2016 at 14:30
  • so your problem is you don't get 456 and 789? Commented Dec 28, 2016 at 14:34
  • Yes, I have edited my question Commented Dec 28, 2016 at 14:35

2 Answers 2

1

Solution:

create a loop which loop your $array2 which holds datas you wanted to get whos values match in first array $array1

foreach($array2 as $data) {

Inside loop create another loop which loop your indexes

    foreach($data as  $value) {

But before than create a condition if your index value is array loop it and check it it's index value is match in any indexes from $array1 use php function in_array for that

        if (gettype($value) == "array") {
            foreach($value as $val) {
                if (in_array($val, $array1) ) {
                    $result[] = $data;

Then if you find it just stop the loop using break to avoid duplication

                    break;
                }
            }

Else you just directly use in_array

        } else if (in_array($value, $array1)) {
            $result[] = $data;
        }
    }
}

Just grab the code here in Demo

Help it helps just mark it answer if you are satisfied to it

Sign up to request clarification or add additional context in comments.

Comments

1

Lets make a recursive method for this. What is recursion you ask? Well it is simply a method that calls it self.

<?php
$array1 = array(123,456,789);
$array2 = array(
    array(
         "some text"
        , 888
        , "some"
        , "text"
    ),
    array(
        "some text"
        ,123
        ,"some"
        ,"text"
    ),
    array(
        "some text"
        ,999
        ,"some"
        ,"text"
    ),  
    array(
        "some text"
        ,array(456,789)
        ,"some"
        ,"text"
    ),
    array(
        "another text"
        ,123
        ,"some"
        ,"text"
    )   
);
$final = array();
foreach($array1 as $needle){
    foreach($array2 as $haystack){
        if(find($needle,$haystack)){
            $final[] = $haystack;
        }
    }
}

print_r($final);

function find($needle, $haystack){
    $result = false;
    foreach ($haystack as $value) {
        if(is_array($value)){
            $result = find($needle, $value);
        } else {
            if($needle == $value){
                $result = true;
            } 
        }
    }
    return $result;
}

5 Comments

@codexy I just want to make sure you only want the key of the top level array that has a match in it?
Yes, match would be 3 (in this specific case), and that is all I need, so than I can retrieve values from it.
This is what I have now, but I am missing 3.column in output from array2 which is match also
@codexy there is still an issue where the results will have 4 duplicating the array for 456 and 789. You could just store keys of arrays and clean up dups in that.

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.