How to concatenate two array in php - php

How to concatenate two arrays to a single array? In date position 0 and 1 are both concatenated in loop, my array code below here.
Array
(
[Apr-2019] => Array
(
[0] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4.00
)
[1] => Array
(
[current_deposit_amount] => 1.00
[current_deposit_table_refer] => 0.00
[current_deposit_user_refer] => 0.10
[DateUser] => Apr-2019
)
)
)
like my output:
[Apr-2019] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4.00
[current_deposit_amount] => 1.00
[current_deposit_table_refer] => 0.00
[current_deposit_user_refer] => 0.10
[DateUser] => Apr-2019
)
I have tried to use this code,
$data = array_merge($withdrow_amount,$data_casback,$cashbonus_data,$data_discount,$CurrentDeposit);
$months = array();
foreach($data as $date) {
$month = substr($date['DateUser'], 0, 8);
$months[$month][] = $date;
}
echo '<pre>'; print_r($months); die;

You can iterate over your array, using array_merge with the splat operator ... to flatten the internal arrays. Note you can't have two DateUser keys in an array so one will be deleted; assuming they have the same values as in your data that will not be a problem:
$array = array (
'Apr-2019' =>
array (
0 =>
array (
'DateUser' => 'Apr-2019',
'withdarw_amount' => 4.00
),
1 =>
array (
'current_deposit_amount' => 1.00,
'current_deposit_table_refer' => 0.00,
'current_deposit_user_refer' => 0.10,
'DateUser' => 'Apr-2019'
),
),
'Jun-2019' =>
array (
0 =>
array (
'DateUser' => 'Jun-2019',
'withdarw_amount' => 334.00
),
)
);
foreach ($array as &$arr) {
$arr = array_merge(...$arr);
}
print_r($array);
Output:
Array
(
[Apr-2019] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4
[current_deposit_amount] => 1
[current_deposit_table_refer] => 0
[current_deposit_user_refer] => 0.1
)
[Jun-2019] => Array
(
[DateUser] => Jun-2019
[withdarw_amount] => 334
)
)
Demo on 3v4l.org

You can use simple loops also to do that -
$new = [];
foreach ($array as $key =>$a) {
$new[$key] = []; // Define with key
foreach ($a as $v) {
$new[$key] += $v; // Concat
}
}

Related

Group subsets of data in 3-level array by identifying column

