combine multidimentional associative arrays with matching key value pair - php

[traffic] => Array
(
[0] => Array
(
[id] => 1
[visitors] => 310
[pageviews] => 1333
[created_date] => 2016-03-09
)
[1] => Array
(
[id] => 2
[visitors] => 374
[pageviews] => 1010
[created_date] => 2016-03-10
)
[2] => Array
(
[id] => 3
[visitors] => 143
[pageviews] => 617
[created_date] => 2016-03-11
)
)
[source] => Array
(
[0] => Array
(
[created_date] => 2016-03-09
[scount] => 368
)
[1] => Array
(
[created_date] => 2016-03-10
[scount] => 550
)
[2] => Array
(
[created_date] => 2016-03-11
[scount] => 238
)
)
I have two multidimensional arrays, I want to combine both arrays into one with matching created_date value, the result should be like this,
Array
(
[0] => Array
(
[created_date] => 2016-03-09
[id] => 1
[visitors] => 310
[pageviews] => 1333
[scount] => 368
)
[1] => Array
(
[created_date] => 2016-03-10
[id] => 2
[visitors] => 374
[pageviews] => 1010
[scount] => 550
)
[2] => Array
(
[created_date] => 2016-03-11
[id] => 3
[visitors] => 143
[pageviews] => 617
[scount] => 238
)
)

$traffic = []; //...
$source = []; // ...
foreach($traffic as $key => $value)
{
if(isset($source[$key]))
{
$token = $source[$key];
foreach($token as $keyy => $valuee)
{
if(isset($traffic[$key][$keyy]))
{
// Collision handling, if any ...
$traffic[$key][$keyy] = $valuee;
}
else $traffic[$key][$keyy] = $valuee;
}
}
}

The following code should do the trick.
The solution:
# I split your array into 2 parts ($traffic = $your_array['traffic'])
$traffic = array(
array(
id => 1,
visitors => 310,
pageviews => 1333,
created_date => '2016-03-09'
),
array(
id => 2,
visitors => 374,
pageviews => 1010,
created_date => '2016-03-10'
),
array(
id => 3,
visitors => 143,
pageviews => 617,
created_date => '2016-03-11'
)
);
# I split your array into 2 parts ($source = $your_array['source'])
$source = array(
array (
created_date => '2016-03-09',
scount => 368
),
array (
created_date => '2016-03-10',
scount => 550
),
array (
created_date => '2016-03-11',
scount => 238
)
);
# copy the traffic array cause we want to merge the new data into it
$result = $traffic;
# loop over the traffic array
foreach ($traffic as $k => $t) {
# loop over the source
foreach ($source as $s) {
# try to find a match
if ($t['created_date'] === $s['created_date']) {
# add data to result
$result[$k]['scount'] = $s['scount'];
# we exit the inner foreach-loop here as there is only 1 match
break;
}
}
}
# print the result
echo '<pre>'; print_r($result); echo '</pre>';
The result:
Array
(
[0] => Array
(
[id] => 1
[visitors] => 310
[pageviews] => 1333
[created_date] => 2016-03-09
[scount] => 368
)
[1] => Array
(
[id] => 2
[visitors] => 374
[pageviews] => 1010
[created_date] => 2016-03-10
[scount] => 550
)
[2] => Array
(
[id] => 3
[visitors] => 143
[pageviews] => 617
[created_date] => 2016-03-11
[scount] => 238
)
)
you can test it here: http://www.writephponline.com

Here is your solution:-
$arr1 = $arr1['traffic']; // assign key traffic record to array1
$arr2 = $arr2['source']; // assign key source record to array2
$result = [];
foreach($arr1 as $key=>$value){
$result[$key] = $value;
// find created_date in second array
$keyOfSecondArr = array_search($value['created_date'], array_column($arr2, 'created_date'));
$result[$key]['scount'] = $arr2[$keyOfSecondArr]['scount'];
}
echo '<pre>'; print_r($result);
output:-
Array
(
[0] => Array
(
[id] => 1
[visitors] => 310
[pageviews] => 1333
[created_date] => 2016-03-09
[scount] => 368
)
[1] => Array
(
[id] => 2
[visitors] => 374
[pageviews] => 1010
[created_date] => 2016-03-10
[scount] => 550
)
[2] => Array
(
[id] => 3
[visitors] => 143
[pageviews] => 617
[created_date] => 2016-03-11
[scount] => 238
)
)

