Transforming array values in elements of a subarray using PHP - php

I would like to merge two arrays in a way that values of first array are transformed in first elements of the subarray and values of the second array are transformed in second elements of the subarray. Here my example:
Array01
(
[0] => 41558194
[1] => 44677841
[2] => 44503689
[3] => 40651770
)
Array02
(
[0] => 551
[1] => 546
[2] => 531
[3] => 519
)
mergedArray
(
[0] => Array([0] => 41558194 [1] => 551)
[1] => Array([0] => 44677841 [1] => 546)
[2] => Array([0] => 44503689 [1] => 531)
[3] => Array([0] => 40651770 [1] => 519)
)
What is the most efficient way to do this?
Many thanks in advance!

Here is a concise example using array_map:
function merge_arrays($a1, $a2) {
return array($a1, $a2);
}
$result = array_map("merge_arrays", $arr, $arr2);

An example with your values :
$array1 = array(
0 => 41558194,
1 => 44677841,
2 => 44503689,
3 => 40651770
);
$array2 = array(
0 => 551,
1 => 546,
2 => 531,
3 => 519
);
$finalArray = array();
foreach ($array1 as $key1 => $value1) {
$finalArray[$key1] = array($value1, $array2[$key1]);
}

Related

combine 2 array with same key into 1 array

I have 2 Array:
$arr1 = Array (
[2] => Array ( [0] => 41000 [1] => 31079 )
[3] => Array ( [0] => 42963 [1] => 41189 )
)
$arr2 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 )
[3] => Array ( [0] => 42998 [1] => 34567 )
)
I want to combine these two arrays to this array with same key:
$arr3 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 [2] => 41000 [3] => 31079 )
[3] => Array ( [0] => 42998 [1] => 34567 [2] => 42963 [3] => 41189 )
)
I tried almost anything (merge, combine, join) but I can't figure it out. Any suggestions?
This is a nice and easy thing to do.
$arr1 = [2 => [0 => 41000, 1 => 31079], 3 => [0 => 42963, 1 => 41189]];
$arr2 = [2 => [0 => 40213, 1 => 42054], 3 => [0 => 42998, 1 => 34567]];
function addArray(&$bucket, $water)
{
foreach ($water as $key => $drop) {
$bucket[$key] = array_merge($bucket[$key] ?? [], $drop);
}
}
$combined = [];
addArray($combined, $arr1);
addArray($combined, $arr2);
var_export($combined);
See a PHP fiddle here.
The output is:
array (
2 =>
array (
0 => 41000,
1 => 31079,
2 => 40213,
3 => 42054,
),
3 =>
array (
0 => 42963,
1 => 41189,
2 => 42998,
3 => 34567,
),
)
How does it work? I wrote a little function that would add the input arrays to an array that combines all arrays.
$arr1 = array(
2 => array(41000, 31079),
3 => array(42963, 41189)
);
$arr2 = array(
2 => array(40213, 42054),
3 => array(42998, 34567)
);
$arr3 = array();
foreach($arr1 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
foreach($arr2 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
echo '<pre>';
print_r($arr3);
Prints:
Array
(
[2] => Array
(
[0] => 41000
[1] => 31079
[2] => 40213
[3] => 42054
)
[3] => Array
(
[0] => 42963
[1] => 41189
[2] => 42998
[3] => 34567
)
)

PHP push value array from one in another

i have two arrays and want to get one
//first
[0] => Array (
[drink] => A
[litres] => 100.05
)
[1] => Array (
[drink] => B
[litres] => 3943
)
[2] => Array (
[drink] => C
[litres] => 1.46
)
//second
[0] => 22
[1] => 12
[2] => 16
The result should be:
[0] => Array (
[drink] => A
[litres] => 100.05
[price] => 22
)
[1] => Array (
[drink] => B
[litres] => 3943
[price] => 12
)
[2] => Array (
[drink] => C
[litres] => 1.46
[price] => 16
)
i tried with loops , merge etc all i should find on this site. But i dont get it.
foreach ($arr as $array2) {
$array1[] = array( 'price' => $array2 );
}
print_r($array1);
i dont get it
Thanks in advance
$result = array_map(function($i, $j) { return array_merge($i, array('price'=>$j)); },
$arr1, $arr2);
demo on eval.in
Short solution using array_map and array_merge functions:
$result = array_map(function($a, $b){
return array_merge($a, ['price'=>$b]);
}, $arr1, $arr2);
print_r($result);
The output:
Array
(
[0] => Array
(
[drink] => A
[litres] => 100.05
[price] => 22
)
[1] => Array
(
[drink] => B
[litres] => 3943
[price] => 12
)
[2] => Array
(
[drink] => C
[litres] => 1.46
[price] => 16
)
)
The other answers are correct methods, I just want to show how it can be done with a foreach() loop:
Code:
$a=[
["drink"=>"A","litres"=>100.05],
["drink"=>"B","litres"=>3943],
["drink"=>"C","litres"=>1.46]
];
$b=[22,12,16];
foreach($a as $i=>$subarray){
$a[$i]["price"]=$b[$i];
}
var_export($a);
This method is not a one-liner but will run slightly faster because it has fewer function calls and it will also be easier to read by most people. (Though I'll admit -- I lean toward one-liners) Note, my method doesn't declare a new result variable, it just extends the first array.
Output (as desired):
array (
0 =>
array (
'drink' => 'A',
'litres' => 100.05,
'price' => 22,
),
1 =>
array (
'drink' => 'B',
'litres' => 3943,
'price' => 12,
),
2 =>
array (
'drink' => 'C',
'litres' => 1.46,
'price' => 16,
),
)

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?

PHP Compare two nested arrays to get a new array

I am trying to subtract parts of one nested array from another, but I'm having difficulty specifying the parts that I want to subtract as both values are numbers.
My arrays are, for example:
Array ( [0] => Array ( [id] => 43 [quantity] => 4 ) )
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
And after the subtraction I want the Result to be:
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
I'm using the following code to perform the subtraction, but I can't stop it from subtracting the id from itself:
foreach(array_keys($arrayA) as $id)
{
foreach(array_keys($arrayA[$id]) as $type)
{
$newArray[$id][$type] = $arrayA[$id][$type] - $arrayB[$id][$type];
}
}
print_r($newArray);
Could someone please tell me how I can just effect the [quantity] part of the array, without changing the [id]? With the code as it is I get:
Array ( [0] => Array ( [id] => 0 [quantity] => 2 ) )
Thanks in advance.
$ar1 = array(0 => array('id' => 43, 'quantity' => 4));
$ar2 = array(0 => array('id' => 43, 'quantity' => 2));
$new_array = array();
foreach($ar1 as $key => $value)
{
$new_array[$key] = array('id' => $value['id'], 'quantity' => ($value['quantity'] - $ar2[$key]['quantity']));
}
Array
(
[0] => Array
(
[id] => 43
[quantity] => 2
)
)

Remove duplicate elements off a multi-dimension array

I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke

Categories