I've this type of array in PHP:
Array(
[100] => Array(
[1] => Array (
[AVA_Date] => 2019-04-18
[ROO_Id] => 100
[RAT_Id] => 9
)
[2] => Array (
[AVA_Date] => 2019-04-20
[ROO_Id] => 100
[RAT_Id] => 10
)
[4] => Array (
[AVA_Date] => 2019-04-21
[ROO_Id] => 100
[RAT_Id] => 10
)
[7] => Array (
[AVA_Date] => 2019-04-22
[ROO_Id] => 100
[RAT_Id] => 9
)
)
)
I would like to merge items on ROO_Id and RAT_Id.
Then, for the AVA_Date, I need to list them under a new array in the current array.
So, the desired output is:
Array(
[100] => Array(
[0] => Array (
[AVA_Date] => Array (
[0] => 2019-04-18
[1] => 2019-04-22
)
[ROO_Id] => 100
[RAT_Id] => 9
)
[1] => Array (
[AVA_Date] => Array (
[0] => 2019-04-20
[1] => 2019-04-21
)
[ROO_Id] => 100
[RAT_Id] => 10
)
)
)
Here what I have tried:
$newArrOtherRooms = array_reduce($newArr, function($acc, $val) {
$room = array_search($val['ROO_Id'], array_column($acc, 'ROO_Id'));
$rate = array_search($val['RAT_Id'], array_column($acc, 'RAT_Id'));
if($rate == $room && $room > -1) {
array_push($acc[$room]['AVA_Date'], $val['AVA_Date']);
}
else {
$new_arr = $val;
$new_arr['AVA_Date'] = [$val['AVA_Date']];
array_push($acc, $new_arr);
}
return $acc;
},[]);
But it doesn't work like I want.
There are a couple of issues with your code. Firstly, you need to wrap the array_reduce with a foreach over the outer level of $newArr. Secondly, your call to array_search doesn't consider the fact that a ROO_Id or RAT_Id value might exist more than once in the array, as it only returns the first key at which it finds the value. To work around this, you can use array_keys to get an array of key values for each ROO_Id and RAT_Id value, and then take the intersection of those two arrays using array_intersect to see if both are present in the same element. If so, you update that element, otherwise you create a new one:
foreach ($newArr as $key => $array) {
$newArrOtherRooms[$key] = array_reduce($array, function($acc, $val) {
$room = array_keys(array_column($acc, 'ROO_Id'), $val['ROO_Id']);
$rate = array_keys(array_column($acc, 'RAT_Id'), $val['RAT_Id']);
$common = array_intersect($room, $rate);
if(!empty($common)) {
array_push($acc[current($common)]['AVA_Date'], $val['AVA_Date']);
}
else {
$new_arr = $val;
$new_arr['AVA_Date'] = [$val['AVA_Date']];
array_push($acc, $new_arr);
}
return $acc;
},[]);
}
print_r($newArrOtherRooms);
Output:
Array(
[100] => Array(
[0] => Array (
[AVA_Date] => Array (
[0] => 2019-04-18
[1] => 2019-04-22
)
[ROO_Id] => 100
[RAT_Id] => 9
)
[1] => Array (
[AVA_Date] => Array (
[0] => 2019-04-20
[1] => 2019-04-21
)
[ROO_Id] => 100
[RAT_Id] => 10
)
)
)
Demo on 3v4l.org
There is absolutely no reason to be making all of those iterated function calls.
Use a nested loop to iterate the subset of data for each room, group on the room "rate id", and push all "available date" values into a subarray in the respective group. When the subset of data is fully iterated, push its grouped data into the result array.
Code: (Demo)
$result = [];
foreach ($newArr as $rooId => $rows) {
$groups = [];
foreach ($rows as $row) {
if (!isset($groups[$row['RAT_Id']])) {
$row['AVA_Date'] = (array) $row['AVA_Date'];
$groups[$row['RAT_Id']] = $row;
} else {
$groups[$row['RAT_Id']]['AVA_Date'][] = $row['AVA_Date'];
}
}
$result[$rooId] = array_values($groups);
}
var_export($result);
Output:
array (
100 =>
array (
0 =>
array (
'AVA_Date' =>
array (
0 => '2019-04-18',
1 => '2019-04-22',
),
'ROO_Id' => 100,
'RAT_Id' => 9,
),
1 =>
array (
'AVA_Date' =>
array (
0 => '2019-04-20',
1 => '2019-04-21',
),
'ROO_Id' => 100,
'RAT_Id' => 10,
),
),
)

Compare two multidimensional array and replace values based on matching keys