Related

PHP Subtract multidimensional arrays based on 2 values

I need to subtract the qt from two arrays based on id and type, keeping the array complete.
How do I do in php to be able to subtract these arrays?
I have 2 arrays:
=> this is the first array with multiple key "down"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => down
[qt] => 12
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => down
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => down
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => down
[qt] => 45
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => down
[qt] => 3
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
=> this is the second array with multiple key "up"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => up
[qt] => 5
)
[1] => Array
(
[id] => 86
[loc] => 3
[type] => up
[qt] => 27
)
[2] => Array
(
[id] => 23
[loc] => 9
[type] => up
[qt] => 3
)
)
=> I need cubtract "qt" (if "id" and "loc" are the same then subtract the "qt")
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => total
[qt] => 7
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => total
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => total
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => total
[qt] => 18
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => total
[qt] => 0
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
Try this out
function findMatch($array, $id, $loc)
{
foreach ($array as $key => $item) {
if ($item["id"] == $id and $item["loc"] == $loc) {
return $key;
}
}
return false;
}
$total = [];
foreach($down as $d) {
$matched = findMatch($up, $d["id"], $d["loc"]);
if($matched !== false){
$total[] = array_replace($d, [
"type" => "total",
"qt" => ($d["qt"] - $up[$matched]["qt"])
]);
} else {
$total[] = array_replace($d, ["type" => "total"]);
}
}
print_r($total);

add sum of values to first array for each matching ID in the second array in PHP

I have two arrays like this:
Array1
$array1 = Array
(
0 => Array
(
'ID' => 101,
'Code' => 1075,
'Date' => '2012-03-03 17:13:12.433'
),
1 => Array
(
'ID' => 103,
'Code' => 175,
'Date' => '2012-09-05 20:30:02.217'
),
2 => Array
(
'ID' => 109,
'Code' => 178,
'Date' => '2012-07-05 20:30:02.217'
)
);
Array2
$array2 = Array
(
0 => Array
(
'Amount' => 1234,
'ID' => 101
),
1 => Array
(
'Amount' => 5656,
'ID' => 101
),
2 => Array
(
'Amount' => 1342,
'ID' => 103
),
3 => Array
(
'Amount' => 0,
'ID' => 0
)
);
I'm using the code below to perform a join on the two arrays :
$arr2 = array_column($array2, "ID");
$finalArray = array();
foreach($array1 as $arr){
$key = array_search($arr['ID'], $arr2);
if($key ===false){
$key = array_search(0, $arr2);
$array2[$key]['Found'] = "No";
}
else {
$array2[$key]['Found'] = "Yes";
}
unset($array2[$key]['ID']);
$finalArray[] = array_merge($arr,$array2[$key]);
}
print_r($finalArray);
The current output using the code above is :
finalArray
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[Amount] => 1234 //considers only the first entry of ID 101 in array2
[Found] => Yes
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[Amount] => 1342
[Found] => Yes
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[Amount] => 0
[Found] => No
)
)
But since in array2 there are two entries for ID 101 but the code above only takes the first match for a matching ID.
The expected output is :
Desired Output
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[Amount] => 6890 //sum of all the amounts(1234+5656)for matching ID 101
[Found] => Yes
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[Amount] => 1342
[Found] => Yes
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[Amount] => 0
[Found] => No
)
)
I'm not able to figure out how to do the addition here.
The code should do addition of the Amount feild for each matching ID of array2 and merge that amount to array1 Amount feild as shown in the expected output above.
How do I modify my current code such that it gives me the desired output?
What about:
$finalArray = array();
foreach ($array1 as $arr1)
{
$amount = 0;
foreach ($array2 as $key2 => $arr2)
{
if ($arr1['ID'] === $arr2['ID'])
{
$amount += $arr2['Amount'];
unset($array2[$key2]);
}
}
$finalArray[] = array_merge($arr1, array(
'Amount' => $amount,
'Found' => $amount ? "Yes" : "No"
));
}
print_r($finalArray);
Output:
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[amount] => 6890
[found] => Yes
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[amount] => 1342
[found] => Yes
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[amount] => 0
[found] => No
)
)
Let me know if you need any further explanation.

