How to combine 2 arrays into 1 array - php

I have 2 arrays like that:
array1
(
[0] => Array
(
[id] => 133
)
[1] => Array
(
[id] => 134
)
)
array2
(
[0] => 1
[1] => 2
)
My problem is: how can I combine two arrays into one array like:
array3
(
[133] => 1
[134] => 2
)
Thanks for any help :D

Try
$array3 = array();
foreach ($array1 as $key => $value) {
$array3[$value['id']] = $array2[$key];
}

$array3 = array_combine(array_map('current', $array1), $array2);

I've done it like this:
<?php
$arrayOne = array(
array("id" => 133),
array("id" => 134)
);
$arrayTwo = array(1,2);
$arrayThree = array();
foreach($arrayOne as $index => $value){
$arrayThree[$value['id']] = $arrayTwo[$index];
}
if you do a
print_r($arrayThree);
now you will get your third array:
Array
(
[133] => 1
[134] => 2
)

Related

Merge values from different arrays to one with the same key [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have two arrays:
Array
(
[0] => 5
[1] => 4
)
Array
(
[0] => BMW
[1] => Ferrari
)
And I would like to have that result. Merge the values with the same key
Array
(
[0] => Array
(
[0] => 5
[1] => BMW
)
[1] => Array
(
[0] => 4
[1] => Ferrari
)
)
How could I do that? Is there any native PHP function that does this? I tried array_merge_recursive and array_merge but did not get the expected result
As to #splash58 comment:
You can use array-map. Here an example:
$array = [["5", "4"], ["BMW", "Ferrari"]];
$res = array_map(null, ...$array);
Now res will contain:
Array
(
[0] => Array
(
[0] => 5
[1] => BMW
)
[1] => Array
(
[0] => 4
[1] => Ferrari
)
)
If the array in 2 different var you can use:
$res= array_map(null, ["5", "4"], ["BMW", "Ferrari"]);
<?php
$a = array();
$a[0] = '5';
$a[1] = '4';
$b = array();
$b[0] = 'BMW';
$b[1] = 'Ferrari';
merge_by_key($a, $b);
function merge_by_key($a, $b){
$c = array();
fill_array($c,$a);
fill_array($c,$b);
print_r($c);
}
function fill_array(&$c, $a) {
foreach ($a as $key => $value){
if(isset($c[$key])) {
array_push($c[$key], $value);
} else {
$c[$key] = array($value);
}
}
}
Output:
Array
(
[0] => Array
(
[0] => 5
[1] => BMW
)
[1] => Array
(
[0] => 4
[1] => Ferrari
)
)
You can use array_map with null as the first argument (there is an example in the manual), to get your desired result:
<?php
$nums = [0 => 5, 1 => 4];
$cars = [0 => 'BMW', 1 => 'Ferrari'];
var_export(array_map(null, $nums, $cars));
Output:
array (
0 =>
array (
0 => 5,
1 => 'BMW',
),
1 =>
array (
0 => 4,
1 => 'Ferrari',
),
)
Note that the following input would give the same result:
$nums = ['puff' => 5, 'powder' => 4];
$cars = ['powder' => 'BMW', 'puff' => 'Ferrari'];
It is the order, not the keys, that determine the pairings in the result when using array_map as above.
To associate by key using foreach (note order of $cars):
<?php
$nums = [0 => 5, 1 => 4];
$cars = [1 => 'Ferrari', 0 => 'BMW'];
foreach($nums as $k => $num)
$result[] = [$num, $cars[$k]];
var_export($result);
Results also in the desired output.

Assigning an array as a value of another array

I have three arrays i need to create an array which can be multidimensional.
1st array
Array
(
[0] => Test_One
[1] => Test_two
)
2nd array
Array
(
[0] => www.link.com
[1] => www.link2.com
)
3rd array
Array
(
[0] => Song1
[1] => song2
)
What i want
Array
(
[www.link.com] => Array
(
[0] => Test_one
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
[1] => Song2
)
)
Assuming that you have same number of elements in all three arrays:
<?php
$arr1 = Array
(
0 => "Test_One",
1 => "Test_two"
);
$arr2 = Array
(
0 => "www.link.com",
1 => "www.link2.com"
);
$arr3 = Array
(
0 => "Song1",
1 => "Song2"
);
$final = []; //for versions below PHP 5.4 use $final = array();
foreach($arr2 as $key=>$value) {
$final[$value] = [$arr1[$key],$arr3[$key]];
}
print_r($final);
will output:
Array
(
[www.link.com] => Array
(
[0] => Test_One
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
[1] => Song2
)
)
Update: Simplified foreach loop. From Comments #uchiha
Assuming that you havn't same number of elements in all three arrays:
<?php
$arr1 = Array
(
0 => "Test_One",
1 => "Test_two"
);
$arr2 = Array
(
0 => "www.link.com",
1 => "www.link2.com"
);
$arr3 = Array
(
0 => "Song1",
);
$final = []; //for versions below PHP 5.4 use $final = array();
foreach($arr2 as $key=>$value) {
if(array_key_exists($key,$arr1)) {
$final[$value][] = $arr1[$key];
}
if(array_key_exists($key,$arr3)) {
$final[$value][] = $arr3[$key];
}
}
print_r($final);
Output:
Array
(
[www.link.com] => Array
(
[0] => Test_One
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
)
)
Hope this help :)
<?php
$array1 = Array
(
'Test_One',
'Test_two'
);
$array2 = Array
(
'www.link.com',
'www.link2.com'
);
$array3 = Array
(
'Song1',
'song2'
);
$array4 = array();
$i = 0;
foreach ($array2 as $a2){
$array4[$a2][] = $array1[$i];
$array4[$a2][] = $array3[$i];
$i++;
}
echo "<pre>";
print_r($array4);
?>