I have 2 arrays of different length:
$array1 = Array
(
[0] => Array
(
['_id'] => "Group1"
['M'] => 0
['F'] => 0
)
[1] => Array
(
['_id'] => "Group2"
['M'] => 0
['F'] => 0
)
[2] => Array
(
['_id'] => "Group3"
['M'] => 0
['F'] => 0
)
[3] => Array
(
['_id'] => "Group4"
['M'] => 0
['F'] => 0
)
)
$array2 = Array
(
[0] => Array
(
['_id'] => "Group2"
['M'] => 180
['F'] => 200
)
[1] => Array
(
['_id'] => "Group4"
['M'] => 360
['F'] => 500
)
)
I want to compare the values of ['_id'] in both array and if they match, I will replace the values of ['M'] and ['F'] in array1 with those from array2 based on the corresponding ['_id].
So my desired ouptput would be:
$array1 = Array
(
[0] => Array
(
['_id'] => "Group1"
['M'] => 0
['F'] => 0
)
[1] => Array
(
['_id'] => "Group2"
['M'] => 180
['F'] => 200
)
[2] => Array
(
['_id'] => "Group3"
['M'] => 360
['F'] => 500
)
[3] => Array
(
['_id'] => "Group4"
['M'] => 0
['F'] => 0
)
)
This is my code but I can't seem to get the values replaced with the new values. The values are still the same as before.
foreach ($array1 as $defArr)
{
foreach ($array2 as $dayArr)
{
if($dayArr['_id'] == $defArr['_id'])
{
$defArr['M'] = $dayArr['M'];
$defArr['F'] = $dayArr['F'];
}
}
}
This can be a one-character change:
foreach ($array1 as $defArr)
goes to
foreach ($array1 as &$defArr)
# ^
The & reference operator points to the original sub array in the foreach loop context rather than a temporary variable.
However, it's a bit safer to use the index explicitly:
foreach ($array1 as $i => $defArr) {
foreach ($array2 as $j => $dayArr) {
if ($dayArr['_id'] == $defArr['_id']) {
$array1[$i]['M'] = $array2[$j]['M'];
$array1[$i]['F'] = $array2[$j]['F'];
}
}
}
If speed is important or $array2 is large, the time complexity of your algorithm is O(n * m). I recommend hashing $array2 for fast lookups as follows (O(n)):
$lookup = array_reduce($array2, function ($a, $e) {
$a[$e['_id']] = $e;
return $a;
});
foreach ($array1 as $i => $e) {
if (array_key_exists($e['_id'], $lookup)) {
$array1[$i]['M'] = $lookup[$e['_id']]['M'];
$array1[$i]['F'] = $lookup[$e['_id']]['F'];
}
}
Try it!
<?php
$array1 = [['_id'=>'abc','M'=>0,'F'=>0],['_id'=>'abcd','M'=>0,'F'=>0],['_id'=>'abcde','M'=>0,'F'=>0]];
$array2 = [['_id'=>'abc','M'=>50,'F'=>300],['_id'=>'abcde','M'=>600,'F'=>700]];
foreach($array2 as $key=> $array2value){
$searched_in_array_1= array_search($array2value['_id'],array_column($array1,'_id'));
if(is_numeric($searched_in_array_1)) $array1[$searched_in_array_1] = $array2value;
}
var_dump($array1);
?>
You want to get a help with php array functions when dealing with arrays. I have used array_column and array_search functions for this

Matching array value (PHP Array)

Input post :
$_POST['dateSlot']
$_POST['timeStart']
$_POST['timeEnd']
$_POST['quota']
These input post will resulting the below array.
Array
(
[dateSlot] => Array
(
[0] => 2018-04-05
[1] => 2018-04-05
[2] => 2018-04-05
)
[timeStart] => Array
(
[0] => 11:06 AM
[1] => 10:06 AM
[2] => 9:06 AM
)
[timeEnd] => Array
(
[0] => 11:06 AM
[1] => 9:06 AM
[2] => 7:06 AM
)
[quota] => Array
(
[0] => 12
[1] => 10
[2] => 10
)
)
I'm trying to foreach them to match the index key and form another array with this idea. Not so sure if can get the value I want :
foreach ($_POST['dateSlot'] as $k => $val) {
foreach ($_POST['timeStart'] as $k2 => $val2) {
foreach ($_POST['timeEnd'] as $k3 => $val3) {
foreach ($_POST['quota'] as $k4 => $val4) {
if($k == $k2 && $k == $k3 && $k == $k4){
$timeslots[$k]['date_slot'] = $val;
$timeslots[$k]['time_start'] = $val2;
$timeslots[$k]['time_end'] = $val3;
$timeslots[$k]['event_quota'] = $val4;
}
}
}
}
}
By that foreach, I'm getting the error Illegal string offset for date_slot, time_start, time_end, and event_quota
Based on the rows in the array, my goal is to re-form the array so that they all will be combined together to form 3 rows.
Example :
Array
(
[0] => Array
(
[date_slot] => 2018-04-05
[time_start] => 11:06 AM
[time_end] => 11:06 AM
[event_quota] => 12
)
[1] => Array
(
[date_slot] => 2018-04-05
[time_start] => 10:06 AM
[time_end] => 9:06 AM
[event_quota] => 10
)
[2] => Array
(
[date_slot] => 2018-04-05
[time_start] => 9:06 AM
[time_end] => 7:06 AM
[event_quota] => 10
)
)
Another approach to grouping this kind of data without needing to know the key names in advance.
This works by using the first row's data current( $data ) as the main iterator, then builds an array by combining the outer keys array_keys( $data ) and the inner column value array_column( $data, $column ) with array_combine() which combines two arrays of keys and an array of value to make each row's final array structure keyed by column name.
This is absolutely reliant on each multidimensional array having the same count of elements. As such this is not suitable for forms with checkbox inputs in them. At which point I would suggest using name="row[0][ColumnName]" as your name attribute and negating the need for this array processing.
http://php.net/manual/en/function.array-column.php
http://php.net/manual/en/function.array-combine.php
http://php.net/manual/en/function.array-keys.php
$data = array(
'Column-1'=>array('Row-1a','Row-2a','Row-3a'),
'Column-2'=>array('Row-1b','Row-2b','Row-3b'),
'Column-3'=>array('Row-1c','Row-2c','Row-3c')
);
$array = array();
foreach( array_keys( current( $data ) ) as $column )
{
$array[] = array_combine( array_keys( $data ), array_column( $data, $column ) );
}
print_r( $array );
Produces
Array
(
[0] => Array
(
[Column-1] => Row-1a
[Column-2] => Row-1b
[Column-3] => Row-1c
)
[1] => Array
(
[Column-1] => Row-2a
[Column-2] => Row-2b
[Column-3] => Row-2c
)
[2] => Array
(
[Column-1] => Row-3a
[Column-2] => Row-3b
[Column-3] => Row-3c
)
)
If you know that the element keys in all 4 of those post variables will always correlate to one timeslot element, then I think this will work for you:
foreach ($_POST['dateSlot'] as $key => $value) {
$timeslots[$key] = [
'date_slot' => $_POST['dateSlot'][$key],
'time_start' => $_POST['timeStart'][$key],
'time_end' => $_POST['timeEnd'][$key],
'event_quota' => $_POST['quota'][$key],
];
}
print_r($timeslots);
$dateSlot = $_POST['dateSlot']
$timeStart = $_POST['timeStart']
$timeEnd = $_POST['timeEnd']
$quota = $_POST['quota']
$all = array();
foreach($dateSlot as $key => $date) {
$all[] = array(
"data_slot" => $dateSlot[$key],
"time_start" => $timeStart[$key],
"time_end" => $timeEnd[$key],
"quota" => $quota[$key]
)
}
Input
$array = array(
'dateSlot' => array('2018-04-05','2018-04-05','2018-04-05'),
'timeStart' => array('11:06 AM','10:06 AM','9:06 AM'),
'timeEnd' => array('11:06 AM','9:06 AM','7:06 AM'),
'quota' => array(12,10,10)
);
Solution
$new = array();
for($i=0;$i<count($array['dateSlot']);$i++){
$new[] = array(
'dateSlot' => $array['dateSlot'][$i],
'timeStart' => $array['timeStart'][$i],
'timeEnd' => $array['timeEnd'][$i],
'event_quota' => $array['quota'][$i],
);
}
echo "<pre>";print_r($new);
Output
Array
(
[0] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 11:06 AM
[timeEnd] => 11:06 AM
[event_quota] => 12
)
[1] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 10:06 AM
[timeEnd] => 9:06 AM
[event_quota] => 10
)
[2] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 9:06 AM
[timeEnd] => 7:06 AM
[event_quota] => 10
)
)

