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,
),
)
Related
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
)
)
I have an associative array. I want to remove specific string from whole array items. Here is the structure of my array:
Array
(
[50808] => Array
(
[quantity] => 2
[name] => asv (test)
)
[50809] => Array
(
[quantity] => 2
[name] => 37 (test)
)
[50810] => Array
(
[quantity] => 3
[name] => 38 (test)
)
)
Output i want:
Array
(
[50808] => Array
(
[quantity] => 2
[name] => asv
)
[50809] => Array
(
[quantity] => 2
[name] => 37
)
[50810] => Array
(
[quantity] => 3
[name] => 38
)
)
I know its very simple using loop but i want to do it without loop.
Use array_map function
<?php
$array = Array
(
'50808' => Array
(
'quantity' => 2,
'name' => 'asv (test)',
),
'50809' => Array
(
'quantity' => 2,
'name' => '37 (test)'
),
'50810' => Array
(
'quantity' => 3,
'name' => '38 (test)'
)
);
echo '<pre>';
$new_array = array_map(function($val){
$val['name'] = trim(str_replace('(test)', '', $val['name']));
return $val;
}, $array);
print_r($new_array);
Output:
Array
(
[50808] => Array
(
[quantity] => 2
[name] => asv
)
[50809] => Array
(
[quantity] => 2
[name] => 37
)
[50810] => Array
(
[quantity] => 3
[name] => 38
)
)
Another method can be array_walk function:
array_walk($array, function(&$val){
$val['name'] = trim(str_replace('(test)', '', $val['name']));
});
print_r($array); //out put: desired output
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]);
}
I have an array looks like this:
Array
(
[0] => Array
(
[variant_name] => Green
[product_id] => 2
[variant_id] => 67
[amount] => 1000
)
[1] => Array
(
[variant_name] => Red
[product_id] => 2
[variant_id] => 68
[amount] => 0
)
)
This expected to be like this:
Array (
[2] => array (
[67] => array (
[variant_id] => Green
[amount] => 1000
(
[68] => array (
[variant_id] => Red
[amount] => 0
(
)
Which is group by product_id and then split into arrays group by variant_id.
How can I do that.
Thank you so much.
foreach($values as $product)
{
$newValues[$product["product_id"]][$product["variant_id"]]=
array(
'product_name'=>$product["variant_name"],
'amount'=>$product["amount"]
);
}
Output
Array
(
[2] => Array
(
[67] => Array
(
[product_name] => Green
[amount] => 1000
)
[68] => Array
(
[product_name] => Red
[amount] => 0
)
)
)
Note You have variant_name in your original array and you are using that as variant_id in your output. You can play with indexes in this code. I just used what seemed to match.
A simple foreach loop should suffice. Of course, you need to build them inside a new array. Consider this example: (In your expected example, I think you were referring to variant_name)
$values = array( array( 'variant_name' => 'Green', 'product_id' => 2, 'variant_id' => 67, 'amount' => 1000, ), array( 'variant_name' => 'Red', 'product_id' => 2, 'variant_id' => 68, 'amount' => 0, ), );
$new_values = array();
foreach($values as $key => $value) {
$new_values[$value['product_id']][$value['variant_id']] = array(
'variant_name' => $value['variant_name'],
'amount' => $value['amount'],
);
}
echo '<pre>';
print_r($new_values);
echo '</pre>';
Sample Output
here's the result of my first function:
Array
(
[0] => Array
(
[MaidID] => 13
[Stores] => Array
(
[0] => 14
[1] => 5
)
)
[1] => Array
(
[MaidID] => 3
[Stores] => Array
(
[0] => 4
)
)
[2] => Array
(
[MaidID] => 41
[Stores] => Array
(
[0] => 14
)
)
)
Then, here's the result of my second function:
Array
(
[1] => Array
(
[MaidID] => 14
[Cash] => 10000
[Debit] => 0
[Credit] => 0
)
)
and here's should be the result:
Array ([0] => Array (
[MaidID] => 14
[Cash] => 10000.00
[Debit] => 0
[Credit] => 0
[MaidsID] => Array(
[0] => 13
[1] => 41
)
)
)
Is it possible to make it?I need to a new key name MaidsID pointing to the list of MaidID owned by more than one Stores.Please help me, Please be patience in my question, im just a beginner.Thank you so much.
this code work ok. $a is your first array $b is the second, $c is result
$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);`
I tested it here and it was ok. (Just replace $a with your first array and $b with seccond ).
Are you sure that the structure of your arrays is exactly as you have written above?? Maybe there is any problem with that.
You have puted array inside another array? (its not needed i think)
Howerver: For this code:
`$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
print_r($a);
echo "<br><br>================================================<br><br>";
print_r($b);
echo "<br><br>================================================<br><br>";
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);
print_r($c);
echo "<br><br>================================================<br><br>";`
The output is:
Array ( [0] => Array ( [Maid] => 1 [Stores] => Array ( [0] => 1 [1] => 5 ) ) [1] => Array ( [Maid] => 3 [Stores] => Array ( [0] => 4 ) ) [2] => Array ( [Maid] => 4 [Stores] => Array ( [0] => 1 ) ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) [MaidsID] => Array ( [0] => 1 [1] => 3 [2] => 4 ) )
================================================
Isn't this how you want the result?
Have a look at this. Maybe this can help you.
$result = array_merge($array1, $array2);