Merging elements of two different arrays to third array [duplicate] - php

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

Related

How do I combine these two arrays in PHP?

I have these two arrays:
Page Views
array:5 [▼
0 => array:3 [▼
"lp_group" => "store"
"hour" => 15
"views" => 1
]
1 => array:3 [▼
"lp_group" => "teaser"
"hour" => 15
"views" => 2
]
2 => array:3 [▼
"lp_group" => "store"
"hour" => 17
"views" => 1
]
3 => array:3 [▼
"lp_group" => "teaser"
"hour" => 17
"views" => 3
]
4 => array:3 [▼
"lp_group" => "store"
"hour" => 21
"views" => 1
]
]
Button Clicks
array:3 [▼
0 => array:3 [▼
"lp_group" => "store"
"hour" => 15
"clicks" => 3
]
1 => array:3 [▼
"lp_group" => "teaser"
"hour" => 15
"clicks" => 3
]
2 => array:3 [▼
"lp_group" => "teaser"
"hour" => 17
"clicks" => 1
]
]
I'd like to get this:
array:5 [▼
0 => array:3 [▼
"lp_group" => "store"
"hour" => 15
"views" => 1
"clicks" => 3
]
1 => array:3 [▼
"lp_group" => "teaser"
"hour" => 15
"views" => 2
"clicks" => 3
]
2 => array:3 [▼
"lp_group" => "store"
"hour" => 17
"views" => 1
]
3 => array:3 [▼
"lp_group" => "teaser"
"hour" => 17
"views" => 3
"clicks" => 1
]
4 => array:3 [▼
"lp_group" => "store"
"hour" => 21
"views" => 1
]
]
In case you don't see it, one of the arrays (for each hour/lp_group) counts views the other clicks. I'd like to render these in a table, so I need to "combine" them into one array.
Neither array_merge nor array_merge_recursive work properly. And all the others I've tried overwrite data.
Define your two arrays as $ar1 and $ar2 and try code below:
$ar = array_map(function ($ar1, $ar2){
if ($ar1['lp_group'] == $ar2['lp_group'] && $ar1['hour'] == $ar2['hour']) {
$ar1['clicks'] = $ar2['clicks'];
}
return $ar1;
}, $ar1, $ar2);

how to get first key (0 or associative ) form array and output in new array

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

Need to make new array

Need Help to make new array using two array.
One array is
array:2 [▼
"Date Date 2018-07-23" => array:1 [▼
6 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
]
"Date Date 2018-07-28" => array:3 [▼
1 => array:2 [▼
"avg" => 0.031472222222222
"line_n0" => 1
]
4 => array:2 [▼
"avg" => 0.011111111111111
"line_n0" => 4
]
5 => array:2 [▼
"avg" => 0.011111111111111
"line_n0" => 5
]
]
]
and another array is
array:6 [▼
1 => array:2 [▼
"avg" => 0
"line_n0" => 1
]
2 => array:2 [▼
"avg" => 0
"line_n0" => 2
]
3 => array:2 [▼
"avg" => 0
"line_n0" => 3
]
4 => array:2 [▼
"avg" => 0
"line_n0" => 4
]
5 => array:2 [▼
"avg" => 0
"line_n0" => 5
]
6 => array:2 [▼
"avg" => 0
"line_n0" => 6
]
array:2 [▼
"Date Date 2018-07-23" => array:1 [▼
1 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
2 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
3 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
4 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
5 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
6 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
]
"Date Date 2018-07-28" => array:3 [▼
1 => array:2 [▼
"avg" => 0.031472222222222
"line_n0" => 1
]
2 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
3 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
4 => array:2 [▼
"avg" => 0.011111111111111
"line_n0" => 4
]
5 => array:2 [▼
"avg" => 0.011111111111111
"line_n0" => 5
]
6 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
]
]
Please help me how i will combine the array ,I want to push 2nd array element if not fund in first array key.I trying but not clear to make new array. I have tried array merge and array combine but not clear to make this new array.
Use array_merge() function Example below
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Output like this:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)

