Combine arrays to form a multidimensional array - php

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?

Related

Merge array key values - PHP

Hi all i need to merge the same key to convert to single array from multiple array list please any one help me to the problem
for example here the array.
Array
(
[0] => Array
(
[0] => Mr.
[1] => Mrs.
)
[1] => Array
(
[0] => Rob
[1] => Tam
)
[2] => Array
(
[0] => kar
[1] => Man
)
[3] => Array
(
[0] => 55345345345
[1] => 44545345435
)
)
i need the output is
Array
(
[0] => Array
(
[0] => Mr.
[1] => Rob
[2] => kar
[3] => 55345345345
)
[1] => Array
(
[0] => Mrs.
[1] => Tam
[2] => Man
[3] => 44545345435
)
)
Please any one help
Thanks
For PHP version >= 5.5.0 You can use array_column() and array_merge() for this as
$result = array_merge(array_column($records, '0'), array_column($records, '1'));
print_r($result);
$a = array(
0 => array(
0 => 'Mr.',
1 => 'Mrs.'
),
1 => array
(
0 => 'Rob',
1 => 'Tam'
),
2 => array
(
0 => 'kar',
1 => 'Man'
),
3 => array
(
0 => 55345345345,
1 => 44545345435
)
);
$arr1 = array();
foreach($a as $arr)
{
foreach($arr as $key=>$value)
{
$arr1[$key][] = $value;
}
}
echo '<pre>';
print_r($arr1);
Use this one. You can get output same as you want.
try like this
$out = array();
foreach ($arr1 as $key => $value){
$out[] = (object)array_merge((array)$arr2[$key], (array)$value);
}
print_r($out)
$title = $array[0];
$firstname = $array[1];
$lastname = $array[2];
$number = $array[3];
$output = array();
for($i=0; $i < count($title); $i++)
{
$output[] = array($title[$i],$firstname[$i],$lastname[$i],$number[$i])
}
var_dump($output);

How to concatenate arrays element recursively [duplicate]

This question already has answers here:
Joining similar data from two indexed arrays
(2 answers)
Closed 5 months ago.
I'm working on a project and I'm stacked since 2 days, this is my problem: I have two arrays and want to retrieve the second item in each object in Array_2 and concatenate it to the content of each object in first Array_1 in PHP.
Array_1
[[1453274700000,24011],[1453275000000,24222],[1453275300000,24284],[1453275600000,24331],...]
Array_2
[[1453274700000,51951],[1453275000000,52093],[1453275300000,52251],[1453275600000,52288],...]
Wanted_array
[[1453274700000,24011,51951],[1453275000000,24222,52093],[1453275300000,24284,52251],[1453275600000,24331,52288]...]
A functional solution:
$result = array_map(function (array $a1, array $a2) {
return array_merge($a1, [$a2[1]]);
}, $array_1, $array_2);
This assumes that all items are in order and only need to be merged by their order, not by their first value.
If you want $item[0] to define what "group" each value belongs to, you can iterate through the first array and save $item[0] as the key and $item[1] as the value. Do the same for the second array. Now iterate through the saved array for array1, and check if the saved array for array2 contains the same keys. Do the same for array2 (in case it has key that array1 doesn't have), and save it all to a new array:
<?php
$arr1 = array(
array('1453274700000',24011),
array('1453275000000',24222),
array('1453276000000',24222), // inexistent in $arr2
);
$arr2 = array(
array('1453275000000',52093),
array('1453274700000',51951),
array('1453273000000',24222), // inexistent in $arr1
);
$arr1dictionary = [];
$arr2dictionary = [];
$result = [];
foreach ($arr1 as $collection) {
$arr1dictionary[$collection[0]] = $collection[1];
}
foreach ($arr2 as $collection) {
$arr2dictionary[$collection[0]] = $collection[1];
}
foreach ($arr1dictionary as $key => $value) {
if (isset($arr2dictionary[$key])) {
$result[$key] = [$key, $value, $arr2dictionary[$key]];
} else {
$result[$key] = [$key, $value, null];
}
}
foreach ($arr2dictionary as $key => $value) {
if (isset($result[$key])) {
continue;
}
$result[$key] = [$key, null, $value];
}
$result = array_values($result);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453276000000
[1] => 24222
[2] => (null, the value only exists in $arr1)
)
[3] => Array
(
[0] => 1453273000000
[1] => (null, the value only exists in $arr2)
[2] => 24222
)
)
DEMO
Use array_walk and add second item from $array2 if it exists.
$array1 = array(
array(1453274700000,24011),
array(1453275000000,24222),
array(1453275300000,24284),
array(1453275600000,24331)
);
$array2 = array(
array(1453274700000,51951),
array(1453275000000,52093),
array(1453275300000,52251),
array(1453275600000,52288),
);
array_walk($array1, function(&$item, $key) use ($array2){
if(isset($array2[$key][1])){
$item[] = $array2[$key][1];
}
});
print_r($array1);
Output
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453275300000
[1] => 24284
[2] => 52251
)
[3] => Array
(
[0] => 1453275600000
[1] => 24331
[2] => 52288
)
)
EDIT
As #h2ooooooo pointed out that there could be possibility that array items are in random order. If array items can be in random order and they are matched with first index value, use this (works with PHP >= 5.5.0):
$array1 = array(
array(1453274700000,24011),
array(1453275000000,24222),
array(1453275300000,24284),
array(1453275600000,24331),
array(1453276000000,24222) // no match in $array2
);
$array2 = array(
array(1453275000000,52093),
array(1453274700000,51951),
array(1453275300000,52251),
array(1453275600000,52288),
);
array_walk($array1, function(&$item, $key) use ($array2){
// Find match in $array2
$array2_key = array_search($item[0], array_column($array2, 0));
// If match found
if($array2_key !== false && isset($array2[$array2_key][1])){
$item[] = $array2[$array2_key][1];
}
// No match
else{
$item[] = null;
}
});
print_r($array1);
OUTPUT
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453275300000
[1] => 24284
[2] => 52251
)
[3] => Array
(
[0] => 1453275600000
[1] => 24331
[2] => 52288
)
[4] => Array
(
[0] => 1453276000000
[1] => 24222
[2] =>
)
)

