I have a multidimensional array as
array:3 [▼
0 => array:3 [▼
"product_id" => "8"
"qty" => 1
]
1 => array:3 [▼
"product_id" => "9"
"qty" => 2
]
]
and I would like to merge a static associative array ['invoice_id' => 1] in to all the arrays in the multidimensional array. and the end result should be something like this
array:3 [▼
0 => array:3 [▼
"product_id" => "8"
"qty" => 1,
"invoice_id" => 1
]
1 => array:3 [▼
"product_id" => "9"
"qty" => 2,
"invoice_id" => 1
]
]
is there a way to do this with out looping through the multidimensional array
Try using Array_Map
suppose Array data store in $testArray variable
$testArray= array_map(function($arr){
return $arr + ['invoice_id' => 1];
}, $testArray);
Related
I have 3 arrays that I need to merge together but can't figure out how?
array 1
array:4 [▼
0 => "admin98#wassiah.test"
1 => "admin69#wassiah.test"
2 => "admin25#wassiah.test"
3 => null
]
array 2
array:4 [▼
0 => "one"
1 => "three"
2 => "two"
3 => null
]
array 3
array:4 [▼
0 => "10"
1 => "11"
2 => null
3 => null
]
And I need to make new array like this:
array:4 [▼
0 => array:2 [▼
"email" => "admin98#wassiah.test"
"name" => "one"
"id" => "10"
]
1 => array:2 [▼
"email" => "admin69#wassiah.test
"name" => "three"
"id" => "11"
]
2 => array:2 [▼
"email" => "admin25#wassiah.test"
"name" => "two"
"id" => null
]
3 => array:2 [▼
"email" => null
"name" => null
"id" => null
]
]
Code
$mails = $request->input('mails'); // array 1
$names = $request->input('names'); // array 2
$heirIds = $request->input('ids'); // array 3
Any idea?
You can try the below code
<?php
$arr1 = array(
"admin98#wassiah.test",
"admin97#wassiah.test",
"admin96#wassiah.test",
"",
);
$arr2 = array(
"one",
"three",
"two",
"",
);
$arr3 = array(
"10",
"11",
"",
"",
);
$result = array();
$count = count($arr1);
for($i=0;$i<$count;$i++){
$result[$i]['email'] = $arr1[$i];
$result[$i]['name'] = $arr2[$i];
$result[$i]['id'] = $arr3[$i];
}
echo '<pre>'; print_r($result);
i have an array that contains another array and i wanna get specifics elements sum
Array
array:1 [▼
0 => array:3 [▼
"shop_id" => 1
"delegate_id" => 2
"items" => array:3 [▼
0 => array:3 [▼
"product_id" => 2
"price" => 4.75
"quantity" => 1
]
1 => array:3 [▼
"product_id" => 6
"price" => 18.5
"quantity" => 1
]
2 => array:3 [▼
"product_id" => 10
"price" => 88
"quantity" => 2
]
]
]
]
i need to get the sum of price
any ideas to do this?
If there is relationships there is a better approach. As the data you have given you could call sum twice and make it work.
collect($array)->sum(function (array $shop) {
return collect($shop['items'])->sum('price');
});
This will sum each individual shop and the sum amount would be the sum of each shops items.
I Would Like To Get The First Element Of This Array And Put In New Same Array Output
One Requirement: It Cannot Be Done With Passing By reference Index eg 0
This Input Array
[ 'id','ID','dt-text' ] ,
[ 'name','Name','dt-text' ] ,
[ 'artistList'=>['list','mm','defalut'] ,'Artist List','dt-select'] ,
[ 'nationality'=>['nationality','mm','defalut'] ,'Nationality','dt-select'] ,
[ 'view','View',''],
[ 'status','Status' ,'']
array:6 [▼
0 => array:3 [▼
0 => "id"
1 => "ID"
2 => "dt-text"
]
1 => array:3 [▼
0 => "name"
1 => "Name"
2 => "dt-text"
]
2 => array:3 [▼
"artistList" => array:3 [▼
0 => "list"
1 => "mm"
2 => "defalut"
]
0 => "Artist List"
1 => "dt-select"
]
3 => array:3 [▼
"nationality" => array:3 [▼
0 => "nationality"
1 => "mm"
2 => "defalut"
]
0 => "Nationality"
1 => "dt-select"
]
4 => array:3 [▼
0 => "view"
1 => "View"
2 => ""
]
5 => array:3 [▼
0 => "status"
1 => "Status"
2 => ""
]
]
The New Array I Needed
This IS OutPUT Array
['id','name','artistList'=>['list','mm','defalut'] ,'nationality'=>['nationality','mm','defalut'] ,'view','status']
array:6 [▼
0 => "id"
1 => "name"
"artistList" => array:3 [▼
0 => "list"
1 => "mm"
2 => "defalut"
]
"nationality" => array:3 [▼
0 => "nationality"
1 => "mm"
2 => "defalut"
]
2 => "view"
3 => "status"
]
Note
I Can Controll in Input Array Same , I Try with foreach in php And Tray In Laravel Helper Function head Put I get S
array:6 [▼
0 => "id"
1 => "name"
2 => array:1 [▼
"artistList" => array:3 [▼
0 => "list"
1 => "mm"
2 => "defalut"
]
]
3 => array:1 [▼
"nationality" => array:3 [▼
0 => "nationality"
1 => "mm"
2 => "defalut"
]
]
4 => "view"
5 => "status"
]
Put I Cant Get Resslut So , How Can I Do this?
Since you are changing the keys (structure) of the array, there is no way to do that without either generating a new array or passing the array by reference. One way to do it by generating a new array is with array_reduce:
$array = [
[ 'id','ID','dt-text' ] ,
[ 'name','Name','dt-text' ] ,
[ 'artistList'=>['list','mm','defalut'] ,'Artist List','dt-select'] ,
[ 'nationality'=>['nationality','mm','defalut'] ,'Nationality','dt-select'] ,
[ 'view','View',''],
[ 'status','Status' ,'']
];
$array = array_reduce($array, function ($c, $v) {
$first_key = array_keys($v)[0];
return array_merge($c, array($first_key => $v[$first_key])); }, []);
print_r($array);
Output:
Array (
[0] => id
[1] => name
[artistList] => Array (
[0] => list
[1] => mm
[2] => defalut
)
[nationality] => Array (
[0] => nationality
[1] => mm
[2] => defalut
)
[2] => view
[3] => status
)
Demo on 3v4l.org
This question already has answers here:
Merge rows of data from two 2D arrays (each with a shared and a unique column) based on the shared column value
(3 answers)
Closed 5 months ago.
I wish you could help me ...
I have 2 arrays, which I would like to insert into a 1array, by new_date to which if there is no data is data = '0', if there is no data1 is data1 = '0'.
Sorry if I do not know how to explain it well, I gave a small example. I have already tried array_merge (but everything is separate) and array_combine (it gives an error of Both parameters should have an equal number of elements).
Thank you in advance for all the help you can give ...
array1 = array:5 [▼
0 => array:2 [▼
"data" => 118
"new_date" => "06-2017"
]
1 => array:2 [▼
"data" => 263
"new_date" => "07-2017"
]
2 => array:2 [▼
"data" => 264
"new_date" => "08-2017"
]
3 => array:2 [▼
"data" => 266
"new_date" => "09-2017"
]
4 => array:2 [▼
"data" => 306
"new_date" => "10-2017"
]
5 => array:2 [▼
"data" => 100
"new_date" => "11-2017"
]
array2 = array:6 [▼
0 => array:2 [▼
"data1" => 100
"new_date" => "02-2016"
]
1 => array:2 [▼
"data1" => 170
"new_date" => "06-2017"
]
2 => array:2 [▼
"data1" => 354
"new_date" => "07-2017"
]
3 => array:2 [▼
"data1" => 397
"new_date" => "08-2017"
]
4 => array:2 [▼
"data1" => 421
"new_date" => "09-2017"
]
5 => array:2 [▼
"data1" => 531
"new_date" => "10-2017"
]
Exemple:
array3 = array:7 [▼
0 => array:3 [▼
"data1" => 111
"data" => 0
"new_date" => "02-2016"
]
1 => array:3 [▼
"data1" => 170
"data" => 118
"new_date" => "06-2017"
]
2 => array:3 [▼
"data1" => 354
"data" => 263
"new_date" => "07-2017"
]
3 => array:3 [▼
"data1" => 397
"data" => 264
"new_date" => "08-2017"
]
4 => array:3 [▼
"data1" => 421
"data" => 266
"new_date" => "09-2017"
]
5 => array:3 [▼
"data1" => 531
"data" => 306
"new_date" => "10-2017"
]
6 => array:3 [▼
"data1" => 0
"data" => 100
"new_date" => "11-2017"
]
Use array_combine to set the new_date as keys. Then add the first array element the d1 (as "data1" in your example) with array_map. Finely, loop on second array and add d1 is exist and all the rest with d ("data" in your example) as 0 if not.
You can do it like this:
$a1 = array(["d"=>1, "new_date"=> "06-2017"],["d"=>2, "new_date"=> "02-2016"]);
$a2 = array(["d1"=>3, "new_date"=> "06-2017"],["d1"=>4, "new_date"=> "07-2017"]);
$a1 = array_combine(array_column($a1, "new_date"), $a1);
$a2 = array_combine(array_column($a2, "new_date"), $a2);
// adding default d1 as 0
$a1 = array_map(function ($e) {return array_merge($e, ["d1" => 0]);},$a1);
foreach($a2 as $k => $v) {
if (isset($a1[$k]))
$a1[$k]["d1"] = $v["d1"]; // if new_data exist set only d1
else
$a1[$k] = array_merge($v, ["d" => 0]); //add with d as 0
}
Your result will be in $a1
I have the following arrays:
array:2 [▼
0 => array:2 [▼
"id" => 3
"total" => 2
]
1 => array:2 [▼
"id" => 4
"total" => 1
]
]
and
array:2 [▼
0 => array:2 [▼
"id" => 3
"total" => 5
]
1 => array:2 [▼
"id" => 4
"total" => 5
]
2 => array:2 [▼
"id" => 5
"total" => 2
]
]
I need to merge them together, keeping ids and summing the results, so I would get the resulting array as:
array:2 [▼
0 => array:2 [▼
"id" => 3
"total" => 7
]
1 => array:2 [▼
"id" => 4
"total" => 6
]
2 => array:2 [▼
"id" => 5
"total" => 2
]
]
These arrays come from a laravel database query using the toArray() method, so answers containing laravel default collection methods are welcome too.
try this
arr3 = arr1->merge(arr2);
arr3->map(function(itemM)use(arr1, arr2){
return itemM->total = arr1->sum(function(itemS)use(itemM){
return itemS->id == itemM->id ? itemS->total : 0;
}) +
arr2->sum(function(itemS)use(itemM){
return itemS->id == itemM->id ? itemS->total : 0;
});
})