How do I combine two arrays where the first array contains duplicate keys? e.g. combine the following:
$keys_arr
Array
(
[0] => iPhone
[1] => iPhone
[2] => iPhone
[3] => Samsung
)
$values_arr
Array
(
[0] => 5
[1] => 5
[2] => 5
[3] => Galaxy IV
)
The result I'm looking for is:
$combined_array
Array
(
[iPhone] => 5
[iPhone] => 5
[iPhone] => 5
[Samsung] => Galaxy IV
)
Using the following foreach (PHP - merge two arrays similar to array_combine, but with duplicate keys):
foreach ($keys_arr as $key => $value) {
$combined_array[] = array($value => $values_arr[$key]);
}
I get:
Array
(
[0] => Array
(
[iPhone] => 5
)
[1] => Array
(
[iPhone] => 5
)
[2] => Array
(
[iPhone] => 5
)
[3] => Array
(
[Samsung] => Galaxy IV
)
)
Where am I going wrong?
$combined_array = array(array('iPhone', '5'), array('iPhone' ,'5'), array('iPhone' ,'5'), array('Samsung' ,'Galaxy IV') );[{'product_line':'iPhone', 'model': '5'}, ... , {'product_line':'Samsung','model': 'Galaxy IV'}]such that you create a new property name for each of the array values by which you can definitively lookup the information you are trying to store.$combined_array[] = ...will add a new element (which happens to be an array) to$combined_array.