2

I've an array's

first one:

    [0] => 0289 [1] => 0146 [2] => 5519 [3] => 5308 [4] => 5503 [5] => 5357

second one(associative):

    [78941] => 5308 [15749] => 5519 [1469156] => 5308 [78971413] => 5357 [418979] => 0289

Need to find keys in second one by first one value. One by one. I did some loop:

for($i=0;$i<=5;$i++){
$keys=array_search($first_array[$i],$second_array);   
file_put_contents('check.txt',$keys,FILE_APPEND);
}    

But get nothing. What I'am doing wrong?

Addition

The second array is more large than I show here, approximately 10000 values.

I must insert 5 values per file and these values must be uniq, to avoid overlap.

It will be looks like :

  $t=0;
 for($i=0;$i<=count($second_array);$i++){

$keys=array_search($first_array[$t],$second_array);   
file_put_contents('check.txt',$keys,FILE_APPEND);
 $t++
 if ($t==5){$t=0}

}

Hope it would help.

5
  • Your code has no errors as it is. Does the program have permissions to create the file or write in it? What does "getting nothing" mean? Commented Oct 14, 2013 at 7:54
  • Thank for answer. File is creating. But there is really nothing in it) I'll check the solutions provided. And reply soon. Commented Oct 14, 2013 at 8:28
  • Try to isolate the problem into an SSCCE. If I copy your code as it is into a .php file and run it, a file called check.txt is created and it contains a sequence of numbers that correspond to the indices that array_search finds, so the real problem is probably somewhere else. For example, maybe the user that you use to run PHP does not have write permission to the file. Commented Oct 14, 2013 at 10:26
  • Thanx for your attention. But I see just empty array "Array()" in file. Commented Oct 14, 2013 at 10:32
  • That cannot happen with the code you have shown: array_search returns a single value, not an array, so single values are written to the file. If you really are using this code and find Array() in the file, something else is writing over the file afterwards. Commented Oct 14, 2013 at 10:34

4 Answers 4

4

If you need only keys, so just filter them:

$keys = array_intersect($first, array_keys($second));

However, if you want to get both values and keys, then it'll be like:

$keysAndValues = array_intersect_key($second, array_flip($first));
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answer. But I have some different task. I don't need all values be added to array(or written to file) at once. Need to find and write to file(or add to array) one by one. I mean first "0289" -> write to file and delete from existing one, then "0146" and etc. If I had several values like in example "5308", need to find first one, write to file, delete from existing and then loop must go again in same sequence.
Note: second array will be more large than in example, but first one will be as it seen here.
@Sam then provide your logic and samples in question
Still can't get - why finding full intersection & writing it then in one piece to your file isn't a solution?
If sequence in $keys will be like: 1,2,3,4,5,1,2,3,4,5 (by $first_array value). Then it will solution for me :)
1

You can do it much simple way using foreach loop

<?php
      $i = 0;
      foreach($array2 as $key => $value):
         if($array1[$i] == $value) {
           //$key is the required key, manage your stuffs here.
         }
         $i++;
      endforeach;
 ?>

1 Comment

This is more efficient than my suggestion. Use this.
0
foreach($first_array as $first_key => $first_value){
    foreach($second_array as $second_key => $second_value){
        if($first_value == $second_value){
            file_put_contents($file_name, $second_key . "\n", FILE_APPEND);
        }
    }
}

1 Comment

Agreed. @nurakantech has a better solution.
0

array_search() returns the key if it finds the value, and returns false if not found, so if

you want keys, this code is what you want:

$one=array("0"=>"0146","1"=>"5519","2"=>"5308","3"=>"5503","4"=>"5357");
$two=array("78941"=>"5308","15749"=>"5519","1469156"=>"5308","78971413"=>"5357","418979"=>"5357");
$result=array();

for($i=0;$i<=5;$i++){
if(array_search($one[$i],$two))
$result[]=array_search($one[$i],$two);
}
print_r($result);//OR file_put_contents('check.txt',$result,FILE_APPEND)

Comments

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.