How to group array 1 dimensional to array 2 dimensional? - php

My array one is like this :
Array
(
[0] => Array
(
[total_transaction] => 2000000
[month] => May
)
[1] => Array
(
[total_transaction] => 1000000
[month] => June
)
)
My array two is like this :
Array
(
[0] => 4000000
[1] => 5000000
)
I want to group the array like this :
Array
(
[0] => Array
(
[target] => 4000000
[total_transaction] => 2000000
[month] => May
)
[1] => Array
(
[target] => 5000000
[total_transaction] => 1000000
[month] => June
)
)
I'm still confused
Any solution to solve my problem?
Thank you very much

The solution using array_map and array_replace_recursive functions:
// $arrOne is your first array
$arrTwo = [4000000, 5000000]; // it's your second array
$arrTwo = array_map(function($v){ return ['target' => $v]; }, $arrTwo);
$result = array_replace_recursive($arrOne, $arrTwo);
print_r($result);
The output:
Array
(
[0] => Array
(
[total_transaction] => 2000000
[month] => May
[target] => 4000000
)
[1] => Array
(
[total_transaction] => 1000000
[month] => June
[target] => 5000000
)
)

Try :
foreach($array1 as $key1=>$arr1) {
if(isset($array2[$key1])) $array1[$key1]['target'] = $array2[$key1];
}

$a = array
(
0 => array
(
"total_transaction" => 2000000,
"month" => "May"
),
1 => array
(
"total_transaction" => 1000000,
"month" => "June"
)
);
$b = array
(
0 => 4000000,
1 => 5000000
);
foreach($a as $key=>$val) {
$a[$key]['target'] = $b[$key];
}
Try this one..

try this,
$out = array();
foreach ($aa1 as $key => $value){
$out[] = (object)array_merge((array)$aa2[$key], (array)$value);
}
print_r($out);
https://3v4l.org/kBJVB
i hope it will be helpful.

$array1 = array(
array(
'total_transaction' => 2000000,
'month' => 'May'
),
array(
'total_transaction' => 1000000,
'month' => 'June'
));
$array2 = array(
'0' => 4000000,
'1' => 5000000
);
foreach($array2 as $k=>$v){
$array1["$k"]['target']=$v;
}

Related

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 merge or combine 2 arrays based on their keys in php

i have 2 array and i want to merge or combine them...
Array
(
[0] => Array
(
[year] => 2015
[value] => 32
)
[1] => Array
(
[year] => 2016
[value] => 54
)
)
Array
(
[0] => Array
(
[year] => 2015
[value] => 95
)
[1] => Array
(
[year] => 2016
[value] => 2068
)
)
i want them to look like this...
Array(
[2015]=>array(
[0] => 32
[1] => 95
)
[2016]=>array(
[0] => 54
[1] => 2068
)
)
it this possible? if ever, how?.... thanks so much
$a = array(
0 => array
(
"year" => 2015,
"value" => 32
),
1 => array
(
"year" => 2016,
"value" => 54
)
);
$b = array(
0 => array
(
"year" => 2015,
"value" => 300
),
1 => array
(
"year" => 2016,
"value" => 5400
)
);
$c = array_merge($a,$b);
$output = array();
foreach($c as $key=>$val)
{
$output[$val['year']][] = $val['value'];
}
echo '<pre>';
print_r($output);
exit;
Try this code..
If the original arrays are $a and $b, run this code and the result you want will be in $result
$sources = array_merge($a,$b);
$result = [];
foreach($sources as $data){
$yr = $data['year'];
if(!isset($result[$yr])) $result[$yr]=[];
$result[$yr][]=$data['value'];
}
Live demo
Try:
$newArr = array();
foreach($array1 as $key1=>$arr1) {
$newArr[$arr1['year']][] = $arr1['value'];
$newArr[$arr1['year']][] = $array2[$key]['value'];
}
You can also do something like this,
<?php
$test1 = [["year"=>2015,"value"=>32],["year"=>2016,"value"=>54]];
$test2 = [["year"=>2015,"value"=>95],["year"=>2016,"value"=>2068]];
$newarray=array();
foreach($test1 as $key1=>$value1){
$temp = [$value1['value']];
foreach($test2 as $key2=>$value2){
if($value1['year']==$value2['year']){
$temp[] = $value2['value'];
}
$newarray[$value1['year']] = $temp;
}
}
print_r($newarray);
?>
check here : https://eval.in/605323
output is :
Array
(
[2015] => Array
(
[0] => 32
[1] => 95
)
[2016] => Array
(
[0] => 54
[1] => 2068
)
)

merging mutidimensional arrays

