PHP: Intersect two arrays and get the values of both - php

So I am a bit stuck here, I can do it with foreach loop but I want to find a cleverer way of doing it.
Update: There was something that I've missed in the question. The arrays may come in random order and in different length thus having different keys. Examples below updated.
Here is the case:
Array1
array (
slug1 => England,
slug2 => France,
slug3 => Italy,
slug4 => Germany,
)
Array2
array (
slug2 => 215,
slug1 => 168,
slug4 => 55,
slug5 => 149,
slug3 => 40,
slug6 => 137,
)
I want to intersect those arrays and build new one which has the following elements:
array (
168 => England,
215 => France,
40 => Italy,
55 => Germany,
)
Note: elements are not ordered though that could be achieved easily.

Answer to Original Question
You can use array_combine it creates an array by using one array for keys and another for its values
$array1 = array(
"slug1" => "England",
"slug2" => "France",
"slug3" => "Italy",
"slug4" => "Germany");
$array2 = array(
"slug1" => "168",
"slug2" => "215",
"slug3" => "40",
"slug4" => "55");
$final = array_combine($array2, $array1);
echo "<pre>";
print_r($final);
Output
Array
(
[168] => England
[215] => France
[40] => Italy
[55] => Germany
)
See Live Demo
Answer to Updated Question
Update: There was something that I've missed in the question. The arrays may come in random order and in different length thus having different keys. Examples below updated.
$array1 = array(
"slug1" => "England",
"slug2" => "France",
"slug3" => "Italy",
"slug4" => "Germany");
$array2 = array (
"slug2" => 215,
"slug1" => 168,
"slug4" => 55,
"slug5" => 149,
"slug3" => 40,
"slug6" => 137);
$final = customCombine($array2, $array1);
echo "<pre>";
print_r($final);
Output
Array
(
[215] => France
[168] => England
[55] => Germany
[40] => Italy
)
Function Used
function customCombine($keys, $arr) {
$t = array();
foreach ( $keys as $k => $val ) {
isset($arr[$k]) and $t[$val] = $arr[$k];
}
return $t;
}

Baba's answer should work fine for you, but here's an interesting way to deal with different key orders between both arrays and/or different sizes.
// get values from $array2 in the order in which they appear in $array1
// whereby the array keys are used to match them
$keys = array_intersect_key($array2, $array1);
// create a new array with the keys found in the previous step and
// another array_intersect_key() to only select the matching items
// from $array1
array_combine($keys, array_intersect_key($array1, $keys));
It also makes sure that array_combine() works with same sized arrays; the size of $array2 is the output size.

To expand Jack's answer, as it might combine the arrays in the order they're constructed in and not the order the keys are matching:
Array
(
[215] => England
[168] => France
[55] => Italy
[40] => Germany
)
Some intermediate data juggling can sort it out (and it works no matter which array is shorter):
$array1 = array(
'slug1' => 'England',
'slug2' => 'France',
'slug3' => 'Italy',
'slug4' => 'Germany'
);
$array2 = array (
'slug2' => 215,
'slug1' => 168,
'slug4' => 55,
'slug5' => 149,
'slug3' => 40,
'slug6' => 137
);
$keys = array_intersect_key($array2, $array1);
ksort($keys);
$intersect = array_intersect_key($array1, $keys);
ksort($intersect);
$final = array_combine($keys, $intersect);
print_r($final);
Outputs
Array
(
[168] => England
[215] => France
[40] => Italy
[55] => Germany
)

use array_combine()
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
?>
http://in1.php.net/array-combine

I found the best solution for me:
http://eval.in/3578
<?php
$array1 = array("slug1" => "England","slug2" => "France","slug3" => "Italy","slug4" => "Germany");
$array2 = array("slug1" => "168","slug2" => "215","slug3" => "40","slug4" => "55", "slug5" => "178");
ksort($array1);
ksort($array2);
$test1 = array_intersect_key($array1, $array2);
$test2 = array_intersect_key($array2, $array1);
$final = array_combine($test2, $test1);
print_r($final);
?>

Related

How to split multidimensional array into multiple array dynamically

I got multidimensional arrays i want it to break into multi array in one variable
Array
(
[name] => Array
(
[0] => John Davis
[1] => Marie J
)
[work] => Array
(
[0] => employee
[1] => none
)
[address] => Array
(
[0] => street 1
[1] => street 2
)
)
I want output to :
$array1 = array("name" => "John Davis", "work" => "employee", "address" => "street 1");
$array2 = array("name" => "Marie J", "work" => "none", "address" => "street 2");
and how to generate dynamically if values keys more than 2
thanks alot
Please try below code get output:
$newArray = [];
foreach ($outerArray as $outerkey => $outerArr) {
foreach ($outerArr as $key => $innerArr) {
$newArray[$key][$outerkey] = $innerArr;
}
}
print_r($newArray);
Functional solution
// make array of keys
$keys = array_keys($arr);
// combine it with data sets
$res = array_map(function (...$x) use($keys)
{ return array_combine($keys, $x); },
...array_values($arr));
demo

