How to combine two array in php? [duplicate] - php

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));

Related

I want to store data in new index as per my key in php [duplicate]

This question already has answers here:
Split array into a specific number of chunks
(7 answers)
Closed 9 months ago.
I am having a data in array format with key and value what I want is from first key 0 to key 6 data I want to store in a new array with index 0 same for every other key 7 to 13 key with 1 index in new array in PHP
First, as suggested above, do not upload image of your code, just paste it here.
Just use PHP array_chunk function:
$pieces = array_chunk($my_array, 6);
you can use array_chunk() function
<?php
$a = ["a","a","a","a","a","a","a","a"];
$b = array_chunk($a, 7);
echo"<pre>";print_r($b);
?>
output be like:
Array
(
[0] => Array
(
[0] => a
[1] => a
[2] => a
[3] => a
[4] => a
[5] => a
[6] => a
)
[1] => Array
(
[0] => a
)
)

PHP : How combine array like layer [duplicate]

This question already has answers here:
Can't concatenate 2 arrays in PHP
(11 answers)
Closed 6 years ago.
I have two arrays, I need to merge both like layer
first array
Array (
[0]=>1
[1]=>2
[2]=>3
)
Second array
Array (
[0]=>4
[1]=>5
[2]=>6
)
How to combine it with a result of
Array (
[0]=>1
[1]=>2
[2]=>3
[3]=>4
[4]=>5
[5]=>6
)
If you don't need to do anything with indexes then the right function is array_merge so in this example:
array_merge($a, $b);
Interactive mode enabled
php > $a = [1, 2, 3];
php > $b = [4, 5, 6];
php > print_r(array_merge($a, $b));
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)

What is the way to mearge two arrays even duplicate values exists?

sele_itmid =
Array
(
[0] => 1
[1] => 1
[2] => 5
[3] => 6
)
$recp_qty =
Array
(
[0] => 4
[1] => 16
[2] => 1
[3] => 10
)
//when i tried using
$comine = array_combine($sele_itmid,$recp_qty);
print_r($comine);exit();
am getting a result like
Array
(
[1] => 16
[5] => 1
[6] => 10
)
what i actually want is
[1]=>4
[1] => 16
[5] => 1
[6] => 10
If possible some one Please explain why array_combine neglecting it!!
after getting an array what i actually want need to sum values of same keys
"why array_combine neglecting it?" - an array doesn't allow duplicate keys.
Here is a simple solution using array_map function (it will sum up the values of the same keys):
$result = [];
array_map(function($key, $b) use (&$result){
(isset($result[$key]))? $result[$key] += $b : $result[$key] = $b;
}, $sele_itmid, $recp_qty);
print_r($result);
The output:
Array
(
[1] => 20
[5] => 1
[6] => 10
)
Sounds as though you'd want to just map the two arrays together:
function sum($v1, $v2) {
return $v1 + $v2;
}
$result = array_map('sum', $sele_itmid, $recp_qty);
In here $sele_itmid 's values are used as the key of $comine array. Since an array can not have duplicate keys first value is rejected.

Combining the keys and values of two arrays [duplicate]

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.

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