How to convert array to Multidimensional array and push a value

I have two multiple select input result that i want to join. Its about delivery destination and delivery fee.
here is my array result :
Array
(
[destination] => Array
(
[0] => London
[1] => Liverpool
[2] => Nottingham
[3] => Oxford
)
[fee] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
)
)
And I want to push these value to each array :
$status = "1";
Result I expected is :
Array
(
[0] => Array
(
[destination] => London
[fee] => 10
[status] => 1
)
[1] => Array
(
[destination] => Liverpool
[fee] => 15
[status] => 1
)
[2] => Array
(
[destination] => Nottingham
[fee] => 20
[status] => 1
)
[3] => Array
(
[destination] => Oxford
[fee] => 25
[status] => 1
)
)
Thanks for helping me.
$array = ['destination' => ['London', 'Liverpool', 'Nottingham', 'Oxford'], 'fee' => [10, 15, 20, 25]];
$result = [];
foreach ($array['destination'] as $index => $value)
{
$result[] = ['destination' => $value, 'fee' => $array['fee'][$index], 'status' => 1];
}
Try this: https://3v4l.org/004PF
<?php
$givenArray = [
'destination' => [
'London',
'Liverpool',
'Nottingham',
'Oxford',
],
'fee' => [
10,
15,
20,
25
],
];
$output = [];
foreach ($givenArray['destination'] as $key => $destination) {
$fee = $givenArray['fee'][$key];
$output[] = [
'destination' => $destination,
'fee' => $fee,
'status' => 1,
];
}
print_r($output);
Output is:
Array
(
[0] => Array
(
[destination] => London
[fee] => 10
[status] => 1
)
[1] => Array
(
[destination] => Liverpool
[fee] => 15
[status] => 1
)
[2] => Array
(
[destination] => Nottingham
[fee] => 20
[status] => 1
)
[3] => Array
(
[destination] => Oxford
[fee] => 25
[status] => 1
)
)

Sort a multidimensional an array with numeric keys but keep the keys same just change the order [duplicate]

This question already has an answer here:
PHP sort associative array by numeric key in asc order [duplicate]
(1 answer)
Closed 10 months ago.
So I have 3 dimensional array. I want that array to be reordered based on the keys but the value of the keys should remain as it is. Like for an example if the array keys are 5,2,4,1,3 then it should become 1,2,3,4,5. Below I'm providing the array I have and excepted array and the solutions I have tried.
This is the array I have :-
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
Following are the solutions I have tried :-
$result = ksort($result);
$result = array_values($result);
$result = array_splice($result, 0, 0);
$result = sort($result);
$result = array_splice($result, 0, count($result));
This is the expected array :-
Array
(
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
)
Nothing is working any help will be appreciated. thanks in advance.
You are using ksort as $result = ksort($result);, ksort return TRUE/FALSE. That means you are assigning that to $results.
Read here PHP ksort
Your code should be:-
ksort($results);
instead of
$result = ksort($result);
You can use ksort for the keys sorting, here is an example
$arr = [
5 => [1,3],
3 => [2,3],
2 => [0,7]
];
ksort($arr);
echo '<pre>';
print_r($arr);
Output
Array
(
[2] => Array
(
[0] => 0
[1] => 7
)
[3] => Array
(
[0] => 2
[1] => 3
)
[5] => Array
(
[0] => 1
[1] => 3
)
)

Sort array based on count of any value