how to calculate sum of total key in same array

i need to calculate the total key within the following array that contains 4 different sub arrays
Array
(
[0] => Array
(
[total] => 4.2
[sku] => 4321
)
[1] => Array
(
[total] => 2
[sku] => 2456
)
[2] => Array
(
[total] => 3
[sku] => 2245
)
[3] => Array
(
[total] => 1.5
[sku] => 2674
)
)
i was calculating it using mysql directly but i prefer to use php
$sql = "SELECT SUM(CAST(IFNULL(total,0) AS DECIMAL(10,2))) FROM trans WHERE trans.userid = :userid";
so total has to be in the same format as from the query
10.70
You can loop over the array, adding up as you go, and then formatting with number_format(), or use array_column() to extract the 'total', and use array_sum() to add them, and again, using number_format() or sprintf() to format them.
echo number_format(array_sum(array_column($array, 'total')), 2);
I guess something like this:-
$array = Array
(
[0] => Array
(
[total] => 4.2
[sku] => 4321
)
[1] => Array
(
[total] => 2
[sku] => 2456
)
[2] => Array
(
[total] => 3
[sku] => 2245
)
[3] => Array
(
[total] => 1.5
[sku] => 2674
)
);
$total = 0;
foreach ($array as $key => $value) {
$total += $value['total'];
}
print_r($total);
you can iterate your array and sum all values like:
<?php
$array = array(
array(
'total' => 4.2,
'sku' => 4321
),
array(
'total' => 2,
'sku' => 2456
),
array(
'total' => 3,
'sku' => 2245
),
array(
'total' => 1.5,
'sku' => 2674
),
);
$result = array('total' => 0,'sku' => 0);
foreach($array as $key => $value) {
$result['total'] += $value['total'];
$result['sku'] += $value['sku'];
}
var_dump($result);
Result: array(2) { ["total"]=> float(10.7) ["sku"]=> int(11696) }
for single total sum output:
echo $result['total'];
You can also try with array_walk_recursive() like this https://eval.in/875555
$input = [your_2d_array_goes_here];
$final = array();
array_walk_recursive($input, function($item, $key) use (&$final){
$final[$key] = isset($final[$key]) ? $item + $final[$key] : $item;
});
print '<pre>';
print_r($final);
print '</pre>';
print $final['total'];
?>

