i have this array:
[Jessica CS] => Array
(
[2011-04-20] => Array
(
[0] => 69.90
[cancel] => 1311145200
[1] => 29.95
[2] => 69.90
)
)
[Rex CS] => Array
(
[2011-04-20] => Array
(
[0] => 119.94
[cancel] =>
)
[2011-04-26] => Array
(
[0] => 199.50
[cancel] =>
[1] => 29.95
)
....
and i am adding together these values by using a loop:
$i=0;
foreach($dates as $d){
$total[$i] += array_sum($value[$d]);
#i++;
}
this will add everything together and what i want is to exclude the [cancel] field from being added to the array_sum
edit: i could probably add the values that are int, but not sure how to sort them
any ideas?
thanks
Personally I would change the layout of the array so that it went more like:
[Jessica CS] => Array
(
[2011-04-20] => Array
(
[cancel] => 1311145200
[costs] => Array
(
[0] => 69.90
[1] => 29.95
[2] => 69.90
)
)
)
Replacing [costs] with whatever name is most relevant.
Working with your existing array
Failing that
$i=0;
foreach($dates as $d){
$total[$i] += array_sum(array_diff_key($value[$d], array('cancel'));
#i++;
}
Just loop through the array, perhaps like this:
$input = array(
'Jessica CS' => array(
'2011-04-20' => array(
0 => 69.9,
'cancel' => 1311145200,
1 => 29.95,
2 => 69.90,
),
),
'Rex CS' => array(
'2011-04-20' => array(
0 => 119.94,
'cancel' => null,
),
'2011-04-26' => array(
0 => 199.50,
'cancel' => null,
1 => 29.95,
)
)
);
$totals = array();
foreach($input as $person => $dates){
$totals[$person] = 0;
foreach ($dates as $date => $values) {
foreach ($values as $key => $val) {
if ($key !== 'cancel') {
$totals[$person] += $val;
}
}
}
}
print_r($totals)
Produces:
Array
(
[Jessica CS] => 169.75
[Rex CS] => 349.39
)
Related
This question already has answers here:
Group subarrays by one column, make comma-separated values from other column within groups
(2 answers)
Closed last month.
here's how it looks in the PHP code:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
print_r($array);
?>
Example of print_r() function output:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml
)
[1] => Array
(
[name] => filter_amount
[value] => 200-ml
)
[2] => Array
(
[name] => page_size
[value] => 7
)
)
I need to combine duplicates of filter_amount values from the array.
The values of these duplicates must be commas separated and the result should be the following code:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml,200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
[2] => Array
(
[name] => orderby
[value] => rating
)
[3] => Array
(
[name] => paged
[value] => 1
)
)
Since you want value to be concatenated by a comma, you'll have to make a cycle of it
<?php
//Allow me to change this variable name, just to not create confusion
$content = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
//$content is your initial array
//$outputArray is the final worked-up array
$outputArray = [];
//Let's make a cycle going for every array inside $content
foreach ($content as $innerArray) {
//Does this $innerArray['name'] (filter_ammount) exist in $outputArray in an array
//consisting in key => value where the key is 'name' and equals
//what we look for that is(filter_ammount)?
$key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//If not, let's place this array in the $output array
if ($key === false) {
array_push($outputArray, $innerArray);
} else {
//If exists, then $key is the $key of the $outputArray and let's add to its value
//our current value, that is in our $innerArray, concatenated with a comma
$outputArray[$key]['value'] .= ",". $innerArray['value'];
}
}
//Boom, magic
print_r($outputArray);
//Note: This is going to affect every duplicate it finds, as in:
//If you got 3 arrays with name 'filter_ammount' and 2 arrays with name
//'page_size', it's going to concatenate the filter_ammount and the 'page_size'.
//If you specifically just want filter_ammount,
//replace this -> $key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//with this -> $key = array_search('filter_ammount', array_column($outputArray , 'name'));
?>
References
http://php.net/manual/en/function.array-search.php
http://php.net/manual/en/function.array-column.php
How to combine two items by 2 duplicate columns?
[root#localhost TEST]# php R00.php
Array
(
[0] => Array
(
[0] => S01
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445
)
[1] => Array
(
[0] => S02
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[2] => Array
(
[0] => S03
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 23
)
[4] => Array
(
[0] => S05
[1] => 100.100.100.100
[2] => 192.168.100.100
[3] => 22
)
[5] => Array
(
[0] => S06
[1] => 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[6] => Array
(
[0] => S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[7] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[8] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[9] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
[root#localhost TEST]#
combine 1 or 2 items by 2 column duplicate below is result I need
Array
(
[0] => Array
(
[0] => S01 , S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445 , 23
)
[1] => Array
(
[0] => S02 , S03 , S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S05 , S06
[1] => 100.100.100.100 , 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[4] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[5] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[6] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
Try this:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$tmp = array();
foreach($array as $val) {
$tmp[$val['name']]['values'][] = $val['value'];
}
foreach($tmp as $k => $v) {
$item = implode(',', array_unique(explode(',', implode(',',$v['values']))));
$newArr[] = array('name' => $k, 'value' => $item);
}
echo '<pre>';
print_r($newArr);
echo '</pre>';
got it with the following crazy mess:
$name = array_column($array, 'name');
$value = array_column($array, 'value');
foreach($name as $nk=>$nv)
foreach($value as $vk=>$vv)
if($nk == $vk)
$a[$nv][] = $vv;
foreach($a as $k=>$v)
$b[$k] = implode(',', $v);
$z = 0;
foreach($b as $k=>$v)
{
$c[$z]['name'] = $k;
$c[$z]['value'] = $v;
$z++;
}
$c is the resulting array
Or using a medley of array functions:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$names = array_column($array, 'name');
$values = array_column($array, 'value');
$result = [];
foreach (array_unique($names) as $k)
$result[$k] = implode(", ", array_filter($values,
function($v, $indx) use ($names, $k) {
return $names[$indx] == $k;
}, ARRAY_FILTER_USE_BOTH));
print_r($result);
$result2 = [];
foreach ($result as $k=>$v) $result2[] = ['name'=>$k, 'value'=>$v];
print_r($result2);
Results in:
Array
(
[filter_amount] => 100-ml, 200-ml
[page_size] => 7
)
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml, 200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
)
All of the other answers up to now are using two or more iterating techniques for this task. There only needs to be one loop.
Build an associative output array based on the name values as you iterate. If the associative key isn't set, then save the whole row. If it is set, then just append a comma then the new value data to the stored value element.
Using temporary keys allows isset() to swiftly check for existence. It will always outperform array_search() and in_array() because of how php treats arrays (as hash maps).
Remove the temporary keys when the loop is finished by calling array_values().
Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['name']])) {
$result[$row['name']] = $row;
} else {
$result[$row['name']]['value'] .= ',' . $row['value'];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'name' => 'filter_amount',
'value' => '100-ml,200-ml',
),
1 =>
array (
'name' => 'page_size',
'value' => '7',
),
)
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
)
)
I tried to read from a JSON file using PHP. But I am stuck now.
This is my JSON file :
Array
(
[date] => 25-1-2017
[leagues] => Array
(
[0] => Array
(
[league_id] => 0
[league_name] => كأس امم افريقيا
[league_logo] => http://3.bp.blogspot.com/-4iampWUCLto/VmldL2XTz7I/AAAAAAAAD0I/eZFfzSxRbnE/s60/africa.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 1
[team1logo] => http://2.bp.blogspot.com/-mycTzHXuzJA/Ugbb-UG_3JI/AAAAAAAAEv0/_mzaSHnRedE/s60/egypt+(4).png
[team2logo] => http://1.bp.blogspot.com/-kYrkU4jahZY/UgbeVpHf8RI/AAAAAAAAFIQ/TBgM5fvVW14/s60/ghana+(3).png
[match_time] => 19:00
[channels_id] => Array
(
[0] => 18
)
[team1] => مصر
[team2] => غانا
)
[1] => Array
(
[match_id] => 2
[team1logo] => http://3.bp.blogspot.com/-_y1MazEn-vg/UgbeoIWUpII/AAAAAAAAFPI/JTRGGpG3GiQ/s60/mali+(4).png
[team2logo] => http://2.bp.blogspot.com/-GTxKONxvKy4/Ugt6AKXOmyI/AAAAAAAAF8Y/pPcM0O58cQI/s60/uganda_2.png
[match_time] => 19:00
[channels_id] => Array
(
[0] => 19
)
[team1] => مالي
[team2] => أوغندا
)
)
)
[1] => Array
(
[league_id] => 1
[league_name] => كأس ملك أسبانيا
[league_logo] => http://1.bp.blogspot.com/-zAzWT2Vpbe0/Vmle9TUWDlI/AAAAAAAAD04/-BKgPzRCT1k/s60/Copa_del_Rey_logo_since_2012.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 3
[team1logo] => http://4.bp.blogspot.com/-6sfdkbboNdk/VBxVKisRkcI/AAAAAAAADG8/oBKDHmBW5xc/s60/eibar+fc.png
[team2logo] => http://2.bp.blogspot.com/-S7p2yMaLywM/UhCz6GqNCRI/AAAAAAAAGFE/CfxzSQk8bgQ/s60/Atletco+Madrid2.Png
[match_time] => 18:15
[channels_id] => Array
(
[0] => 1
)
[team1] => إيبار
[team2] => أتلتيكو مدريد
)
[1] => Array
(
[match_id] => 4
[team1logo] => http://1.bp.blogspot.com/--6y77FXuPLI/UjeVcpJ-AJI/AAAAAAAAGok/QyZhwahPamo/s60/Celta+Vigo.png
[team2logo] => http://2.bp.blogspot.com/-tncIAL_U6mI/UgbnQw75V8I/AAAAAAAAFhA/8-9Xpw83GKY/s60/Real+Madrid+(3).png
[match_time] => 20:15
[channels_id] => Array
(
[0] => 3
)
[team1] => سيلتا فيغو
[team2] => ريال مدريد
)
)
)
[2] => Array
(
[league_id] => 2
[league_name] => كأس الرابطة الإنجليزية
[league_logo] => http://4.bp.blogspot.com/-31G65FeskFs/VmlezHMPm_I/AAAAAAAAD0w/9OMjh8AQP-M/s60/TheFA_CapitalOneCup.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 5
[team1logo] => http://3.bp.blogspot.com/-D0lb4b-qN5U/UgbeDosqjYI/AAAAAAAAFBY/Qg7kvoodvFY/s60/Liverpool+(2).png
[team2logo] => http://2.bp.blogspot.com/-prH3jgmfewQ/Ug8ZgNxPlcI/AAAAAAAAGAU/HLmbFvGuDB8/s60/Southampton.png
[match_time] => 20:00
[channels_id] => Array
(
[0] => 2
)
[team1] => ليفربول
[team2] => ساوثهامبتون
)
)
)
[3] => Array
(
[league_id] => 3
[league_name] => كاس ايطاليا
[league_logo] => http://2.bp.blogspot.com/-p9Kjb2_GZPU/VnAdQcHV9_I/AAAAAAAAD7E/6R-P54Upui4/s60/tim-cup.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 6
[team1logo] => http://1.bp.blogspot.com/-POxAfSSnlW0/UhCt7oQsdLI/AAAAAAAAGDo/rMRXx2mqvUI/s60/Juventus[1].Png
[team2logo] => http://3.bp.blogspot.com/-_Qj_GaxVaDE/UgaEPtt7EsI/AAAAAAAAEA8/redsOTj7F4Q/s60/Ac+Milan+(3).png
[match_time] => 20:00
[channels_id] => Array
(
[0] => 103
)
[team1] => يوفنتوس
[team2] => ميلان
)
)
)
)
)
And this is my PHP so far:
$str = file_get_contents($url);
$json = json_decode($str, true);
$date = $json['date'];
$leagues = $json['leagues'][0];
foreach ($leagues as $key => $value) {
for ($i=0; $i<count($value); $i++) {
foreach ($value[$i] as $key1 => $value1) {
$sql = "INSERT INTO table_name (column1,column2,column3,column4) VALUES (...,...,...,...)";
for ($c=0; $c<count($value1); $c++) {
foreach ($value1[$c] as $key2 => $value2) {
echo $value2;
}
}
}
}
}
the problem how can read all each array by foreach and store every value to table in database.
I would appreciate an example for this.
Basic structure to loop all nodes in your json
//leagues level
foreach ($json['leagues'] as $key => $league) {
$league_id = $league['league_id'];
//leagues -> league_matches level
foreach ($league['league_matches'] as $key => $match) {
$match_id = $match['match_id'];
//channel ids
foreach ($match['channels_id'] as $channels_id) {
//$channels_id;
//create an sql
$sql = "INSERT INTO xy (col1,col2,col3) values ($league_id,$match_id,$channels_id)";
$db->query($sql);
}
}
}
About the SQL You dont show any structure of your databse, so i cant help creating the sql.
I hope your JSON file sends the value using json_encode() method. You need to separate the array elements with a comma (,). Also you need to write the array indexes within quotes ('). Here is a corrected sample of your JSON page.
$retArr = Array
(
['date'] => 25-1-2017,
['leagues'] => Array
(
[0] => Array
(
['league_id'] => 0,
['league_name'] => كأس امم افريقيا,
['league_logo'] => http://3.bp.blogspot.com/-4iampWUCLto/VmldL2XTz7I/AAAAAAAAD0I/eZFfzSxRbnE/s60/africa.png,
['league_matches'] => Array
(
[0] => Array
(
['match_id'] => 1,
['team1logo'] => http://2.bp.blogspot.com/-mycTzHXuzJA/Ugbb-UG_3JI/AAAAAAAAEv0/_mzaSHnRedE/s60/egypt+(4).png,
['team2logo'] => http://1.bp.blogspot.com/-kYrkU4jahZY/UgbeVpHf8RI/AAAAAAAAFIQ/TBgM5fvVW14/s60/ghana+(3).png,
['match_time'] => 19:00
['channels_id'] => Array
(
[0] => 18
)
['team1'] => مصر ,
['team2'] => غانا
)
)
)
)
);
echo json_encode($retArr);
On the receiving page, you need to set a second parameter to TRUE in the json_decode() method so that you will get the result as an array rather than as a JSON Object.
$str = file_get_contents($url);
$json = json_decode($str, true);
$date = $json['date'];
$leagues = $json['leagues'][0];
foreach ($leagues as $key => $value) {
for ($i=0; $i<count($value); $i++) {
foreach ($value[$i] as $key1 => $value1) {
$sql = "INSERT INTO table_name (column1,column2,column3,column4) VALUES (...,...,...,...)";
for ($c=0; $c<count($value1); $c++) {
foreach ($value1[$c] as $key2 => $value2) {
echo $value2;
}
}
}
}
}
Try this way.
I'm wanting to edit an array key name, changing it from a date into it's numeric order id, and throwing the date inside the array.
[entries] => Array
(
[2015-07-19] => Array
(
[value] => 14.8
)
[2015-07-18] => Array
(
[value] => 14.9
)
Into
[entries] => Array
(
[0] => Array
(
[value] => 14.8
[date] => 2015-07-19
)
[1] => Array
(
[value] => 14.9
[date] => 2015-07-18
)
Have you tried:
$entries = array(
'2015-07-19' => array(
'value' => 14.8
),
'2015-07-18' => array(
'value' => 14.9
)
);
$result = array();
foreach ($entries as $key => $value) {
$result['entries'][] = array('date'=> $key, 'value' => $value['value']);
}
print_r($result);
My suggestion (without creating new array):
$entries = array(
'2015-07-19' => array ('value' => 14.8),
'2015-07-18' => array ('value' => 14.9),
);
foreach ($entries as $key => &$entry)
{
$entry['date'] = $key;
}
unset($entry);
$entries = array_values($entries);
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);
?>