I have an array like the following
Array ( [0] => "txt1" [1] => "txt2" [2] => "txt3")
I have another array like it but with different content : Array ( [0] => on [2] => on)
The aim is to get a final array with the keys of the second and the content of the first, it's like merging them.
So that the final result is : Array ( [0] => "txt1" [2] => "txt3") It would be better to change the keys to 0 - 1, but that a trivial issue, let's focus on merging them one to one.
The easiest way to do this is with array_intersect_key
(See the PHP Docs). It grabs the values from the first array passed corresponding to the keys present in all other arrays passed.
So, your example would look like this:
$a = array(0 => "txt1", 1 => "txt2", 2 => "txt3");
$b = array(0 => 1, 2 => 1);
$c = array_intersect_key($a, $b);
print_r($c);
prints:
Array
(
[0] => txt1
[2] => txt3
)
array_combine
- Creates an array by using one array for keys and another for its values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With