I have an array that looks like this:
getting array need to convert array as same key value as 0
foreach($array as $key=>$id){
$consumer_data[]=$this->App_model->get_session($id);
}
print_r($consumer_data);
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
)
[1] => Array
(
[0] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
i want to implement array like this in same key value as 0
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
[3] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
I am using PHP. Can anyone point me to a good starting point as to how I should go about doing this?
You can use array_merge():
$new_array[0] = array_merge($array[0], $array[1]);
Where $array is the first array.
SEE DEMO
OR for a more dynamic approach:
$new_array = array(0 => array());
foreach($array as $a) {
$new_array[0] = array_merge($new_array[0], $a);
}
SEE DEMO 2
The simpliest solution is to do it with:
$input = array(
array(
array('ConsumerID' => 1, 'name' => 'asdfd'),
array('ConsumerID' => 5, 'name' => 'test'),
array('ConsumerID' => 4, 'name' => 'test1'),
),
array(
array('ConsumerID' => 4, 'name' => 'test4'),
),
);
$output = array(
array()
);
foreach ($input as $data) {
$output[0] = array_merge($output[0], $data);
}
Try this->
$newArray = array();
foreach($values as $key=>$val){
$newArray [0][$key]=$val;
}
print_r($newArray);
Check this:
<?php
$arr[0] = array(0 => array("ConsumerID" => 1, "name" => "Ni"), 1 => array("ConsumerID" => 2, "name" => "Ab"));
$arr[1] = array(1 => array("ConsumerID" =>5, "name" => "GE"), 1 => array("ConsumerID" => 6, "name" => "DB"));
$new = array();
foreach($arr as $key => $value) {
foreach($value as $innerkey => $innervalue) {
$new[0][] = $innervalue;
}
}
print_r($new);
?>

how to merge array when the value is same

I have two arrays like this:
array1 = Array (
[0] => Array ( [value] => 1 [date] => 2014-03-15 ),
[1] => Array ( [value] => 1 [date] => 2014-03-15 )
);
array2 = Array (
[0] => Array ( [value] => 1 [date] => 2014-03-15 ),
[1] => Array ( [value] => 1 [date] => 2014-03-16 )
);
How to get output like this?
date 2014-03-15 = 3
date 2014-03-16 = 1
You can not merge those array directly with array_merge, because the counting based on 'value', so you must create some codes, try this:
$array1 = array(
array("value" => 1, "date" => "2014-03-15"),
array("value" => 1, "date" => "2014-03-15"),
);
$array2 = array(
array("value" => 1, "date" => "2014-03-15"),
array("value" => 1, "date" => "2014-03-16"),
);
foreach(array_merge($array1, $array2) as $arr){
!isset($array[$arr['date']]) ? $array[$arr['date']] = $arr['value'] : $array[$arr['date']] += $arr['value'];
}
print_r($array);
it will returns :
Array ( [2014-03-15] => 3 [2014-03-16] => 1 )

How to Add a new Key and Merge the array values in multidimemsional array PHP

here's the result of my first function:
Array
(
[0] => Array
(
[MaidID] => 13
[Stores] => Array
(
[0] => 14
[1] => 5
)
)
[1] => Array
(
[MaidID] => 3
[Stores] => Array
(
[0] => 4
)
)
[2] => Array
(
[MaidID] => 41
[Stores] => Array
(
[0] => 14
)
)
)
Then, here's the result of my second function:
Array
(
[1] => Array
(
[MaidID] => 14
[Cash] => 10000
[Debit] => 0
[Credit] => 0
)
)
and here's should be the result:
Array ([0] => Array (
[MaidID] => 14
[Cash] => 10000.00
[Debit] => 0
[Credit] => 0
[MaidsID] => Array(
[0] => 13
[1] => 41
)
)
)
Is it possible to make it?I need to a new key name MaidsID pointing to the list of MaidID owned by more than one Stores.Please help me, Please be patience in my question, im just a beginner.Thank you so much.
this code work ok. $a is your first array $b is the second, $c is result
$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);`
I tested it here and it was ok. (Just replace $a with your first array and $b with seccond ).
Are you sure that the structure of your arrays is exactly as you have written above?? Maybe there is any problem with that.
You have puted array inside another array? (its not needed i think)
Howerver: For this code:
`$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
print_r($a);
echo "<br><br>================================================<br><br>";
print_r($b);
echo "<br><br>================================================<br><br>";
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);
print_r($c);
echo "<br><br>================================================<br><br>";`
The output is:
Array ( [0] => Array ( [Maid] => 1 [Stores] => Array ( [0] => 1 [1] => 5 ) ) [1] => Array ( [Maid] => 3 [Stores] => Array ( [0] => 4 ) ) [2] => Array ( [Maid] => 4 [Stores] => Array ( [0] => 1 ) ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) [MaidsID] => Array ( [0] => 1 [1] => 3 [2] => 4 ) )
================================================
Isn't this how you want the result?
Have a look at this. Maybe this can help you.
$result = array_merge($array1, $array2);

Categories