Combining the keys and values of two arrays [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Merging PHP array, one as Keys the other as Values?
I have the following two arrays in PHP, which represent atmospheric gases and their compositions:
Array
(
[0] => nitrogen
[1] => argon
[2] => oxygen
[3] => carbon dioxide
)
Array
(
[0] => 78
[1] => 1
[2] => 21
[3] => 0
)
Is there an elegant way of obtaining the following array, a combination of keys and values:
Array
(
"nitrogen" => 78
"argon" => 1
"oxygen" => 21
"carbon dioxide" => 0
)
The methods I can think of involve loops, I don't know if I'm complicating it. Is there a simple way?

array_combine(), simply:
$new_arr = array_combine($keys,$values);

use array_combine
$key = array('nitrogen', 'argon', 'oxygen', 'carbon dioxide');
$values = array(78, 1, 21, 0 );
$return = array_combine($key, $values);
print_r($return);
output:
Array
(
[nitrogen] => 78
[argon] => 1
[oxygen] => 21
[carbon dioxide] => 0
)

array_combine() Example:
<?php
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
[a] => 2
[b] => 3
)
If two keys are the same, the second one prevails.

Believe it or not - array_combine() exists and is exactly what you are looking for!
http://php.net/manual/en/function.array-combine.php
array_combine — Creates an array by using one array for keys and
another for its values
Example adapted from the above link -
$a = array('Stack', 'Server', 'Super');
$b = array('Overflow', 'Fault', 'User');
$c = array_combine($a, $b);
print_r($c);
Array
(
[Stack] => Overflow
[Server] => Fault
[Super] => User
)
And there you have it - an array fit for a trilogy.

Related

Multiple arrays merging issue [duplicate]

This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 5 years ago.
I have two arrays
First array
array(
[0] => +970
[1] => +971
[2] => +972
)
And Second array
array(
[0] => 465465454
[1] => 321321355
[2] => 987946546
)
I want to merge them like this
array(
[+970] => 465465454
[+971] => 321321355
[+972] => 987946546
)
I try with array_merge but this gives me the result that I didn't want e.g.
$busi_code = $page1_data->business_code; //array
$busi_num = $page1_data->business_number; //array
$business_phone_numbers = array_merge($busi_code, $busi_num);
echo '<pre>';
print_r($business_phone_numbers);
echo '</pre>';
And its result is
[0] => +970
[1] => +971
[2] => +972
[3] => 465465454
[4] => 321321355
[5] => 987946546
So please guide me how can I achieve my required result.
You're looking for array_combine, rather than array_merge:
Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
$business_phone_numbers = array_combine($busi_code, $busi_num);
See https://eval.in/954799
This is job for array_combine function:
$business_phone_numbers = array_combine($busi_code, $busi_num);
DOCS: http://php.net/manual/en/function.array-combine.php
You must use array_combine.
Try this:
$a = array(
0 => +970,
1 => +971,
2 => +972);
$b = array(
0 => 465465454,
1 => 321321355,
2 => 987946546);
$r = array_combine($a,$b);
print_r($r);

how to create the multiple values with same key in array in php [duplicate]

This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed last month.
I have an array with multiple key.
Array 1
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Output
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
Expected Output
$age = array("value"=>"35", "value"=>"37", "value"=>"43");
Array ( [value] => 35 [value] => 37 [value] => 43 )
You can't
Indeed, array keys must be unique. Otherwise, what should the program output when you try to access value ?
But ...
If you need to store a list of value for one key, you can use array of arrays.
$array = array("value" => array());
array_push($array["value"], 35, 40, 53);
print_r($array)
And the output will be:
Array
(
[value] => Array
(
[0] => 35
[1] => 40
[2] => 53
)
)
Only way is to turn this array into 2D array:
$age = array(
array("value" => "35"),
array("value" => "37"),
array("value" => "43")
);
-- Output --
Array
(
[0] => Array
(
[value] => 35
)
[1] => Array
(
[value] => 37
)
[2] => Array
(
[value] => 43
)
)
-- Usage --
$age[0]['value'];
$age[1]['value'];
$age[2]['value'];
But it completely depends if $age array is in our control and can be changed.
You can't
Array keys must be unique.
http://php.net/manual/en/language.types.array.php
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Yes, array key should be unique. So, whatever you are asking it's not possible. Can you tell us what's requirements? So that, people can suggest any alternate solution.
This cannot be possible with native php arrays. What you need is a multimap, and you can find several implementations of it on github.
For example: link
EDIT: the link above is an interface. you need to include also link2

