Issue with array_combine() - php

I have two arrays. The one's array key is another's value. Here is code:
$arr1 = array(
'a' => 'apple',
'b' => 'banana',
'c' => 'pear',
);
$arr2 = array(
'bird' => 'a',
'dog' => 'b',
);
And my question, how to combine two arrays in one like:
$arr3 = array(
'bird' => 'apple',
'dog' => 'banana',
);
Is there have some array function to do this probably?

<?php
$arr3 = array();
foreach ($arr2 as $item => $value) {
$arr3[$item] = $arr1[$value];
}
print_r($arr3);
something along those lines anyway.
If you literally want to merge the arrays, array_merge will do the job fine.

Edit: This is a fun way and matches the keys:
$arr3 = array_combine(array_intersect_key($k = array_flip($arr2), $arr1),
array_intersect_key($arr1, $k));
Original with no key matching:
Here's a way. Doesn't matter which array is longer:
$arr3 = array_combine(array_slice(array_keys($arr2), 0, count($arr1)),
array_slice($arr1, 0, count($arr2)));

Related

Combine a value from an array with each values from other?

What i want is to combine every value from first array with every value from second array.
For example, let's take two arrays:
$array1 = ['green', 'red', 'blue'];
$array2 = ['s', 'm'];
The result array should be:
$result = [1 => 'green-s', 2 => 'green-m', 3 => 'red-s', 4 => 'red-m', 5 => 'blue-s' ...];
The result array can be different, but with that elements combined.
check this,
<?php
$array1 = array('green', 'red', 'blue');
$array2 = array('s', 'm');
$data = array();
foreach($array1 as $val){
foreach($array2 as $val2){
$data[] = $val."-".$val2;
}
}
print_r($data);
?>

Get all the keys presented in all arrays

I have some arrays, for example
$arr[0]=array(k1=>1,k2=>1,k3=>1);
$arr[1]=array(k2=>1,k3=>1,k4=>1);
$arr[2]=array(k3=>1,k4=>1,k5=>1);
So, I need to get all the keys (dynamically, the number of arrays can differ), presented in all arrays. In this case it is k3 key. So the result should be array('k3'=>1)
I suggest it could be achieved by multiple loops, but probably there's some easier way.
You need the function array_intersect_key():
<?php
$arr1 = array('k1' => 1, 'k2' => 1, 'k3' => 1);
$arr2 = array('k2' => 1, 'k3' => 1, 'k4' => 1);
$arr3 = array('k3' => 1, 'k4' => 1, 'k5' => 1);
print_r(
array_intersect_key($arr1, $arr2, $arr3)
);
Output:
Array
(
[k3] => 1
)
To get the common elements in three arrays, you can use array_intersect()
Note: This function works on common array values and not common array keys
Try this:
$key1 = array_flip($arr1);
$key2 = array_flip($arr1);
$key3 = array_flip($arr1);
$intersect = array_flip(array_intersect($key1, $key2, $key3));

php - sort an array by key to match another array's order by key

