Find common values in multiple arrays with PHP - php

I need to find common values in multiple arrays. Number of arrays may be infinite.
Example (output from print_r)
Array1
(
[0] => 118
[1] => 802
[2] => 800
)
Array2
(
[0] => 765
[1] => 801
)
Array3
(
[0] => 765
[1] => 794
[2] => 793
[3] => 792
[4] => 791
[5] => 799
[6] => 801
[7] => 802
[8] => 800
)
now, I need to find the values that are common on all 3 (or more if available) of them.... how do I do that?
Thanx

array_intersect()
$intersect = array_intersect($array1,$array2,$array3);
If you don't know how many arrays you have, then build up an array of arrays and user call_user_func_array()
$list = array();
$list[] = $array1;
$list[] = $array2;
$list[] = $array3;
$intersect = call_user_func_array('array_intersect',$list);

Related

How to merge mutiple array in a single array

As i searched alot but didn't get perfect solution of merging array in one single array.
These arrays are Dynamic (may be increase in future- would 50+). So for that we have to use count() or for loops to fetch and then merge.
Here's my code which i'm trying to resolve over core level. Please anyone tell me how may receive all values in Single array.
Array(
[0] => Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
)
[1] => Array
(
[0] => 93
[1] => 94
[2] => 95
[3] => 172
[4] => 30
)
[2] => Array
(
[0] => 109
[1] => 81
[2] => 79
[3] => 155 )
)`
My expectation of result is: (which i'm unable to get)
Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
[4] => 93
[5] => 94
[6] => 95
[7] => 172
[8] => 30
[9] => 109
[10] => 81
[11] => 79
[12] => 155
)
Use array_merge with splat operator,
$result = array_merge(...$arr);
array_merge — Merge one or more arrays
Splat operator - Since PHP 5.6, you can use splat operator (...) to create simpler variadic functions (functions that take an undefined number of arguments).
Demo
Output:-
Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
[4] => 93
[5] => 94
[6] => 95
[7] => 172
[8] => 30
[9] => 109
[10] => 81
[11] => 79
[12] => 155
)
using array_merge()
$a[0] = [1,2,3];
$a[1] = [4,5,6];
$a[2] = [7,8,9];
$newArr = [];
$newArr = array_merge($a[0], $a[1], $a[2]);
print_r($newArr);
assuming that your array will grow, you can use foreach like this :
$a[0] = [1,2,3];
$a[1] = [4,5,6];
$a[2] = [7,8,9];
$newArr = [];
foreach($a as $index){
foreach($index as $item){
array_push($newArr, $item);
}
}
As i used this technique. I got my answer in just a simple way. of array_flatten
print_r(array_flatten($overall_arr));
function array_flatten($array) {
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)){
$return = array_merge($return, array_flatten($value));
} else {
$return[$key] = $value;
}
}
return $return;
}

How to remove key from another array that holds values?

let's suppos we have the following two arrays
Let's suppose this is called $array1
Array
(
[0] => Array
(
[Name] => Jack
[Height] => 190
[Shoe Size] => 40
)
[1] => Array
(
[Name] => Rose
[Height] => 160
[Shoe Size] => 52
)
)
Suppose this is called $array2
Array
(
[0] => Name
[1] => Shoe Size
)
Now, what I need to do, is to keep the keys in $array1 which are found in $array2 as values, so the output I'm expecting is something like this
Array
(
[0] => Array
(
[Name] => Jack
[Shoe Size] => 40
)
[1] => Array
(
[Name] => Rose
[Shoe Size] => 52
)
)
I tried array_intersect and array_intersect_key but they're both failing. does anyone have any idea how to do this?
What you need is array_intersect_key with array_flip
$array3 = array_flip($array2);
foreach($array1 as &$a) {
$a = array_intersect_key($a, $array3);
}

Merge two 2d arrays on shared value from different keys

I two arrays set out like this.
First array
Array
(
[0] => Array
(
[manufacture] => Moto
[name] => ZSX 125
[code] => ZS125-48A
[cc] => 125
[bike_type] => 3 Motorcycle
[title] => Mto ZSX 125
[about] => The ZSX
)
[1] => Array
(
[manufacture] => Moto
[name] => LSM 125
[code] => STR125YB
[cc] => 125
[bike_type] => 6 Endurancemotor
[title] => Moto
[about] => Moto
)
)
Second array
Array
(
[0] => Array
(
[id] => 183
[model] => ZS125-48A
[engine_brand] => 158FMI-B
[engine_type] => Single Cylinder
)
[1] => Array
(
[id] => 172
[model] => STR125YB
[engine_brand] => K154FMI
[engine_type] => Single Cylinder
)
)
As you can see 'code' from the first array is the same as 'model' from the second and there will always be a match.
This is the array i would like to have.
Array
(
[0] => Array
(
[id] => 183
[model] => ZS125-48A
[engine_brand] => 158FMI-B
[engine_type] => Single Cylinder
[manufacture] => Moto
[name] => ZSX 125
[code] => ZS125-48A
[cc] => 125
[bike_type] => 3 Motorcycle
[title] => Moto ZSX 125
[about] => The ZSX
)
[1] => Array
(
[id] => 172
[model] => STR125YB
[engine_brand] => K154FMI
[engine_type] => Single Cylinder
[manufacture] => Moto
[name] => LSM 125
[code] => STR125YB
[cc] => 125
[bike_type] => 6 Endurancemotor
[title] => Moto
[about] => Moto
)
)
Is this possible? i have tried array_merge and array_merge_recursive, but it only appends the second array onto the end of the first, it doesnt merge it on the keys.
Deceze's solution is perfect if the two arrays are synchronized (i.e. items with matching code and model are at the same index in both arrays). If that is not the case you will need more than one line of code.
The arrays need to have string keys in order to be merged with array_merge. In this case you want to rekey the first one based on code and the second based on model. After rekeying you can merge them with array_merge_recursive.
Rekeying is easy in PHP 5.5+:
// array_column will conveniently rekey when the second argument is null
$array1 = array_column($array1, null, 'code');
$array2 = array_column($array2, null, 'model');
A bit more complicated in earlier versions:
// array_map is a more complicated way to extract a column from an array
// and array_combine will do the rekeying
$array1 = array_combine(
array_map(function($i) { return $i['code']; }, $array1),
$array1);
$array2 = array_combine(
array_map(function($i) { return $i['model']; }, $array2),
$array2);
In both cases the final step would be the same:
$result = array_merge_recursive($array1, $array2);
Looks to me like this should do it:
$array3 = array_map('array_merge', $array1, $array2);
This feeds $array1[0] and $array2[0] to array_merge, then $array1[1] and $array2[1] etc.
And, if you prefer "simpler" code... :-)
$result = array();
foreach ($one as $a) {
foreach ($two as $b) {
if ($a->code == $b->model) {
array_push($result, array_merge($a, $b));
break;
}
}
}
print_r($result);

merge array in single array

I have used the following code to get array and merge them.
On the print_r(arr1) I get following array. In $arr I am trying to merge array but when i print_($arr) at the end of for-each I get same array.
Am I doing wrong array merge?
How can i combine or merge it?
foreach($q1->result_array() as $row4)
{
$arr1 = $q1->result_array();
echo"<pre>";
print_r($arr1);
echo"</pre>";
$arr = array_merge($arr, $arr1);
echo "<br/>";
$id = $row4['id'];
$parent_id = $row4['parent_id'];
if(!empty($arr1))
{
$this->showreply($id);
}
}
print_r($arr);
Array which i get on print_r($arr1):
Array
(
[0] => Array
(
[id] => 69
[reply] => First reply to Reply
[parent_id] => 68
[postid] => 0
[us_id] => 41
[added_by] => Shailesh
[photo] => 9.jpg
[added_on] => 2013-04-01 16:06:13
)
)
Array
(
[0] => Array
(
[id] => 70
[reply] => Reply to Nested Reply
[parent_id] => 69
[postid] => 0
[us_id] => 41
[added_by] => Shailesh
[photo] => 9.jpg
[added_on] => 2013-04-01 16:07:24
)
)
Array
(
[0] => Array
(
[id] => 52
[reply] => Reply on demand
[parent_id] => 70
[postid] => 0
[us_id] => 50
[added_by] => swapnil
[photo] =>
[added_on] => 2013-03-29 16:27:57
)
)
$arr = array_merge($arr, $arr1);
This $arr variable was never initialized, so nothing plus $arr1 equals to $arr1.
Also why is this code inside a foreach?
try this...i think this is what you are looking for
foreach($q1->result_array() as $row4)
{
$arr1 = $q1->result_array();
$arr[]=$row4;
}
print_r($arr)

Array sorting question

So I have an array such as this one:
Array
(
[-1] => Array
(
[3] => 3
[1] => 1
[6] => 6
[7] => 7
[5] => 5
)
)
It also contains some other keys that should not be modified.
I'd like to the numbers which are in a second array to come first (in the order of that second array), and then will be the numbers that don't exist in the second array, if any.
So for that matter, the second array would be:
Array
(
[0] => 6
[1] => 5
[2] => 3
)
And the final array should be as follows (please remember, there are some more keys inside of that array that should stay as they are):
Array
(
[-1] => Array
(
[6] => 6
[5] => 5
[3] => 3
[1] => 1
[7] => 7
)
)
Any ideas how that can be done?
Thanks!
It's not and shouldn't be termed as sorting but may be this code snippet may help you do what you want to:
$a1 = Array ( [-1] => Array ( [3] => 3 [1] => 1 [6] => 6 [7] => 7 [5] => 5 ) );
$a2 = Array ( [0] => 6 [1] => 5 [2] => 3 );
$sorted = getSortedArray($a1[-1] , $array2);
function getSortedArray($array1 , $array2){
$temp = Array();
$count = 0;
$totalKeys = sizeof($array2);
for($i=0;$i<sizeof($array2);$i++){
$temp[i] = $array1[$array2[i]];
unset($array1[$array2[i]]);
}
while($count!=sizeof($array1))
$temp[$totalKeys++] = $array1[$count++];
return $temp;
}
I believe the function you're looking for is called array_multisort().
array_multisort() can be used to sort
several arrays at once, or a
multi-dimensional array by one or more
dimensions.

Categories