array_merge_recursive on multidimensional array

Please how do i apply array_merge_recursive on the code below
<?php
array:4 [▼
0 => array:1 [▼
1 => "1_1"
]
1 => array:1 [▼
1 => "1_2"
]
2 => array:1 [▼
2 => "2_1"
]
3 => array:1 [▼
2 => "2_2"
]
]
?>
I need the output to be
<?php
array:2 [▼
0 => array:2 [▼
0 => "1_1",
1 => "1_2"
]
1 => array:2 [▼
0 => "2_1",
1 => "2_2"
]
]
?>
Note: the inner array might increase from 4 to 1000
Thank you.
Short and to the point.
$array = [
0 => [
0 => '1_1',
],
1=> [
0 => '1_2',
],
2 => [
0 => '2_1',
],
3 => [
0 => '2_2',
],
];
foreach(array_chunk($array, 2) as $value){
print_r(array_merge(array_shift($value), array_pop($value)));
}
Get a chunk of 2 from the big array. Loop over the chunks, get the first element in the array and the last element and merge them.
Result:
Array
(
[0] => 1_1
[1] => 1_2
)
Array
(
[0] => 2_1
[1] => 2_2
)
Remove the print_r and adapt to your own needs.
array_merge_recursive can also be used instead of array_merge but it this case it does exactly the same thing. You'd still need to somehow get the chunks.

Reducing and combining array

I am trying to create an array that succinctly gives me the following:
ID of a component part
ID of the supplier for that component part
The volume breakpoints, and their associated unit costs
I am using Laravel 5.2, although this is a more general PHP question.
So, I have a database table that looks like this:
I have a function, as below, to get some prices of components:
public function get_component_prices()
{
$components = DB::table('component_supplier')
->select('component_id', 'supplier_id', 'volume', 'unit_cost')
->get();
$prices = [];
foreach ($components as $component) {
array_push($prices, ["component_id" => $component->component_id, "supplier_id" => $component->supplier_id, "volumes" => [$component->volume => $component->unit_cost]]);
}
dd($prices);
}
This gives me the array:
array:7 [▼
0 => array:3 [▼
"component_id" => 3
"supplier_id" => 1
"volumes" => array:1 [▼
100 => "1.5000"
]
]
1 => array:3 [▼
"component_id" => 3
"supplier_id" => 1
"volumes" => array:1 [▼
207 => "1.0100"
]
]
2 => array:3 [▼
"component_id" => 3
"supplier_id" => 1
"volumes" => array:1 [▼
500 => "0.8000"
]
]
3 => array:3 [▼
"component_id" => 3
"supplier_id" => 1
"volumes" => array:1 [▼
1000 => "0.4000"
]
]
4 => array:3 [▼
"component_id" => 3
"supplier_id" => 2
"volumes" => array:1 [▼
10000 => "0.2000"
]
]
5 => array:3 [▼
"component_id" => 4
"supplier_id" => 2
"volumes" => array:1 [▼
100 => "0.1000"
]
]
6 => array:3 [▼
"component_id" => 4
"supplier_id" => 2
"volumes" => array:1 [▼
500 => "0.0700"
]
]
]
You can see that certain suppliers and components have multiple volumes.
Therefore, I'd like to try to group the array a bit better, combining the repeated parts - perhaps like so, for example:
6 => array:3 [▼
"component_id" => 4
"supplier_id" => 2
"volumes" => array:3 [▼
100 => "0.1000",
500 => "0.0700"
]
]
So that for each component_id and supplier_id group, there's a set of 'volumes'.
Any advice is much appreciated... I have been trying for hours to get the array sorted!
In Mysql, you can do something like this
SELECT component_id, supplier_id,
GROUP_CONCAT(volume, ':', unit_cost) AS volumes
FROM component_supplier
GROUP BY CONCAT(component_id, supplier_id)
http://sqlfiddle.com/#!9/cb7bb/1
Then you can simply loop this query in PHP and explode volumes field by the comma.

Categories