I have two arrays, both have the same keys (different values) however array #2 is in a different order. I want to be able to resort the second array so it is in the same order as the first array.
Is there a function that can quickly do this?
I can't think of any off the top of my head, but if the keys are the same across both arrays then why not just loop over the first one and use its key order to create a new array using the the values from the 2nd one?
$arr1 = array(
'a' => '42',
'b' => '551',
'c' => '512',
'd' => 'gge',
) ;
$arr2 = array(
'd' => 'ordered',
'b' => 'is',
'c' => 'now',
'a' => 'this',
) ;
$arr2ordered = array() ;
foreach (array_keys($arr1) as $key) {
$arr2ordered[$key] = $arr2[$key] ;
}
You can use array_replace
$arr1 = [
'x' => '42',
'y' => '551',
'a' => '512',
'b' => 'gge',
];
$arr2 = [
'a' => 'ordered',
'x' => 'this',
'y' => 'is',
'b' => 'now',
];
$arr2 = array_replace($arr1, $arr2);
$arr2 is now
[
'x' => this,
'y' => is,
'a' => ordered,
'b' => now,
]
foreach(array_keys($array1) as $key)
{
$tempArray[$key] = $array2[$key];
}
$array2 = $tempArray;
I am not completely sure if this is what your after. anyways as long as the the array remains the same size, than this should work for you.
$gamey = array ("wow" => "World of Warcraft", "gw2" => "Guild Wars2", "wiz101" => "Wizard 101");
$gamex = array ("gw2" => "best game", "wiz101" => "WTF?", "wow" => "World greatest");
function match_arrayKeys ($x, $y)
{
$keys = array_keys ($x);
$values = array_values ($y);
for ($x = 0; $x < count ($keys); $x++)
{
$newarray [$keys[$x]] = $y[$keys[$x]];
}
return $newarray;
}
print_r (match_arrayKeys ($gamey, $gamex));
Output
[wow] => World greatest
[gw2] => best game
[wiz101] => WTF?
Try this
CODE
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
OUTPUT
a = orange
b = banana
c = apple
d = lemon
Check the php manual for ksort()

php. array_values function. how to get mapping from old keys to new keys?

There is function array_values in PHP such that
$array2 = array_values($array1);
$array2 has the same values as $array1 but keys are from
0 to sizeof($array1) - 1. Is it possible to get mapping from old keys to new keys?
EDIT. I will explain on an example:
$array1 = array( 'a' => 'val1', 'b' => 'val1');
$array2 = array_values( $array1 );
so now array2 has next values
$array2[0] = 'val1'
$array2[1] = 'val2'
How get array3 such that:
$array3['a'] = 0
$array3['b'] = 1
To produce a key map you need to first get the keys into a regular array and then flip the keys and values:
$array1_keymap = array_flip(array_keys($array1));
For example:
$array1 = array(
'a' => 123,
'b' => 567,
);
$array1_values = array_values($array1);
$array1_keymap = array_flip(array_keys($array1));
Value of $array1_values:
array(
0 => 123,
1 => 567,
);
Value of $array1_keymap:
array(
'a' => 0,
'b' => 1,
);
So:
$array1['a'] == $array1_values[$array1_keymap['a']];
$array1['b'] == $array1_values[$array1_keymap['b']];
Yes, as simple as
$array2 = $array1;
In this case you would get both values and keys like they are in the original array.
$keyMapping = array_combine(array_keys($array1), array_keys($array2));
This the keys of $array1 and maps them to the keys of $array2 like so
<?php
$array1 = array(
'a' => '1',
'b' => '2',
);
$array2 = array_values($array1);
print_r(array_combine(array_keys($array1), array_keys($array2)));
Array
(
[a] => 0
[b] => 1
)
You can use:
$array3 = array_keys($array1);
Now $array3[$n] is the key of the value in $array2[$n] for any 0 <= $n < count($array1). You can use this to determine which keys were in which places.
If you want to keep the same value of array1 but change the key to index numbers, try this:
$array2 = array();
foreach ($array1 as $key => $value){
$array2[] = $value;
// or array_push($array2, $value);
}
var_dump($array2);

Drop elements from a array based on another array

$arr1 = array(
'a' => 123,
'b' => 123,
'c' => 123,
'd' => 123,
);
$arr2 = ('a', 'b', 'c', 'd', 'e');
How can I remove elements from $arr2 that don't exist as keys in $arr1 ?
for example e doesn't exist as key in $arr1 so it should be removed
$arr2 = array_intersect($arr2, array_keys($arr1))
it computes intersection of two sets - $arr2 values and $arr1 keys
Try this:
foreach ( $arr2 as $key => $value ) {
if ( !array_key_exists( $value, $arr1 ) ) {
unset( $arr2[$key] );
}
}
Why not use the simple approach and use array_keys? This avoids having to perform an operation for each key by getting ALL of the keys at once.
$arr2 = array_keys($arr1);

Categories