how to get array from get to normal array in php

I have an array like this and it can contain multiple values:
Array
(
[rpiid] => Array
(
[1] => 86
)
[sensor_id] => Array
(
[1] => 1
)
[when] => Array
(
[1] => 2014-02-24
)
[val] => Array
(
[1] => 000
)
[train] => Array
(
[1] => True
)
[valid] => Array
(
[1] => False
)
[button] => update
)
Of course, here there is only the number 1 each time but sometimes I have 0, 1, 2 and a value associated. This is because I get this from a GET from multiple forms.
How can I transform this array into
Array
(
[0] => Array
(
[rpiid] => 86
[sensor_id] => 1
...
Thanks,
John.
if your array is $get
$newArray = Array();
foreach($get as $secondKey => $innerArray){
foreach($value as $topKey => $value) {
$newArray[$topKey][$secondKey] = $value;
}
}
This should work
$new_array = array();
foreach($first_array as $value => $key){
$new_array[$key] = $value[1];
}
Sure you can, take a look at this small example:
$a = [ 'rpid' => [1], 'cpid' => [2,2] ];
$nodes = [];
foreach($a as $node => $array) {
foreach($array as $index => $value) {
if(empty($nodes[$index]))
$nodes[$index] = [];
$nodes[$index][$node] = $value;
}
}
print_r($nodes):
Array
(
[0] => Array
(
[rpid] => 1
[cpid] => 2
)
[1] => Array
(
[cpid] => 2
)
)

Categories