How to combine 2 arrays into 1 array

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
)

Build associative array based on values of another associative array

I'm looking for an elegant way to turn this array:
Array (
[foo] => 1
[bar] => 1
[zim] => 3
[dib] => 6
[gir] => 1
[gaz] => 3
)
Into this array:
Array (
[1] => Array ( foo, bar, gir ),
[3] => Array ( zim, gaz ),
[6] => Array ( dib )
)
Note:, there is no relationship between the keys or values. They are completely arbitrary and used as examples only. The resulting array should be an associative array grouped by the values of the input array.
Thanks!
$input = array(
'foo' => 1,
'bar' => 1,
'zim' => 3,
'dib' => 6,
'gir' => 1,
'gaz' => 3
)
$output = array();
foreach ( $input as $k => $v ) {
if ( !isset($output[$v]) ) {
$output[$v] = array();
}
$output[$v][] = $k;
}
I think this will do it just fine:
foreach ($arr1 as $k => $val) $arr2[$val][] = $k;
where $arr1 is the original array outputting the new array to $arr2.

Consolidate an array in PHP?

I have an array that looks like the following:
Array (
[0] => Array
(
[id] => 10
[value] => 5
)
[1] => Array
(
[id] => 10
[value] => 1
)
[2] => Array
(
[id] => 11
[value] => 1
)
[3] => Array
(
[id] => 11
[value] => 1
)
)
How can I consolidated the array by id? The resulting array I'm looking for would be something like:
Array (
[0] => Array
(
[id] => 10
[value] => 6
)
[1] => Array
(
[id] => 11
[value] => 2
)
)
This is not a very efficient structure. Have you considered consolidating it in this form?
array
(
10 => 6,
11 => 2,
);
That would allow fast key lookup on ID.
TO consolidate your first array into that form, just do this:
$array2 = array();
foreach($array1 as $row)
{
if(isset($array2[$row['id']]))
$array2[$row['id']] += $row['value'];
else
$array2[$row['id']] = $row['value'];
}
Which would give you an array in the form of:
$array2 = array
(
10 => 6,
11 => 2,
);
If you really need it in your requested form, one more processing loop will get it there...
$array3 = array();
foreach($array2 as $id => $value)
{
$array3[] = array('id' => $id, 'value' => $value);
}
So, there you go!
And more compact:
$array2 = array();
foreach($array1 as $row)
$array2[$row['id']] = (isset($array2[$row['id']]) ? $array2[$row['id']] : 0) + $row['value'];
$array3 = array();
foreach($array2 as $id => $value)
$array3[] = array('id' => $id, 'value' => $value);

Categories