I have an array which looks like
Array
(
[0] => Array
(
[id] => 39662
[points] => 24
[subject] => 112
)
[1] => Array
(
[id] => 39609
[points] => 24
[subject] => 87
)
[2] => Array
(
[id] => 39610
[points] => 23
[subject] => 77
)
[3] => Array
(
[id] => 39608
[points] => 23
[subject] => 87
)
[4] => Array
(
[id] => 39606
[points] => 22
[subject] => 60
)
[5] => Array
(
[id] => 39604
[points] => 19
[subject] => 75
)
[6] => Array
(
[id] => 39595
[points] => 18
[subject] => 60
)
[7] => Array
(
[id] => 39605
[points] => 18
[subject] => 47
)
[8] => Array
(
[id] => 39650
[points] => 17
[subject] => 87
)
[9] => Array
(
[id] => 39660
[points] => 17
[subject] => 55
)
)
Now I want to sort then based on count of key subject. You can see that subjuet = 87 have 3 records and subject = 60 has two records, so all three records of 87 should display first , after this records of 60 , then others.
I tried array_multisort but its not giving expected result.
Thanks
As per your desired output, you just need to array_map() with array_multisort(),
Example:
<?php
// Test Array
$array = array(
array('id'=>39662,'points'=>'24','subject'=>112),
array('id'=>39609,'points'=>'24','subject'=>87),
array('id'=>39610,'points'=>'23','subject'=>77),
array('id'=>39608,'points'=>'23','subject'=>87),
array('id'=>39606,'points'=>'22','subject'=>60),
array('id'=>39604,'points'=>'19','subject'=>75),
array('id'=>39595,'points'=>'18','subject'=>60),
array('id'=>39605,'points'=>'18','subject'=>47),
array('id'=>39650,'points'=>'17','subject'=>87),
array('id'=>39660,'points'=>'17','subject'=>55),
);
$newArr = array(); // initialize the new Array
foreach ($array as $key => $value)
{
$newArr[$value['subject']][] = $value;
}
array_multisort(array_map('count', $newArr), SORT_DESC, $newArr); // using array_multisort() and Sort as DESC order by using array_map()
echo "<pre>";
print_r($newArr);
?>
Result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 39609
[points] => 24
[subject] => 87
)
[1] => Array
(
[id] => 39608
[points] => 23
[subject] => 87
)
[2] => Array
(
[id] => 39650
[points] => 17
[subject] => 87
)
)
[1] => Array
(
[0] => Array
(
[id] => 39606
[points] => 22
[subject] => 60
)
[1] => Array
(
[id] => 39595
[points] => 18
[subject] => 60
)
)
[2] => Array
(
[0] => Array
(
[id] => 39604
[points] => 19
[subject] => 75
)
)
[3] => Array
(
[0] => Array
(
[id] => 39605
[points] => 18
[subject] => 47
)
)
[4] => Array
(
[0] => Array
(
[id] => 39610
[points] => 23
[subject] => 77
)
)
[5] => Array
(
[0] => Array
(
[id] => 39660
[points] => 17
[subject] => 55
)
)
[6] => Array
(
[0] => Array
(
[id] => 39662
[points] => 24
[subject] => 112
)
)
)
Try the following method for your case
$data[] = array('points' => 67, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 1);
$data[] = array('points' => 85, 'subject' => 6);
$data[] = array('points' => 98, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 6);
$data[] = array('points' => 67, 'subject' => 7);
// Obtain a list of columns
foreach ($data as $key => $row) {
$subject[$key] = $row['subject'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($subject, SORT_DESC, $data);
Example usage with my array from the php manual.
Output of the above was
Array
(
[0] => Array
(
[points] => 67
[subject] => 7
)
[1] => Array
(
[points] => 85
[subject] => 6
)
[2] => Array
(
[points] => 86
[subject] => 6
)
[3] => Array
(
[points] => 67
[subject] => 2
)
[4] => Array
(
[points] => 98
[subject] => 2
)
[5] => Array
(
[points] => 86
[subject] => 1
)
)
function array_sort($array, $key){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $key) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
asort($sortable_array);
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
array_sort($a, 'subjects');
//$a is array which want to sort

Categories