Change the content of an array to be more efficient [duplicate]

This question already has answers here:
PHP array replace numbers with keys
(2 answers)
Closed 6 years ago.
When doing a print_r on my array, I get the following output;
Array
(
[0] => Array
(
[id] => 178
[name] => Briar Price
)
[1] => Array
(
[id] => 90
[name] => Bradley Kramer
)
[2] => Array
(
[id] => 508
[name] => Calvin Yang
)
[3] => Array
(
[id] => 457
[name] => Charles Valenzuela
)
... and so on
How can I modify the array to look like this;
Array
(
[178] => Briar Price
[90] => Bradley Kramer
[508] => Calvin Yang
[457] => Charles Valenzuela
... and so on
)
I just want to make the ID the key for the value name. I always have issues when it comes to arrays reordering.
Pass third parameter to array_column to make its key as
$array = array_column($users, 'name', 'id');
print_r($array);
Use foreach() -
$newArr = array();
foreach ($your_array as $key => $val) {
$newArr[$val['id']] = $val['name'];
}
print_r($newArr) // desired output
I'd use array_combine which attaches new keys to values
$results = [
['id'=>1,'name'=>'John'],['id'=>2,'name'=>'Jane'],
];
$results = array_combine(
array_column($results,'id'), //use 'id' column as keys
array_column($results,'name') //use 'name' column as values
);
//now $results is [1=>'John', 2=>'Jane']
You can do it using array_column and array_combine PHP function without applying custom logic.
Here is how you do it,
<?php
$keys=array_column($mainarray,'id');
$values=array_column($mainarray,'name');
$finalarray=array_combine($keys,$values);
$finalarray will be your desired result.
array_combine creates an array by using one array for keys and another for its values.
array_column returns the values from a single column in the input array.

How to combine two array in php? [duplicate]

This question already has an answer here:
Merging 2 arrays by keys
(1 answer)
Closed 6 years ago.
I have two array $a and $b
Array
(
[0] => 230
[1] => 2
[2] => 71
)
Array
(
[0] => 1500
[1] => 3500
[2] => 5000
)
I want an array which will look like
Array
(
[230] => 1500
[2] => 3500
[71] => 5000
)
How is it possible?
The actual question should have led you to the answer. The way to combine two arrays is by using the array_combine function.
Update: here is a code sample http://codepad.org/6r5DzCmc
Use array_combine.
array_combine($a, $b);
try http://php.net/manual/en/function.array-combine.php
$a = array(230, 2, 71);
$b = array(1500, 3500, 5000);
print_r(array_combine($a, $b));

Add array to an array with the same indexes not being merged [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Combine Two Arrays with numerical keys without overwriting the old keys
OK guys, was searching about this one with no luck - it always points only to array_merge or array_push or array_combine functions which are useless for my purpose.
Here are two arrays (number indexed):
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
)
Array (
[0] => 25485
[1] => "tyjfhgdfsasdfsdf"
[2] => "mojsbnvgsdfbsdf"
)
and I need to create one "joined" (unioned) array, so it will look like:
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
[3] => 25485
[4] => "tyjfhgdfsasdfsdf"
[5] => "mojsbnvgsdfbsdf"
)
As I found nothing on this problem I tried by myself ($arr1 and $arr2 are the two small arrays):
$result_array = $arr1;
foreach($arr2 as $v) {
$result_array[] = $v;
}
This is, of course, working fine but I don't like this approach - imagine the situation when there will not be just 3 elements in second array...
Question: is there a better approach or at the best some built-in function (I do not know about)???
array_merge will work without any problem as your using numeric keys ... see the explanation below from the docs
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
emphasis mine
Array merge works fine for your numerically indexed arrays:
<?php
$arrayOne = array(
0 => 12345
,1 => "asdvsdfsasdfsdf"
,2 => "sdgvsdfgsdfbsdf"
);
$arrayTwo = array(
0 => 25485
,1 => "tyjfhgdfsasdfsdf"
,2 => "mojsbnvgsdfbsdf"
);
$arrayMerged = array_merge($arrayOne, $arrayTwo);
print_r($arrayMerged);
?>
output:
Array
(
[0] => 12345
[1] => asdvsdfsasdfsdf
[2] => sdgvsdfgsdfbsdf
[3] => 25485
[4] => tyjfhgdfsasdfsdf
[5] => mojsbnvgsdfbsdf
)

Categories