Matching keys from one array to another and creating a new array with their values?

Hi I have two arrays the first one looks like this:
array(
00000 => array(
0 => 123,
1 => 456,
2 => 789,
)
11111 => array(
0 => 987,
1 => 654,
2 => 321,
)
My second array looks like this:
array(
00000 => 'apples',
11111 => 'bananas',
)
What am trying to do is match the keys from the 1st array with the keys in array 2nd then if they match take the values of key pair value array of the first array and make them keys for a brand new array and the values of the second array make them values for the brand new array. Something like this:
array(
123 => 'apples',
456 => 'apples',
789 => 'apples',
987 => 'bananas',
654 => 'bananas',
321 => 'bananas',
)
Any help thanks!
so im assuming u have 2 arrays (and you fixed second array to have unique keys)
$array3 = array (); //for the result
foreach ($array1 as $seg)
{
foreach ($seg as $key)
{
$array3[$key] = $array2[$seg];
}
}
Try this
$array = array(
"00000" => array(
0 => 123,
1 => 456,
2 => 789,
),
"11111" => array(
0 => 987,
1 => 654,
2 => 321,
)
);
$arr = array(
"00000" => 'apples',
"11111" => 'bananas',
);
$array3 = array();
foreach ($array as $keyas => $segs)
{
foreach ($segs as $key)
{
$array3[$key] = $arr[$keyas];
}
}
echo "<pre>";
print_r($array3);
unset($array3);
Output :
Array
(
[123] => apples
[456] => apples
[789] => apples
[987] => bananas
[654] => bananas
[321] => bananas
)
Hi actually i did some research on php.net found a solution:
$array3 = array_keys($array1);
$array4 = array_keys($array2);
$array5 = array_intersect($array3, $array4)
$array6 = array();
foreach($array5 as $id) {
foreach($array1[$id] as $userId) {
$array6[$userId] = $array2[$id]
}
}
Probably made more arrays than needed but this works and matches keys of both arrays before assigning values to new array.
Thanks to all that answered and helped!

Merging multiple PHP arrays

My problem is this: I'm creating an Array in order to store these 2 types of 'refinement'. However, what is happening is as the information is collected from the database, each 'refinement' is assigned to it's own specific entry when the arrays are created within the while loop.
while($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
etc...
}
So for instance, the 1st array would be a reference to 'Die Hard' and the 2nd, 'Breaking Bad' and the 3rd, 'Greys Anatomy'. What i'm trying to achieve is to merge them into 1 single array.
Array
(
[genreType] => Action
[mediaType] => Film
)
Array
(
[genreType] => Action
[mediaType] => TV
)
Array
(
[genreType] => Drama
[mediaType] => TV
)
Thanks for any help.
Try looking at this, http://php.net/manual/en/function.array-merge.php
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
OUTPUT
Array (
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Why can't you just use array_merge? from the php docs http://php.net/manual/en/function.array-merge.php
while{$row...
{
$movies[] = $row;
//OR
$tmp['genreType'] = $row['genre'];
//and the like.....
$movies[] = $tmp;
}
Resulting in
$movies = array(
array(
"title"=>"Die Hard",
"genreType"=>"Action",
"mediaType"=>"Film"
),
array(
"title"=>"some other movie",
"genreType"=>"Comedy",
"mediaType"=>"TV"
)
);
like this?

How do I use array_unique on an array of arrays?

I have an array
Array(
[0] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
[1] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
[2] => Array
(
[0] => 33
[user_id] => 33
[1] => 8
[frame_id] => 8
)
[3] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
[4] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
)
As you can see key 0 is the same as 1, 3 and 4. And key 2 is different from them all.
When running the array_unique function on them, the only left is
Array (
[0] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
)
Any ideas why array_unique isn't working as expected?
It's because array_unique compares items using a string comparison. From the docs:
Note: Two elements are considered
equal if and only if (string) $elem1
=== (string) $elem2. In words: when the string representation is the same.
The first element will be used.
The string representation of an array is simply the word Array, no matter what its contents are.
You can do what you want to do by using the following:
$arr = array(
array('user_id' => 33, 'frame_id' => 3),
array('user_id' => 33, 'frame_id' => 3),
array('user_id' => 33, 'frame_id' => 8)
);
$arr = array_intersect_key($arr, array_unique(array_map('serialize', $arr)));
//result:
array
0 =>
array
'user_id' => int 33
'user' => int 3
2 =>
array
'user_id' => int 33
'user' => int 8
Here's how it works:
Each array item is serialized. This
will be unique based on the array's
contents.
The results of this are run through array_unique,
so only arrays with unique
signatures are left.
array_intersect_key will take
the keys of the unique items from
the map/unique function (since the source array's keys are preserved) and pull
them out of your original source
array.
Here's an improved version of #ryeguy's answer:
<?php
$arr = array(
array('user_id' => 33, 'tmp_id' => 3),
array('user_id' => 33, 'tmp_id' => 4),
array('user_id' => 33, 'tmp_id' => 5)
);
# $arr = array_intersect_key($arr, array_unique(array_map('serialize', $arr)));
$arr = array_intersect_key($arr, array_unique(array_map(function ($el) {
return $el['user_id'];
}, $arr)));
//result:
array
0 =>
array
'user_id' => int 33
'tmp_id' => int 3
First, it doesn't do unneeded serialization. Second, sometimes attributes may be different even so id is the same.
The trick here is that array_unique() preserves the keys:
$ php -r 'var_dump(array_unique([1, 2, 2, 3]));'
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[3]=>
int(3)
}
This let's array_intersect_key() leave the desired elements.
I've run into it with Google Places API. I was combining results of several requests with different type of objects (think tags). But I got duplicates, since an object may be put into several categories (types). And the method with serialize didn't work, since the attrs were different, namely, photo_reference and reference. Probably these are like temporary ids.
array_unique() only supports multi-dimensional arrays in PHP 5.2.9 and higher.
Instead, you can create a hash of the array and check it for unique-ness.
$hashes = array();
foreach($array as $val) {
$hashes[md5(serialize($val))] = $val;
}
array_unique($hashes);
array_unique deosn't work recursive, so it just thinks "this are all Arrays, let's kill all but one... here we go!"
Quick Answer (TL;DR)
Distinct values may be extracted from PHP Array of AssociativeArrays using foreach
This is a simplistic approach
Detailed Answer
Context
PHP 5.3
PHP Array of AssociativeArrays (tabluar composite data variable)
Alternate name for this composite variable is ArrayOfDictionary (AOD)
Problem
Scenario: DeveloperMarsher has a PHP tabular composite variable
DeveloperMarsher wishes to extract distinct values on a specific name-value pair
In the example below, DeveloperMarsher wishes to get rows for each distinct fname name-value pair
Solution
example01 ;; DeveloperMarsher starts with a tabluar data variable that looks like this
$aodtable = json_decode('[
{
"fname": "homer"
,"lname": "simpson"
},
{
"fname": "homer"
,"lname": "jackson"
},
{
"fname": "homer"
,"lname": "johnson"
},
{
"fname": "bart"
,"lname": "johnson"
},
{
"fname": "bart"
,"lname": "jackson"
},
{
"fname": "bart"
,"lname": "simpson"
},
{
"fname": "fred"
,"lname": "flintstone"
}
]',true);
example01 ;; DeveloperMarsher can extract distinct values with a foreach loop that tracks seen values
$sgfield = 'fname';
$bgnocase = true;
//
$targfield = $sgfield;
$ddseen = Array();
$vout = Array();
foreach ($aodtable as $datarow) {
if( (boolean) $bgnocase == true ){ #$datarow[$targfield] = #strtolower($datarow[$targfield]); }
if( (string) #$ddseen[ $datarow[$targfield] ] == '' ){
$rowout = array_intersect_key($datarow, array_flip(array_keys($datarow)));
$ddseen[ $datarow[$targfield] ] = $datarow[$targfield];
$vout[] = Array( $rowout );
}
}
//;;
print var_export( $vout, true );
Output result
array (
0 =>
array (
0 =>
array (
'fname' => 'homer',
'lname' => 'simpson',
),
),
1 =>
array (
0 =>
array (
'fname' => 'bart',
'lname' => 'johnson',
),
),
2 =>
array (
0 =>
array (
'fname' => 'fred',
'lname' => 'flintstone',
),
),
)
Pitfalls
This solution does not aggregate on fields that are not part of the DISTINCT operation
Arbitrary name-value pairs are returned from arbitrarily chosen distinct rows
Arbitrary sort order of output
Arbitrary handling of letter-case (is capital A distinct from lower-case a ?)
See also
php array_intersect_key
php array_flip
function array_unique_recursive($array)
{
$array = array_unique($array, SORT_REGULAR);
foreach ($array as $key => $elem) {
if (is_array($elem)) {
$array[$key] = array_unique_recursive($elem);
}
}
return $array;
}
Doesn't that do the trick ?
`
$arr = array(
array('user_id' => 33, 'tmp_id' => 3),
array('user_id' => 33, 'tmp_id' => 4),
array('user_id' => 33, 'tmp_id' => 3),
array('user_id' => 33, 'tmp_id' => 4),
);
$arr1 = array_unique($arr,SORT_REGULAR);
echo "<pre>";
print_r($arr1);
echo "</pre>";
Array(
[0] => Array(
[user_id] => 33
[tmp_id] => 3
)
[1] => Array(
[user_id] => 33
[tmp_id] => 4
)
)
`

How can I merge PHP arrays?

I have two arrays of animals (for example).
$array = array(
array(
'id' => 1,
'name' => 'Cat',
),
array(
'id' => 2,
'name' => 'Mouse',
)
);
$array2 = array(
array(
'id' => 2,
'age' => 321,
),
array(
'id' => 1,
'age' => 123,
)
);
How can I merge the two arrays into one by the ID?
#Andy
http://se.php.net/array_merge
That was my first thought but it doesn't quite work - however array_merge_recursive might work - too lazy to check right now.
This does what Erik suggested (id no. as array key) and merges vlaues in $array2 to $results.
$results = array();
foreach($array as $subarray)
{
$results[$subarray['id']] = array('name' => $subarray['name']);
}
foreach($array2 as $subarray)
{
if(array_key_exists($subarray['id'], $results))
{
// Loop through $subarray would go here if you have extra
$results[$subarray['id']]['age'] = $subarray['age'];
}
}
First off, why don't you use the ID as the index (or key, in the mapping-style array that php arrays are imo)?
$array = array(
1 => array(
'name' => 'Cat',
),
2 => array(
'name' => 'Mouse',
)
);
after that you'll have to foreach through one array, performing array_merge on the items of the other:
foreach($array2 as $key=>$value) {
if(!is_array($array[$key])) $array[$key] = $value;
else $array[$key] = array_merge($array[key], $value);
}
Something like that at least. Perhaps there's a better solution?
<?php
$a = array('a' => '1', 'b' => array('t' => '4', 'g' => array('e' => '8')));
$b = array('c' => '3', 'b' => array('0' => '4', 'g' => array('h' => '5', 'v' => '9')));
$c = array_merge_recursive($a, $b);
print_r($c);
?>
array_merge_recursive — Merge two or more arrays recursively
outputs:
Array
(
[a] => 1
[b] => Array
(
[t] => 4
[g] => Array
(
[e] => 8
[h] => 5
[v] => 9
)
[0] => 4
)
[c] => 3
)
#Andy
I've already looked at that and didn't see how it can help merge multidimensional arrays. Maybe you could give an example.
#kevin
That is probably what I will need to do as I think the code below will be very slow.
The actual code is a bit different because I'm using ADOdb (and ODBC for the other query) but I'll make it work and post my own answer.
This works, however I think it will be very slow as it goes through the second loop every time:
foreach($array as &$animal)
{
foreach($array2 as $animal2)
{
if($animal['id'] === $animal2['id'])
{
$animal = array_merge($animal, $animal2);
break;
}
}
}
foreach ($array as $a)
$new_array[$a['id']]['name'] = $a['name'];
foreach ($array2 as $a)
$new_array[$a['id']]['age'] = $a['age'];
and this is result:
[1] => Array
(
[name] => Cat
[age] => 123
)
[2] => Array
(
[name] => Mouse
[age] => 321
)
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
With PHP 5.3 you can do this sort of merge with array_replace_recursive()
http://www.php.net/manual/en/function.array-replace-recursive.php
You're resultant array should look like:
Array (
[0] => Array
(
[id] => 2
[name] => Cat
[age] => 321
)
[1] => Array
(
[id] => 1
[name] => Mouse
[age] => 123
)
)
Which is what I think you wanted as a result.
I would rather prefer array_splice over array_merge because of its performance issues, my solution would be:
<?php
array_splice($array1,count($array1),0,$array2);
?>
$new = array();
foreach ($array as $arr) {
$match = false;
foreach ($array2 as $arr2) {
if ($arr['id'] == $arr2['id']) {
$match = true;
$new[] = array_merge($arr, $arr2);
break;
}
}
if ( !$match ) $new[] = $arr;
}

Categories