merging subarrays of multidimensional arrays while sorting array alphabetically

Trying to alphabetically merge arrays with foreach loop.
<?php
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
$result[$first_letter] = $subarr;
}
print_r($result);
gives me smth like
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 5
[1] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)
instead of smth like
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
how can I fix it?
You overwrite your result every iteration in this line:
$result[$first_letter] = $subarr;
Just create a new array if the subArray in the result array doesn't exists and merge the ids subArray into your result array.
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
if(!isset($result[$first_letter]))
$result[$first_letter] = [];
$result[$first_letter] = array_merge($result[$first_letter], $subarr["ids"]);
}
Please try to use foreach loop.
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
foreach($subarr as $key=>$value){
foreach ($value as $gkey => $gvalue) {
$result[$first_letter]['ids'][] = $gvalue;
}
}
}
echo "<pre>";
print_r($result);
Display above code output like below.
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)

how to get array from get to normal array in php

I have an array like this and it can contain multiple values:
Array
(
[rpiid] => Array
(
[1] => 86
)
[sensor_id] => Array
(
[1] => 1
)
[when] => Array
(
[1] => 2014-02-24
)
[val] => Array
(
[1] => 000
)
[train] => Array
(
[1] => True
)
[valid] => Array
(
[1] => False
)
[button] => update
)
Of course, here there is only the number 1 each time but sometimes I have 0, 1, 2 and a value associated. This is because I get this from a GET from multiple forms.
How can I transform this array into
Array
(
[0] => Array
(
[rpiid] => 86
[sensor_id] => 1
...
Thanks,
John.
if your array is $get
$newArray = Array();
foreach($get as $secondKey => $innerArray){
foreach($value as $topKey => $value) {
$newArray[$topKey][$secondKey] = $value;
}
}
This should work
$new_array = array();
foreach($first_array as $value => $key){
$new_array[$key] = $value[1];
}
Sure you can, take a look at this small example:
$a = [ 'rpid' => [1], 'cpid' => [2,2] ];
$nodes = [];
foreach($a as $node => $array) {
foreach($array as $index => $value) {
if(empty($nodes[$index]))
$nodes[$index] = [];
$nodes[$index][$node] = $value;
}
}
print_r($nodes):
Array
(
[0] => Array
(
[rpid] => 1
[cpid] => 2
)
[1] => Array
(
[cpid] => 2
)
)

Combine arrays to form a multidimensional array

I have three arrays:
$arr1 = Array (
[0] => 1001
[1] => 1007
[2] => 1006);
$arr2 = Array (
[0] => frank
[1] => youi
[2] => nashua);
$arr3 = Array (
[0] => getfrankemail
[1] => getyouiemail
[2] => getnashuaemail);
Is there a way to combine these arrays to get a multidimensional array like this:?
Array (
[0] => Array (
[0] => 1001
[1] => frank
[2] => getfrankemail)
[1] => Array (
[0] => 1007
[1] => youi
[2] => getyouiemail)
[2] => Array (
[0] => 1006
[1] => nashua
[2] => getnashuaemail)
);
edit: what you are really looking for is a php version of the zip method in ruby/python.
For your specific example array_map works nicely:
$result = array_map(null, $arr1, $arr2, $arr3);
Output:
array (
0 =>
array (
0 => 1001,
1 => 'frank',
2 => 'frankemail',
),
1 =>
array (
0 => 1007,
1 => 'youi',
2 => 'youiemail',
),
2 =>
array (
0 => 1006,
1 => 'nashua',
2 => 'nashuaemail',
),
)
Iterate on the first array (looks like those are ids), and you can match the key for each value to indexes in $arr2 and $arr3
$result = array();
foreach ($arr1 as $key => $value) {
$result[] = array($value, $arr2[$key], $arr3[$key]);
}
as #kingkero mentions in his answer, you will get errors if they keys do not exist, which you could check for and ignore any rows where that is the case.
$result = array();
foreach ($arr1 as $key => $value) {
if (!isset($arr2[$key]) || !isset($arr3[$key])) {
continue;
}
$result[] = array($value, $arr2[$key], $arr3[$key]);
}
You could use array_push($aContainer, $arr1); or $aContainer[] = $arr[1]
You can do this with a loop in which you access each of the three arrays with the same key.
$result = array();
$max = count($arr1);
for ($i=0; $i<$max; $i++) {
$result[] = array(
$arr1[$i],
$arr2[$i],
$arr3[$i],
);
}
This could fire an out of bounds exception, since it doesn't check whether or not $arrX[$i] exists. Can you be sure that it does?

Categories