I'm having this array:
array(
[1] => Array
(
[1111] => Array
(
[one] => 70
[two] => 7.0
)
)
[2] => Array
(
[1111] => Array
(
[one] => 10
[two] => 2.0
)
)
)
And i want it to sum [one] and [two] of the two different arrays and output an array like this:
Array
(
[1111] => Array (
[one] => 80
[two] => 9.0
)
)
Based on the key "1111"
I have tried with the following
$array = array_values( $array );
$sum_array = [];
foreach ( $array as $k => $sub_array ) {
foreach ( $sub_array as $id => $value ) {
$sum_array[$id] += array_key_exists( $id, $sum_array ) ? $sum_array[$id] += $value : $sum_array[$id] = $value;
$sum_array[$id]['two'] += array_sum( $sum_array[$id]['two'] ) / ( 100 * count( $sum_array[$id]['count'] ) );
}
}
But that gives me only the first value like this:
Array
(
[1111] => Array (
[one] => 70
[two] => 7.0
)
)
Can someone please tell me what i'm doing wrong?
You could use the array_reduce to reduce your array to only one element.
The array_reduce function takes two ( sometime three ) parameters. The first one is the array, the second one is a callback with two parameters , the collector, and the current item. If there is a third parameters to the array_reduce function, it will be used to initialize the collector ( like in our case ).
The function will parse each item and do something with it, the collector will be available at each iteration, that's why we use it to keep information of the data, in our case, it's the sum of the values. Once all the item has been iterated over, the function returns the collector.
<?php
$array = [
[
'1111' => [
'one' => 70,
'two' => 7.0
]
],
[
'1111' => [
'one' => 10,
'two' => 2.0
]
]
];
$output = array_reduce($array, function($collector, $item) {
$collector['1111']['one'] += $item['1111']['one'];
$collector['1111']['two'] += $item['1111']['two'];
return $collector;
}, ['1111' => ['one' => 0, 'two' => 0]]);
var_dump($output);
Here is an approach that uses a recursive merge. In the following when merged John's hours become an array. We then need to sum. As James only has one entry his hours do not become an array so we leave them be.
Hopefully the transitional merge output, will give you an idea of the process:
<?php
$items =
[
[
'john' => [
'hours' => 2,
'tips' => 7
]
],
[
'john' => [
'hours' => 3,
'tips' => 10
]
],
[
'james' => [
'hours' => 8,
'tips' => 0
]
]
];
$output = array_merge_recursive(...$items);
var_export($output);
array_walk($output, function(&$v) {
array_walk($v, function(&$v) {
if(is_array($v))
$v = array_sum($v);
});
});
echo "\nResult: \n";
var_export($output);
Output:
array (
'john' =>
array (
'hours' =>
array (
0 => 2,
1 => 3,
),
'tips' =>
array (
0 => 7,
1 => 10,
),
),
'james' =>
array (
'hours' => 8,
'tips' => 0,
),
)
Result:
array (
'john' =>
array (
'hours' => 5,
'tips' => 17,
),
'james' =>
array (
'hours' => 8,
'tips' => 0,
),
)
$total=0;
array_map(function ($item) use ($total)
{
$total += $item['one'] + $item['two'];
}, $array);
echo $total;
Or difference key
$one = array_sum(array_column($array, 'one'));
$two = array_sum(array_column($array, 'two'));
$resultArray = compact('one','two');
```
Related
I have an array with the following structure:
[0] => Array
(
[venue1] => 1
[venue2] => 2
)
[1] => Array
(
[venue1] => 3
[venue2] => 4
)
[2] => Array
(
[venue1] => 2
[venue2] => 1
)
[3] => Array
(
[venue1] => 5
[venue2] => 6
)
I need to remove the duplicate "pair of values", in this case row [0] and row [2]
I tried it with that code, but it doesn't work (and of course it's not very elegant) ;-)
foreach ( $compare_arr as $v1 )
{
$key = array_search( intval($v1[venue1]), array_column( $compare_arr, 'venue2' ) );
if ( $key <> '' ) unset($compare_arr[$key]);
}
Do you have an idea how to solve this?
Thanks a lot for your help!
Oliver
Here is an approach where an intermediate array is formed of sorted values. That you can then search for to find duplicate pairs to remove.
<?php
$venues =
array (
0 =>
array (
'venue1' => 1,
'venue2' => 2,
),
1 =>
array (
'venue1' => 3,
'venue2' => 4,
),
2 =>
array (
'venue1' => 2,
'venue2' => 1,
),
3 =>
array (
'venue1' => 5,
'venue2' => 6,
),
);
$result = $pairs = $venues;
array_walk($pairs, 'sort');
var_export($pairs);
foreach($pairs as $k => $pair) {
if(count(array_keys($pairs, $pair)) > 1) {
unset($result[$k]);
}
}
var_export($result);
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
),
1 =>
array (
0 => 3,
1 => 4,
),
2 =>
array (
0 => 1,
1 => 2,
),
3 =>
array (
0 => 5,
1 => 6,
),
)array (
1 =>
array (
'venue1' => 3,
'venue2' => 4,
),
3 =>
array (
'venue1' => 5,
'venue2' => 6,
),
)
If you want to remove occurring duplicates rather than pruning out duplicates altogether, you can do an array_unique on the sorted array above and then use the remaining keys to filter the original array.
$tmp = $venues;
array_walk($tmp, 'sort');
$tmp = array_unique($tmp, SORT_REGULAR);
$result = array_intersect_key($venues, $tmp);
var_export($result);
Output:
array (
0 =>
array (
'venue1' => 1,
'venue2' => 2,
),
1 =>
array (
'venue1' => 3,
'venue2' => 4,
),
3 =>
array (
'venue1' => 5,
'venue2' => 6,
),
)
You might also first loop the array creating a compound key based on the ordered keys.
Then you can filter the result only keeping arrays where the count is 2 as nothing is added because there are no duplicates.
For example
$result = [];
$compare_arr = [
["venue1" => 1, "venue2" => 2],
["venue1" => 3, "venue2" => 4],
["venue1" => 2, "venue2" => 1],
["venue1" => 5, "venue2" => 6],
];
foreach ($compare_arr as $v1) {
sort($v1);
$cKey = $v1[0] .'-'. $v1[1];
if (array_key_exists($cKey, $result)) {
$result[$cKey][] = $v1;
continue;
}
$result[$cKey] = $v1;
}
$result = array_filter($result, function($item) {
return count($item) === 2;
});
print_r($result);
Output
Array
(
[3-4] => Array
(
[0] => 3
[1] => 4
)
[5-6] => Array
(
[0] => 5
[1] => 6
)
)
You can see the compound keys are the values with a - in between. If you want to have the keys numbered from 0, you can use array_values.
Php demo
Edit
If you want to keep the first matching single pair, you can check for the compound key and if it already exists continue the loop without overwriting the existing one.
$result = [];
$compare_arr = [
["venue1" => 1, "venue2" => 2],
["venue1" => 3, "venue2" => 4],
["venue1" => 2, "venue2" => 1],
["venue1" => 5, "venue2" => 6]
];
foreach ($compare_arr as $v1) {
sort($v1);
$cKey = $v1[0] .'-'. $v1[1];
if (array_key_exists($cKey, $result)) {
continue;
}
$result[$cKey] = $v1;
}
print_r($result);
Output
Array
(
[1-2] => Array
(
[0] => 1
[1] => 2
)
[3-4] => Array
(
[0] => 3
[1] => 4
)
[5-6] => Array
(
[0] => 5
[1] => 6
)
)
Php demo
Whether you use a classic foreach() loop or functional iteration, there is no reason to iterate the input array more than once.
This snippet will appear nearly identical to TheFourthBird's answer, but I don't like the unnecessary use of continue. This snippet will ensure no that rows in the result array have 100% shared venue values (in any order). The subarray keys will also not suffer reordering; in other words the first element key will be venue1 then the second element will be venue2. Using implode() offers additional flexibility because the code won't need to be altered if the number of elements in each row changes.
$result = [];
foreach ($data as $index => $row) {
sort($row);
$key = implode('-', $row);
if (!isset($result[$key])) {
$result[$key] = $data[$index];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'venue1' => 1,
'venue2' => 2,
),
1 =>
array (
'venue1' => 3,
'venue2' => 4,
),
2 =>
array (
'venue1' => 5,
'venue2' => 6,
),
)
To completely remove all rows where venue values are shared, maintain a "found" array as well as a "result" array.
Code: (Demo)
$result = [];
foreach ($data as $index => $row) {
sort($row);
$key = implode('-', $row);
if (!isset($found[$key])) {
$found[$key] = true;
$result[$key] = $data[$index];
} else {
unset($result[$key]);
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'venue1' => 3,
'venue2' => 4,
),
1 =>
array (
'venue1' => 5,
'venue2' => 6,
),
)
This question already has answers here:
Put multiple arrays in one large associative array
(2 answers)
Closed 2 years ago.
I am having two arrays with same keys from two different queries.
First query result:
Array
(
[0] => Array
(
[Contribution] => 1000.00
[P_Name] => A
)
[1] => Array
(
[Contribution] => 1500.00
[P_Name] => B
)
)
Second query result:
Array
(
[0] => Array
(
[Contribution] => 100.00
[P_Name] => A
)
[1] => Array
(
[Contribution] => 200.00
[P_Name] => B
)
)
The first array may be empty and/or the second may be empty.
I want to get the create a new array that finds the sum of Contribution values where P_Name values match, like this:
Array
(
[0] => Array
(
[Contribution] => 1100.00
[P_Name] => A
)
[1] => Array
(
[Contribution] => 1700.00
[P_Name] => B
)
)
I have tried array_merge():
$result1= $this->model1->getOthersTotal($date);
$result2=$this->model1->getMiscTotal($date);
$merged_result = array_merge( $result1, $result2 );
$merged_result contains:
Array (
[0] => Array (
[Contribution] => 1000.00
[P_Name] => A
)
[1] => Array (
[Contribution] => 1001.00
[P_Name] => A
)
[2] => Array (
[Contribution] => 69.00
[P_Name] => B
)
)
Input:
$a=[['Contribution'=>1000,'P_Name'=>'A'],
['Contribution'=>1500,'P_Name'=>'B'],
['Contribution'=>2000,'P_Name'=>'C']];
$b=[['Contribution'=>100,'P_Name'=>'A'],
['Contribution'=>200,'P_Name'=>'B'],
['Contribution'=>300,'P_Name'=>'D']];
If you temporarily assign associative keys to the subarrays using array_column(), then you can leverage array_merge_recursive() to group on P_Name values, then call array_sum() to do the addition if there is more than one value to for a given P_Name.
Method #1: (Demo)
$keyed=array_merge_recursive(array_column($a,NULL,'P_Name'),array_column($b,NULL,'P_Name'));
foreach($keyed as $p_name=>$array){
$result[]=['Contribution'=>(is_array($array['Contribution'])?array_sum($array['Contribution']):$array['Contribution']),'P_Name'=>$p_name];
}
var_export($result);
Or just do a standard merge to create one array, then loop and add as you go. Finalize the output array with array_values() to reindex the elements.
Method #2: (Demo)
foreach(array_merge($a,$b) as $array){
if(isset($result[$array['P_Name']])){
$result[$array['P_Name']]['Contribution']+=$array['Contribution'];
}else{
$result[$array['P_Name']]=$array;
}
}
$result=array_values($result);
var_export($result);
Output: (from either method)
array (
0 =>
array (
'Contribution' => 1100,
'P_Name' => 'A',
),
1 =>
array (
'Contribution' => 1700,
'P_Name' => 'B',
),
2 =>
array (
'Contribution' => 2000,
'P_Name' => 'C',
),
3 =>
array (
'Contribution' => 300,
'P_Name' => 'D',
),
)
It is out of the scope of this question, but chances are the best approach would be to perform this grouping/addition via database query.
Here is a quick and dirty way to do it: Loop over both arrays, the outer loop fills the inner loop. If no match was fount, $x remails 0, and the value will be added to the inner loop. If a match is found, $x is 1 and the inner loop will break to continue the outer loop.
$a = [['a' => 10,'b' => 'g'], ['a' => 11,'b' => 'h']];
$b = [['a' => 1, 'b' => 'g'], ['a' => 2, 'b' => 'h'], ['a' => 3, 'b' => 'i']];
// now its fool proof.
function mergeData( $a, $b )
{
if( empty( $a ) && empty( $b ) )
return [];
if( empty( $a ) )
return $b;
if( empty( $b ) )
return $a;
foreach( $b AS $i => $c ) {
$x = 0;
foreach( $a AS $ii => $d ) {
if( $c['b'] == $d['b'] ) {
$a[ $ii ]['a'] += $c['a'];
$x = 1;
break;
}
}
if( !$x )
$a[] = $b[ $i ];
}
return $a;
}
Output
Array
(
[0] => Array
(
[a] => 11
[b] => g
)
[1] => Array
(
[a] => 13
[b] => h
)
[2] => Array
(
[a] => 3
[b] => i
)
)
Little bit different approach
$array1 = [
[
'Contribution' => 10,
'P_Name' => 'A'
],
[
'Contribution' => 1500,
'P_Name' => 'B'
]
];
$array2 = [
[
'Contribution' => 200,
'P_Name' => 'B'
],
[
'Contribution' => 100,
'P_Name' => 'C'
],
];
$array3 = array_map(function($elem) use (&$array2){
foreach($array2 as $i => &$a2){
if($a2['P_Name'] == $elem['P_Name']){
$a2['Contribution'] += $elem['Contribution'];
return;
}
}
return $elem;
},$array1);
$array3 = array_merge(array_filter($array3),$array2);
print_r($array3);
output:
Array
(
[0] => Array
(
[Contribution] => 10
[P_Name] => A
)
[1] => Array
(
[Contribution] => 1700
[P_Name] => B
)
[2] => Array
(
[Contribution] => 100
[P_Name] => C
)
)
You can use array_reduce(), array_map(), and array_sum():
<?php
function merge(array ...$sets)
{
/**
* group contributions by name
*/
$contributions = array_reduce(
$sets,
function (array $contributions, array $set) {
foreach ($set as $element) {
$name = $element['P_Name'];
$contribution = $element['Contribution'];
if (!array_key_exists($name, $contributions)) {
$contributions[$name] = [];
}
$contributions[$name][] = $contribution;
}
return $contributions;
},
[]
);
/**
* normalize the array so we remove the name as key, and return a tuple of name and contribution, with the desired
* structure
*/
return array_values(array_map(function (array $contribution, $name) {
return [
'Contribution' => array_sum($contribution),
'P_Name' => $name,
];
}, $contributions, array_keys($contributions)));
}
$a = [
[
'Contribution' => 1000,
'P_Name' => 'A',
],
[
'Contribution' => 1500,
'P_Name' => 'B',
],
];
$b = [
[
'Contribution' => 100,
'P_Name' => 'A',
],
[
'Contribution' => 200,
'P_Name' => 'B',
],
];
$merged = merge($a, $b);
var_dump($merged);
Note Because of using variadics, any number of arrays can be passed to merge(). Requires at least PHP 5.6, though.
For reference, see:
http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list
http://php.net/manual/en/function.array-reduce.php
http://php.net/manual/en/function.array-key-exists.php
http://php.net/manual/en/function.array-map.php
http://php.net/manual/en/function.array-sum.php
http://php.net/manual/en/function.array-values.php
http://php.net/manual/en/function.array-keys.php
For an example, see:
https://3v4l.org/EI4Tn
I have two multi dimensional array.If the key is same then i want to get the sum how do i do that.
one is-
Array
(
[2018-08-02] => Array
(
[male] => 1
[female] => 0
)
[2018-08-07] => Array
(
[male] => 1
[female] => 0
)
[2018-08-09] => Array
(
[male] => 1
[female] => 5
)
)
2nd one is-
one is-
Array
(
[2018-08-02] => Array
(
[male] => 3
[female] => 4
)
[2018-08-07] => Array
(
[male] => 1
[female] => 5
)
[2018-08-06] => Array
(
[male] => 2
[female] => 3
)
)
so my result would be
Array
(
[2018-08-02] => Array
(
[male] => 4
[female] => 4
)
[2018-08-07] => Array
(
[male] => 2
[female] => 5
)
[2018-08-09] => Array
(
[male] => 1
[female] => 5
)
[2018-08-06] => Array
(
[male] => 2
[female] => 3
)
)
Code is
foreach ($reportlist as $reportlists){
$daterep=$reportlists['act_date'];
$arr[$daterep]['male']=$reportlists['male_cnt'];
$arr[$daterep]['female']=$reportlists['female_cnt'];
}
foreach ($ureportlist as $ureportlists){
$daterep=$rueportlists['act_date'];
$arr2[$daterep]['male']=$reportlists['male_cnt'];
$arr2[$daterep]['female']=$reportlists['female_cnt'];
}
Here is your code,
<?php
function pr($arr = [])
{
echo "<pre>";
print_r($arr);
echo "</pre>";
}
$arr1 = array
(
"2018-08-02" => array
(
"male" => 1,
"female" => 0,
),
"2018-08-07" => array
(
"male" => 1,
"female" => 0,
),
"2018-08-09" => array
(
"male" => 1,
"female" => 5,
),
);
$arr2 = array
(
"2018-08-02" => array
(
"male" => 3,
"female" => 4,
),
"2018-08-07" => array
(
"male" => 1,
"female" => 5,
),
"2018-08-06" => array
(
"male" => 2,
"female" => 3,
),
);
function custom_function($arr){
$retArr = array();
foreach ($arr as $child) { // arr1, arr2
foreach ($child as $key => $value) { // traversing through keys
foreach($value as $k => $v){
if (isset($retArr[$key][$k])) { // if key is set then add
$retArr[$key][$k] += $v;
} else {
$retArr[$key][$k] = $v; // else initiate
}
}
}
}
return $retArr;
}
$result=custom_function(array($arr1,$arr2));
pr($result);die;
Here is your working code
You can use array_intersect_key() to check if there is a matching results to add them.
for example, Let's assume your two arrays are $array1 and $array2. Then you can use
$result = array_intersect_key($array1, $array2);
This will return intersection in an array. So you can identify which keys have (in your case, which dates) need to be add.
You can use array_merge_recursive to join arrrays and then sum sub-arrays in the result array
$res = array_merge_recursive($arr1, $arr2);
foreach($res as &$date) {
foreach($date as &$sex) {
$sex = array_sum((array) $sex);
}
}
print_r($res);
demo
You simply need to iterate the 2nd array and determine whether or not the date keys exist in the 1st array. isset() is the best / most efficient way to run that check.
If a date from the 2nd array does not yet exist in the first, you can simply store the row's data to the 1st array. If the date already exists in the 1st array, then you will need to add (individually) the male and female values.
Code: (Demo)
$array1 = [
'2018-08-02' => ['male' => 1, 'female' => 0],
'2018-08-07' => ['male' => 1, 'female' => 0],
'2018-08-09' => ['male' => 1, 'female' => 5]
];
$array2 = [
'2018-08-02' => ['male' => 3, 'female' => 4],
'2018-08-07' => ['male' => 1, 'female' => 5],
'2018-08-06' => ['male' => 2, 'female' => 3]
];
foreach ($array2 as $date => $row) {
if (!isset($array1[$date])) {
$array1[$date] = $row; // store row data with unique date
} else {
$array1[$date]['male'] += $row['male']; // perform addition
$array1[$date]['female'] += $row['female']; // perform addition
}
}
// ksort($array1); if you want to order by date
var_export($array1);
Output:
array (
'2018-08-02' =>
array (
'male' => 4,
'female' => 4,
),
'2018-08-07' =>
array (
'male' => 2,
'female' => 5,
),
'2018-08-09' =>
array (
'male' => 1,
'female' => 5,
),
'2018-08-06' =>
array (
'male' => 2,
'female' => 3,
),
)
I have 2 arrays of arrays which I want to merge by keys for the first step and them sum on the second step - example:
Array
(
[2017-03-01] => Array
(
[apples] => 2
[bananas] => 1
)
[2017-03-02] => Array
(
[apples] => 3
[bananas] => 6
)
[2017-03-03] => Array
(
[apples] => 0
[bananas] => 4
)
}
Array
(
[2017-03-01] => Array
(
[apples] => 3
[bananas] => 2
)
[2017-03-02] => Array
(
[apples] => 4
[bananas] => 7
)
[2017-03-03] => Array
(
[apples] => 1
[bananas] => 5
)
}
Wanted result:
Array
(
[2017-03-01] => Array
(
[apples] => 5
[bananas] => 3
)
[2017-03-02] => Array
(
[apples] => 7
[bananas] => 13
)
[2017-03-03] => Array
(
[apples] => 1
[bananas] => 9
)
}
Is there a command that does that (as a 1 single command) that will avoid looping through the arrays?
No. (obligatory additional characters)
Here's an insanely inefficient way of doing but without using any sort of for foreach or while
$result = array_map(function ($aentry, $key) use ($b) {
$bentry = $b[$key] ?? [];
$result = array_map(function ($value, $key) use ($bentry) {
return [$key, $value + ($bentry[$key] ?? 0) ];
},$aentry, array_keys($aentry));
return [ $key, array_combine(array_column($result, 0), array_column($result, 1)) ];
}, $a,array_keys($a));
$result = array_combine(array_column($result, 0), array_column($result, 1));
Example: http://sandbox.onlinephpfunctions.com/code/4c1dca3057c33dd17d0106666a497c7b08e57038
Solution without for/foreach/... , assuming that all keys are the same, you can do:
$array1 = [
'2017-03-01' => [
'apples' => 2,
'bananas' => 1,
],
'2017-03-02' => [
'apples' => 3,
'bananas' => 6,
],
'2017-03-03' => [
'apples' => 0,
'bananas' => 4,
],
];
$array2 = [
'2017-03-01' => [
'apples' => 3,
'bananas' => 2,
],
'2017-03-02' => [
'apples' => 4,
'bananas' => 7,
],
'2017-03-03' => [
'apples' => 1,
'bananas' => 5,
],
];
array_walk($array1, function(&$subarray1, $key) use($array2) {
array_walk($subarray1, function(&$element, $subkey) use($array2, $key) {
$element += $array2[$key][$subkey];
});
});
Not good performance, just for fun.
Thank you all for your answers, here is my code:
function merge_fruit_data($new_data, $old_data){
// If it's the first time running - return new data as an array
if (empty($old_data)){
return $new_data;
}
else {
foreach ( $new_data as $key => $insert_new_data ) {
if ( !$old_data[$key] ) {
$old_data[$key] = $insert_new_data;
}
else{
$old_data[$key]['apples'] += $insert_new_data['apples'];
$old_data[$key]['bananas'] += $insert_new_data['bananas'];
}
}
}
return $old_data;
}
Efficiency comments are welcome.
This may help you
`$a = array('2017-03-01' => array('apples'=> 2, 'bananas'=>1),
'2017-03-02' => array('apples'=> 3, 'bananas'=>6),
'2017-03-03' => array('apples'=> 0, 'bananas'=>4));
$b=array('2017-03-01' => array('apples'=> 3, 'bananas'=>2),
'2017-03-02' => array('apples'=> 4, 'bananas'=>7),
'2017-03-03' => array('apples'=> 1, 'bananas'=>5));
$sumArray = array();
foreach ($a as $key=>$value) {
$sumArray[$key]['apples']=($a[$key]['apples']+$b[$key]['apples']);
$sumArray[$key]['bananas']=($a[$key]['bananas']+$b[$key]['bananas']);
}
print_r($sumArray);
`
I'm looking for some simple solution (1 line) to increment all the keys +1 in an multi array.
Array (
0 => Array (
7 => 'foo',
13 => 'foo'
),
1 => Array (
2 => 'foo',
25 => 'foo'
),
4 => Array (
24 => 'foo'
)
)
RESULT ARRAY
Array (
1 => Array (
8 => 'foo',
14 => 'foo'
),
2 => Array (
3 => 'foo',
26 => 'foo'
),
5 => Array (
25 => 'foo'
)
)
$data = [
0 => [7 => 'foo', 13 => 'foo'],
1 => [2 => 'foo', 25 => 'foo'],
4 => [24 => 'foo'],
];
// top level
$data = array_combine(
array_map(function ($key) { return ++$key; }, array_keys($data)),
$data
);
// then the elements (use array_walk_recursive() if you have more levels)
array_walk(
$data,
function(&$data) {
$data = array_combine(
array_map(function ($key) { return ++$key; }, array_keys($data)),
$data
);
}
);
var_dump($data);
Demo
I'm not sure about a one-liner, especially since you have a multidimensional array. However, this is how I would do it. Honestly, all that one-liner elegance will just make things more complicated for you anyway. It's better to go with readable code in my humble opinion.
// Example array
$array = array (
0 => array (
7 => 'foo',
13 => 'foo'
),
1 => array (
2 => 'foo',
25 => 'foo'
),
4 => array (
24 => 'foo'
)
);
// My solution to increment each key and nested key
$new = array();
foreach($array as $key => $value) {
foreach($value as $inkey => $inval) {
$value[$inkey+1] = $inval;
unset($value[$inkey]);
}
$new[$key+1] = $value;
}
$array = $new;
// Display the array
echo '<pre>'; print_r($array); echo '<pre/>';