How to convert array to Multidimensional array and push a value - php

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
)
)

Related

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.

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
)
)

PHP sum array values with same keys

This is the original main array:
Array
(
[0] => Array
(
[subtotal] => 0.6000
[taxes] => 0.0720
[charged_amount] => 0.6720
[total_discount] => 0.0000
[provinceName] => BC
[store_key] => 1
[store_id] => 5834
[categories] => Array
(
[2] => 0.6000
[4] => 0
[3] => 0
)
)
[1] => Array
(
[subtotal] => 29.8500
[taxes] => 2.3270
[charged_amount] => 20.2370
[total_discount] => 11.9400
[provinceName] => MB
[store_key] => 9
[store_id] => 1022
[categories] => Array
(
[2] => 0
[4] => 29.8500
[3] => 0
)
)
[2] => Array
(
[subtotal] => 0.3000
[taxes] => 0.0390
[charged_amount] => 0.3390
[total_discount] => 0.0000
[provinceName] => NB
[store_key] => 8
[store_id] => 1013
[categories] => Array
(
[2] => 0.3000
[4] => 0
[3] => 0
)
)
[3] => Array
(
[subtotal] => 24.3100
[taxes] => 1.1830
[charged_amount] => 10.2830
[total_discount] => 15.2100
[provinceName] => NL
[store_key] => 4
[store_id] => 3033
[categories] => Array
(
[2] => 24.3100
[4] => 0
[3] => 0
)
)
[4] => Array
(
[subtotal] => 1116.3400
[taxes] => 127.6960
[charged_amount] => 1110.0060
[total_discount] => 134.0300
[provinceName] => ON
[store_key] => 2
[store_id] => 1139
[categories] => Array
(
[2] => 85.7300
[4] => 143.2800
[3] => 887.3300
)
)
[5] => Array
(
[subtotal] => 10.8500
[taxes] => 1.4100
[charged_amount] => 12.2600
[total_discount] => 0.0000
[provinceName] => ON
[store_key] => 5
[store_id] => 1116
[categories] => Array
(
[2] => 10.8500
[4] => 0
[3] => 0
)
)
)
I just need to add the values of the array [categories] with same keys and use it further to print the total, but not getting correct output, can someone help me out to get the desired result:
Desired result
An array with same keys but total of individual array values
Array ( [2] => 0.9000 [4] => 29.8500 [3] => 1.5 )
NOTE: Initial array is dynamic can have n number of key value pair
Thanks
The first thing that you need to do is iterate through the outer array. Then, for each row in the outer array, you and to iterate through each entry in the category element. So this means that we have two foreach loops. Inside the inner foreach, we simply set the value for the current index to be the value of the same index on a 'sum' array (if it doesn't already exist), or increment the value of that index if it already exists in the 'sum' array.
<?php
$sumArray = array();
foreach($outerArray as $row)
{
foreach($row["categories"] as $index => $value)
{
$sumArray[$index] = (isset($sumArray[$index]) ? $sumArray[$index] + $value : $value);
}
}
?>
Demo using your example array

combine multidimentional associative arrays with matching key value pair

[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
)
)

php array data collect from middle if range match

How can I collect array from B1->price[4] to E1->price[6] dynamically.
Array
(
[A1] => Array
(
[price] => 3
[duration] => 1
)
[B1] => Array
(
[price] => 4
[duration] => 3
)
[C1] => Array
(
[price] => 5
[duration] => 2
)
[D1] => Array
(
[price] => 6
[duration] => 3
)
[E1] => Array
(
[price] => 6
[duration] => 2
)
[F1] => Array
(
[price] => 7
[duration] => 3
)
)
Array
(
[A1] => Array
(
[price] => 3
[duration] => 1
)
[B1] => Array
(
[price] => 4
[duration] => 3
)
[C1] => Array
(
[price] => 5
[duration] => 2
)
[D1] => Array
(
[price] => 6
[duration] => 3
)
[E1] => Array
(
[price] => 6
[duration] => 2
)
[F1] => Array
(
[price] => 7
[duration] => 3
)
)
Use this for your result
$arrayName =
array(
'A1' => array('price' => 3,'duration' => 1),
'B1' => array('price' => 4,'duration' => 3),
'C1' => array('price' => 5,'duration' => 2),
'D1' => array('price' => 6,'duration' => 3),
'E1' => array('price' => 6,'duration' => 2),
'F1' => array('price' => 7,'duration' => 3)
);
echo '<pre>';
print_r(array_slice($arrayName, 1, -1)) ;
echo '</pre>';

Categories