I have three array from dynamic data in php for example :
array1 = array('10-11-2015'=>23.6,
'25-11-2015'=>48.6,
'12-11-2015'=>14.52);
array2 = array('10-11-2015'=>8.7,
'22-10-2015'=>86.6,
'12-11-2015'=>78.5);
array3 = array('10-11-2015'=>5.8,
'19-09-2015'=>3.6,
'12-11-2015'=>96.4);
I just need common keys from this three array
newarray = array('10-11-2015','12-11-2015');
I use array_intersect_key but it is for only two array.
How I can get it form three or more array ?
Something like this?
array_keys(array_intersect_key($array1, $array2, $array3));
Related
I have two arrays :
$array1 = [460,471];
$array2 = [193,42,471];
I want to take the value only in $array1 if there is no same value in $array2, if there is a same value in $array2 filter it out.
Expected output if no same value available in $array2:
$output = [460, 471]
Expected output is there is same value in $array2:
$output = [460]
Try array_diff() function
$array1 = [460,471];
$array2 = [193,42,471];
$filtered = array_diff($array1,$array2);
print_r($filtered);
You can achieve the same with the help of Hashing pretty easily.
First, try to map the values of array2 into a Hash.
For example, each element of $array2 = [193,42,471]; will have a value marking their presence as true.
Now, for each element is $array1, check if it has a value in the Hash. If it has one, then skip it.
So after the check, you will get all the values that are not present in the second array.
I am trying to compare two non-associative arrays to create a new array with the matches.
This is what I have though:
//This array has several entries
$a_firstarray = Array();
//This array has less entries than the first array
$a_secondarray = Array();
//This array should contain the matches of the first array and the second array in no particular order
$a_mergedarray
for($i=0;$i <=count($a_firstarray);$i++){
for($a=0;$a <=count ($a_secondarray);$a++){
if($a_firstarray[$i] == $a_secondarray[$a]){
$a_mergedarray[] = $a_activecategory[$i];
}
}
}
It doesn't work for some reason. I am also sure that PHP has some sort of function that does this. Any ideas? thanks in advance.
This is known as the "intersection" of two arrays. PHP provides array_intersect.
use array_intersect.
$result = array_intersect($array1, $array2);
Are you looking for array_intersect()?
http://php.net/manual/en/function.array-intersect.php
If I have two arrays and I want to "combine" them into one array whilst maintaining each individual array and leaving the keys as they are as they are always duplicated each iteration, can I do this?:
$array1 = array('0'=>'Bob', '1'=>'Tom', '2'=>'John');
$array2 = array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan');
If I use array_merge:
$new_array = array_merge($array1, $array2);
I get:
array('0'=>'Bob','1'=>'Tom','2'=>'John','3'=>'Michelle','4'=>'Joan','5'=>'Susan')
whereas I want to get something like:
array(array('0'=>'Bob', '1'=>'Tom', '2'=>'John'),array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan'))
Create a new array and add the other arrays to that one like this:
$arr = array($array1, $array2);
$ new_array = array ($ array, $ array2);
Is there a way to use descriptors in PHP to do the following sort:
Array 1:
$array1 = ["blah", "quack", "yot"];
Array 2
$array2 = [1,9,6]; Essentially, I'm trying to describe blah with 1, quack with 9, and yot with 6. Basically, there are a total of 1 blahs, 9 quacks, and 6 yots. When I sort the arrays, I want to produce the following arrays:
Array 1 Sorted:
$array1 = ["quack","yot","blah"];
Array 2 Sorted:
$array2 = [9,6,1];
array_multisort($array2, SORT_DESC, $array1);
http://ideone.com/4Afyu
http://php.net/array_multisort
You can sort arrays with usort.
Well, you can restructure your data into an associative array.
$var = array ('blah'=>1, 'quack'=>9, 'yot'=>6);
So that your data is in one array. Then you can use asort or any php sorting function to manipulate the array.
I have 2 arrays , $array1 & $array2. I want to create new array such that its key value is values of array1 and values are values of array2. Is it possible..
I used following approach to create this new array named $inputs. Is this correct?
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_fill("$array1",count($array1),$array2);
print_r($inputs);
If I read your question correctly, you can use array_combine() PHP Manual
$array1 = array("3","4","6");
$array2 = array("a","b","c");
$inputs = array_combine($array2, $array1); # keys, values
Use array_combine.
http://www.php.net/manual/de/function.array-combine.php
$inputs = array_combine($array1, $array2);
<?php
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_combine($array1,$array2);
print_r($inputs);
?>
http://codepad.org/6fTXCZa5
use php array_combine function
http://php.net/manual/en/function.array-combine.php