I am new in php. i need sum of two arrays but not getting right output.my arrays are coming in this way.
this is my first array.
Array
(
[1] => Array
(
[1] => Array
(
[-10] => 21787048.7293
[-5] => 21816115.9548
[-1] => 21839369.7352
[0] => 21845183.1803
[1] => 21850996.6254
[5] => 21874250.4058
[10] => 21903317.6313
)
)
[2] => Array
(
[2] => Array
(
[-10] => 21147607.6407
[-5] => 21496395.4105
[-1] => 21775425.6263
[0] => 21845183.1803
[1] => 21914940.7343
[5] => 22193970.9501
[10] => 22542758.7199
)
)
)
and this is second array.
Array
(
[1] => Array
(
[1] => Array
(
[-10] => 26101989.9443
[-5] => 26131057.1698
[-1] => 26154310.9501
[0] => 26160124.3952
[1] => 26165937.8403
[5] => 26189191.6207
[10] => 26218258.8462
)
)
[2] => Array
(
[2] => Array
(
[-10] => 25462548.8556
[-5] => 25811336.6254
[-1] => 26090366.8413
[0] => 26160124.3952
[1] => 26229881.9492
[5] => 26508912.1651
[10] => 26857699.9349
)
)
)
i need sum of these two arrays by keys.please help me out.
this is my code.my two arrays are $pretax_income, $earning.
foreach($pretax_income as $k=>$value)
{
foreach($value as $v=>$val)
{
foreach($val as $u=>$valArr) {
$comboarray[$elno] = $valArr[$k][$v][$u] + $earning[$k][$v][$u];
}
}
} print_r($comboarray);
function sum()
{
$identicalKeysArrays = func_get_args();
if (is_array($identicalKeysArrays[0])) {
$result = call_user_func_array('sum', array_map(function ($array) {
return $array[0];
}, $identicalKeysArrays));
if ($result) {
return $result;
}
}
if ($identicalKeysArrays[0]) {
$summed = array();
foreach ($identicalKeysArrays as $array) {
foreach ($array as $key => $value) {
$summed[$key] = ($summed[$key] ? $summed[$key] : 0) + $value;
}
}
return $summed;
}
}
$a = array(array(
'-1' => 23,
'2' => 2
));
$b = array(array(
'-1' => 3
));
var_dump(sum($a, $b));
Result:
array(2) { [-1]=> int(26) [2]=> int(2) }
Maybe something like this?
foreach($pretax_income as $key1=>$val1){
foreach($val1 as $key2=>$val2){
foreach($val2 as $key3=>$val3){
$comboarray[$key1][$key2][$key3]=$pretax_income[$key1][$key2][$key3]+$earning[$key1][$key2][$key3];
}
}
}
Related
I would like to arrange Array before found a specific array key. For example
Following is array.
Array(
[0] => Array([package_name] => 10.4)
[1] => Array([final_total] => 10.4)
[2] => Array([package_name] => 10.5)
[3] => Array([package_name] => 4.5)
[4] => Array([final_total] => 15)
[5] => Array([package_name] => 15.2)
[6] => Array([final_total] => 15.2)
[7] => Array([package_name] => 8.4)
[8] => 8.4
)
And I want like array.
(
[0] => Array
(
[package_name] => array([0]=>10.4),
[final_total] => 10.4
)
[1] => Array
(
[package_name] => array(
[0] => 10.5,
[1] => 4.5
),
[final_total] => 15
)
[2] => Array
(
[package_name] => array([0]=>15.2)
[final_total] => 15.2
)
[3] => Array
(
[package_name] => array([0]=>8.4)
[final_total] => 8.4
)
)
So What i want If final_total key is found from array then set previous values(package_name) of final_total in a array.
Above example you can see there are 4 final_total key's of array so i want to set each package_name's value in a array that are previous value of final_total.
Following is my code.
This is my array
$main = array(array('package_name' => 10.4),array('final_total' => 10.4),array('package_name' => 10.5),array('package_name' => 4.5),array('final_total' => 15)
,array('package_name' => 15.2),array('final_total' => 15.2));
Code.
<?php
$newArray = [];
$newPackag=[];
$previousValue='';
$currentKey=0;
$PreviousKey=0;
$i=0;
$main_keys = array_keys($main);
foreach ($main as $key => $value) {
$curtent_item[] = isset($main[$key]['package_name']) ? $main[$key]['package_name'] : '';
$currentKey = $key;
if(#$main[$key]['final_total'] ==#$value['final_total']){
$previousValue = #$value['package_name'];
$newArray[] = $previousValue;
$myarray= array(#$main[$key]['package_name']);
if (array_key_exists("final_total",$main[$key])){
if($PreviousKey ==0){
$PreviousKey = $key+1;
}else{
$PreviousKey = $key;
}
}else{
$keys = array_keys($main);
$position = array_search($key, $keys);
echo "Curent Key =".$currentKey.'PreviousKey'.$PreviousKey.'</br>';
if($currentKey != $PreviousKey){
$nextKey = $keys[$currentKey+1 ];
}
$newPackag1[] = array('package_name'=>#$myarray);
}
$mainArray = array('package'=>$newPackag1);
}
$i++;
}
echo "<pre>NE page";print_r($newPackag1);
echo "<pre>";print_r($main);
anyone has better and correct solution. Above code which i am trying not able to get desire output.
Here is the snippet with modified data(surely will work for your case too),
$result = [];$i= 0;
foreach ($main as $key => $value) {
if (is_array($value)) {
$k = key($value);$v = array_shift($value);
($k != 'final_total' ? $result[$i][$k][] = $v : $result[$i][$k] = $v);
if($k == 'final_total'){
$i++;
}
} elseif (!empty($value)) {
$result[$i]['final_total'] = $value;
}
}
print_r($result);
Demo
Output:-
Array
(
[0] => Array
(
[package_name] => Array
(
[0] => 6.5
[1] => 9
)
[final_total] => 15.5
)
[1] => Array
(
[package_name] => Array
(
[0] => 10.5
)
[final_total] => 10.5
)
[2] => Array
(
[package_name] => Array
(
[0] => 17.1
)
[final_total] => 17.1
)
[3] => Array
(
[package_name] => Array
(
[0] => 9.8
)
[final_total] => 9.8
)
[4] => Array
(
[package_name] => Array
(
[0] => 16
)
[final_total] => 16
)
[5] => Array
(
[package_name] => Array
(
[0] => 10.5
)
[final_total] => 10.5
)
)
Array (
[0] => Array ( [2] => 3 )
[1] => Array ( [12] => 32 )
[2] => Array ( [2] => 3 )
[3] => Array ( [24] => 42 )
);
How can i get the output as unique key value. I need to remove the duplicate values.
I need the o/p like this
Array (
[0] => Array ( [2] => 3 )
[1] => Array ( [12] => 32 )
[2] => Array ( [24] => 42 )
);
array_reduce is likely designed for this purpose:
$a = [ [ 2 => 3 ], [ 3 => 4 ], [ 3 => 15 ], [ 2 => 3 ] ];
array_reduce($a, function($memo, $el) {
if(false === array_search($el, $memo)) array_push($memo, $el);
return $memo;
}, array())
#⇒ array(3) {
# [0] =>
# array(1) {
# [2] =>
# int(3)
# }
# [1] =>
# array(1) {
# [3] =>
# int(4)
# }
# [2] =>
# array(1) {
# [3] =>
# int(15)
# }
#}
Try this:
$array = array(
array(2 => 3),
array(12 => 32),
array(2 => 3),
array(24 => 32)
);
echo 'before array_unique_multi:' . "\n";
print_r($array);
echo 'after array_unique_multi:' . "\n";
print_r(array_unique_multi($array));
function array_unique_multi($array)
{
$hashAry = array();
foreach($array as $k=>$v){
$hash = md5(serialize($v));
if(in_array($hash, $hashAry)) unset($array[$k]);
else $hashAry[]=$hash;
}
$array = array_values($array);
return $array;
}
Output:
before array_unique_multi:
Array
(
[0] => Array
(
[2] => 3
)
[1] => Array
(
[12] => 32
)
[2] => Array
(
[2] => 3
)
[3] => Array
(
[24] => 32
)
)
after array_unique_multi:
Array
(
[0] => Array
(
[2] => 3
)
[1] => Array
(
[12] => 32
)
[2] => Array
(
[24] => 32
)
)
You can use the function array_unique.
http://php.net/manual/fr/function.array-unique.php
foreach ($myMultiArray as $index => $doublonArray) {
$myMultiArray[$index] = array_unique($doublonArray);
}
Use this. This will store all the different key / value pairs in the new array.
$test = array(
array(2 => 3),
array(12 => 32),
array(2 => 3),
array(24 => 42)
);
$tmpArray = array();
$newArray = array();
foreach ($test as $elements) {
foreach ($elements as $key => $val) {
if (!array_key_exists($key, $tmpArray) || $tmpArray[$key] !== $val) {
$tmpArray[$key] = $val;
}
}
}
foreach ($tmpArray as $key => $val) {
$newArray[] = array($key => $val);
}
var_dump($newArray);
Output:
array
0 =>
array
2 => int 3
1 =>
array
12 => int 32
2 =>
array
24 => int 42
$check_arr = Array (
[0] => Array ( [13] => 1 )
[1] => Array ( [13] => 3 )
[2] => Array ( [13] => 1 )
[3] => Array ( [10] => 3 )
) ;
print_r(array_unique($check_arr));
This my result.
Array ( [0] => Array ( [13] => 1 ) );
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 5 months ago.
here: Transforming array values in elements of a subarray using PHP I asked quite the same, but the arrays were flatter. I tried to adapt the code, but unfortunately without success.
How could I merge following arrays so the second array won't be added after the end of the first array, but each subarray of the first array will receive the correspondent values of subarrays of the second. In other words, I want to merge the subarrays. Here my example:
Array 01:
Array01 (
[0] => Array
(
[0] => 40292633
[1] => 412
)
[1] => Array
(
[0] => 41785603
[1] => 382
)
[2] => Array
(
[0] => 48792980
[1] => 373
)
[3] => Array
(
[0] => 44741143
[1] => 329
))
Array 02:
Array02(
[0] => Array
(
[0] => 3460581
[1] => 1407424B1
[2] => 951753
)
[1] => Array
(
[0] => 3484251
[1] => 1028325B1
[2] => 159357
)
[2] => Array
(
[0] => 3519102
[1] => 0586365A1
[2] => 456654
)
[3] => Array
(
[0] => 3529714
[1] => 1059876A1
[2] => 852258
))
Final array:
finalArray(
[0] => Array
(
[0] => 40292633
[1] => 412
[2] => 3460581
[3] => 1407424B1
[4] => 951753
)
[1] => Array
(
[0] => 41785603
[1] => 382
[2] => 3484251
[3] => 1028325B1
[4] => 159357
)
[2] => Array
(
[0] => 48792980
[1] => 373
[2] => 3519102
[3] => 0586365A1
[4] => 456654
)
[3] => Array
(
[0] => 44741143
[1] => 329
[2] => 3529714
[3] => 1059876A1
[4] => 852258
))
Many thanks in advance!
try this code
function merge_arrays($a1, $a2) {
return array($a1, $a2);
}
$result = array_map("merge_arrays", $a1, $a2);
foreach($result as $nr)
{
$nres[] = array_merge ($nr[0], $nr[1]);
}
Try this:
$result = array();
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
foreach($keys as $key) {
if( !array_key_exists($key, $arr1) ) {
$result[$key] = $arr2;
} else if( !array_key_exists($key, $arr2) ) {
$result[$key] = $arr1;
} else {
$result[$key] = array_merge($arr1[$key], $arr2[$key]);
}
}
It does not assume that both arrays have the same keys.
If you'd like to use array_map, this is an equivalent solution:
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
$result = array_map(function($key) use ($arr1,$arr2) {
if( !array_key_exists($key, $arr1) ) {
return $arr2;
} else if( !array_key_exists($key, $arr2) ) {
return $arr1;
}
return array_merge($arr1[$key], $arr2[$key]);
},
$keys);
If you're sure both arrays have the same keys, you can try this:
$result = array();
foreach($arr1 as $key => $arr1val) {
$result[$key] = array_merge($arr1val, $arr2[$key]);
}
I have a multidimensional array as follows
Array
(
[0] => Array
(
[1] => val01
[2] => val02
[3] =>
)
[1] => Array
(
[1] => val11
[2] => Array
(
[sub1] => 1
[sub2] =>
[sub3] => Array
(
[primarysub1] =>
[primarysub2] => pmy2
)
)
[3] => val3
)
[2] => Array
(
[1] => val21
[2] =>
[3] => val23
)
)
And I need to filter the empty values and expected output is as follow.
Array
(
[0] => Array
(
[1] => val01
[2] => val02
)
[1] => Array
(
[1] => val11
[2] => Array
(
[sub1] => 1
[sub3] => Array
(
[primarysub2] => pmy2
)
)
[3] => val3
)
[2] => Array
(
[1] => val21
[3] => val23
)
)
Could somebody help me out with a best way using PHP?
You have to use recursive function for this. hope this might help you
<?php
$rs = unsetValues($arr);
function unsetValues($a)
{
foreach($a as $k=>$v)
{
if(is_array($v))
{
$arr2[$k] = unsetValues($v);
} else {
if($v!="")
$arr2[$k] = $v;
}
}
return $arr2;
}
?>
function filter($input, $callback = null)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = filter($value, $callback);
}
}
return array_filter($input, $callback);
}
function remove_empty($val)
{
return !empty($val);
}
$test_arr = array(
0 => array(1 => "val01", 2 => "val02", 3 => ""),
1 => array(1 => "val11", 2 => array("sub1" => 1, "sub2" => "", "sub3" => array("primarysub1" => "", "primarysub2" => "pmy2")), 3 => "val3"),
2 => array(1 => "val21", 2 => "", 3 => "val23")
);
echo '<pre>' . print_r($test_arr, true) . '</pre>';
// filter empty
$result = filter($test_arr, remove_empty);
echo '<pre>' . print_r($result, true) . '</pre>';
How to arrange this array by last inner index ( 0, 1, 2 ) and get the value of the last inner index as the value of each second index:
Array
(
[text] => Array
(
[grid] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[image] => Array
(
[0] =>
[1] =>
[2] =>
)
[align] => Array
(
[0] => left
[1] => right
[2] => left
)
[title] => Array
(
[0] =>
[1] =>
[2] =>
)
[content] => Array
(
[0] =>
[1] =>
[2] =>
)
)
)
And have the results as below:
Array
(
[text] => Array
(
[0] => Array
(
[grid] => 3
[image] =>
[align] => left
[title] =>
[content] =>
)
[1] => Array
(
[grid] => 4
[image] =>
[align] => right
[title] =>
[content] =>
)
[2] => Array
(
[grid] => 5
[image] =>
[align] => left
[title] =>
[content] =>
)
)
)
This will do the work
function restructure($arr){
$newArr = array();
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2){
$newArr[$k][$k2][$k1] = $v2;
}
}
}
return $newArr;
}
As SiGanteng suggested, i dont see other ways than a for/foreach loop:
function buildArray($source, $key = false)
{
// Build the new array
$new_array = array();
// Define groups
$groups = $key === false ? array_keys($source) : array($key);
foreach($groups AS $group)
{
// Get the keys
$keys = array_keys($array[$group]);
// Count the values
$num_entries = count($array[$group][$keys[0]]);
for($i = 0; $i < $num_entries; $i++)
{
foreach($keys AS $key)
{
$new_array[$group][$i][$key] = $array[$group][$key][$i];
}
}
}
return $new_array;
}
This allow you to define the key you need to build; If not specified, the function build the array for every key.
This should work.
function formatit($arr) {
$new = array();
foreach($arr as $k=>$v) {
foreach($v as $k1=>$v1) {
$new[$k1][$k] = $v1;
}
}
return $new;
}
Tested. Call it as
$arr['text'] = formatit($arr['text']);
http://ideone.com/rPzuR