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.
第二个数组比这里显示的大,大约10000个值。
I must insert 5 values per file and these values must be uniq, to avoid overlap.
我必须为每个文件插入5个值,这些值必须是uniq,以避免重叠。
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.
希望它会有所帮助。
4 个解决方案
#1
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));
#2
1
You can do it much simple way using foreach loop
使用foreach循环,您可以用非常简单的方法来完成
<?php
$i = 0;
foreach($array2 as $key => $value):
if($array1[$i] == $value) {
//$key is the required key, manage your stuffs here.
}
$i++;
endforeach;
?>
#3
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);
}
}
}
#4
0
array_search()
returns the key if it finds the value, and returns false if not found, so if
如果找到值,array_search()返回键,如果没有找到,返回false,所以如果。
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)
#1
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));
#2
1
You can do it much simple way using foreach loop
使用foreach循环,您可以用非常简单的方法来完成
<?php
$i = 0;
foreach($array2 as $key => $value):
if($array1[$i] == $value) {
//$key is the required key, manage your stuffs here.
}
$i++;
endforeach;
?>
#3
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);
}
}
}
#4
0
array_search()
returns the key if it finds the value, and returns false if not found, so if
如果找到值,array_search()返回键,如果没有找到,返回false,所以如果。
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)