PHP multi dimentional array sum and make unique [duplicate] - php

This question already has answers here:
Associative array, sum values of the same key
(5 answers)
Closed 3 years ago.
Following is my array. I need to add its Sum field according to each emp_firstname. Some has only one time coming, some coming two times. how can we sum the field and make the array unique?
Array
(
[0] => Array
(
[emp_firstname] => Alistair
[non_pm] => AMZ
[sum] => 2
)
[1] => Array
(
[emp_firstname] => Shakkeer
[non_pm] => SHK
[sum] => 3
)
[2] => Array
(
[emp_firstname] => Waqas
[non_pm] => WAS
[sum] => 12
)
[3] => Array
(
[emp_firstname] => Zain
[non_pm] => ZAI
[sum] => 9
)
[4] => Array
(
[emp_firstname] => Shakkeer
[gud_pmeditor] => SHK
[sum] => 4
)
[5] => Array
(
[emp_firstname] => Zain
[gud_pmeditor] => ZAI
[sum] => 2
)
)

You can get the desired result using this approach
$res=[];
foreach($arr as $val){
if(array_key_exists($val['emp_firstname'], $res))
$res[$val['emp_firstname']]['sum'] = ($res[$val['emp_firstname']]['sum'] + $val['sum']);
else
$res[$val['emp_firstname']] = $val;
}
Live Demo

Related

Convert nested associative array to single array in php [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have nested array with key & value pair. i want to convert it in single array.
/* This is current array */
Array
(
[id] => Array
(
[0] => 1
[1] => 2
)
[qty] => Array
(
[0] => 1
[1] => 1
)
[price] => Array
(
[0] => 364.41
[1] => 300
)
[amount] => Array
(
[0] => 364.41
[1] => 300
)
)
/*Now, I want this type of array*/
Array
(
[0] => Array
(
[id] => 1
[qty] => 1
[price] => 364.41
[amount] => 364.41
)
[1] => Array
(
[id] => 2
[qty] => 1
[price] => 300
[amount] => 300
)
)
I have tried array_walk and some other solutions but i could not find any proper solution
Thanks in advance
Main array is your nested array and Collection is the result you want
foreach($mainArray["id"] as $key => $value ){
$collection[$key] = ['id' => $value];
foreach(array_keys($mainArray) as $arrayKeysOfmainArray){
if(!array_key_exists($arrayKeysOfmainArray, $collection)){
$collection[$key][$arrayKeysOfmainArray] = $mainArray[$arrayKeysOfmainArray][$key];
}
}
}
print_r($collection);
$mainarray['id'] = Array(1,2,3);
$mainarray['qty'] = Array(1,1,4);
$mainarray['price'] = Array(364.41,300,500);
$mainarray['amount'] = Array(364.41,300,600);
$new = [];
foreach($mainarray as $key=> $singleArray){
if(count($singleArray) > 0){
foreach($singleArray as $childKey=> $value){
$new[$childKey][$key] = $value;
}
};
}`

Sort multidimensional array by given key PHP [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
I have a multidimensional array that needs to be reoreder, the array look like this :
[products] => Array
(
[149] => Array
(
[name] => Ichikami1
[qty] => 2
)
[150] => Array
(
[name] => Ichikami2
[qty] => 4
)
[377] => Array
(
[name] => BCL
[qty] => 2
)
)
inside the child array there is 'qty' index, i want to sort the child array by 'qty' index in descending order, so it will look like this:
[products] => Array
(
[0] => Array
(
[name] => Ichikami2
[qty] => 4
)
[1] => Array
(
[name] => Ichikami1
[qty] => 2
)
[2] => Array
(
[name] => BCL
[qty] => 2
)
)
is there a way to do this?
Simply by using array_multisort(), You can do this like:
$qty = array();
foreach ($products as $key => $row)
{
$qty[$key] = $row['qty'];
}
array_multisort($qty, SORT_DESC, $products);

Sum values in multidimensional array by key [duplicate]

This question already has answers here:
Group array data on one column and sum data from another column
(5 answers)
Closed 1 year ago.
I have read a lot of answers here on SO but havent been able to sort this out.
I have multidimensional array that looks like this:
Array
(
[0] => Array
(
[0] =>
[1] => 655
)
[1] => Array
(
[0] => IT-82
[1] => 14
)
[2] => Array
(
[0] => IT-21
[1] => 5
)
[3] => Array
(
[0] => IT-82
[1] => 7
)
[4] => Array
(
[0] =>
[1] => 3
)
[5] => Array
(
[0] => IT-21
[1] => 4
)
[6] => Array
(
[0] =>
[1] => 3
)
[7] => Array
(
[0] => IT-21
[1] => 3
)
[8] => Array
(
[0] => IT-72
[1] => 7
)
[9] => Array
(
[0] => IT-75
[1] => 22
)
[10] => Array
(
[0] => IT-75
[1] => 3
)
)
I would like to sum the values according to the keys ending with a single array like:
Array
(
=> 661
IT-82 => 21
IT-21 => 12
IT-82 => 12
IT-72 => 7
IT-75 => 25
)
Tried with
foreach ($array as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
but this only returned the sum of all the values.
Any help appreciated.
Try:
$sumArray = array();
foreach ($array as $k=>$subArray) { //loop through array
if(isset($sumArray[$subArray[0]]))
$sumArray[$subArray[0]] += $subArray[1]; // set 0th index as key and 1st as value and add value to current index
else
$sumArray[$subArray[0]] = $subArray[1];
}
print_r($sumArray);
Output:
Array
(
[] => 661
[IT-82] => 21
[IT-21] => 12
[IT-72] => 7
[IT-75] => 25
)
I suppose it should be:
foreach ($array as $subArray) {
$sumArray[$subArray[0]] += $subArray[1];
}

Modify array keys of an associative array [duplicate]

This question already has answers here:
Replace keys in an array based on another lookup/mapping array
(25 answers)
Closed 9 years ago.
I have this array
$the_posted = Array
(
0 => Array
(
0 => 1,
1 => 2,
2 => 3,
3 => 4,
),
1 => Array
(
0 => 5,
1 => 6,
2 => 7,
3 => 8,
)
);
whose keys i need to modify.I trying to modify the array keys like
$all_array_keys = array_keys($the_posted);
foreach ( array_keys($the_posted) as $k=>$v )
{
$all_array_keys[$k]= rand();
}
echo '<pre>';
print_r($all_array_keys);
echo "<hr/>";
print_r($the_posted);
echo '<pre>';
I get this result
Array
(
[0] => 25642
[1] => 8731
)
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[1] => Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
)
The change in keys is not reflected in the final array.How do i make this work?.
You can use following code :
foreach ( array_keys($the_posted) as $k=>$v )
{
$new_key = rand();
$new_posted[$new_key] = $the_posted[$v];
unset($the_posted[$v])
}
Here, we have created a new array $new_posted which will have data with new keys like this :
Array
(
[28228] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[23341] => Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
)
To change the key of an item, do something like this:
$the_posted[$newkey] = $the_posted[$oldkey];
unset($the_posted[$oldkey]);
So in your case:
foreach ( $the_posted as $k=>$v )
{
$newkey = rand();
while(isset($the_posted[$newkey]))
$newkey = rand();
$the_posted[$newkey] = $the_posted[$k];
unset($the_posted[$k]);
}

how to split an single array in to arrays - PHP

I need to split single array in to multiple arrays.
For example:
Array
(
[0] => Array
(
[pageviews] => 26
[visits] => 20
)
[1] => Array
(
[pageviews] => 9
[visits] => 4
)
[2] => Array
(
[pageviews] => 18
[visits] => 9
)
)
I need to split the array like below:
Array
(
[ga:pageviews] => 26
[ga:visits] => 20
)
Array
(
[ga:pageviews] => 9
[ga:visits] => 4
)
Array
(
[ga:pageviews] => 18
[ga:visits] => 9
)
How can i do this?
Any help will be thankful and grateful...
Thanks in advance..
Ok, so using foreach:
foreach ( $original as $item ) {
var_dump( array(
'ga:pageviews' => $item['pageviews'],
'ga:visits' => $item['visits'],
) );
}
try
$array = Array
(
[0] => Array
(
[pageviews] => 26
[visits] => 20
)
[1] => Array
(
[pageviews] => 9
[visits] => 4
)
[2] => Array
(
[pageviews] => 18
[visits] => 9
)
)
for($x=0; $x<count($array); $x++){
$newArray = $array[$x]; // that extract the second array, containing pageview and visits.
}
According to your samples, you seem to want to split one variable into multiple variables (or perhaps you used incorrect notation in the 2nd one?). If that is the case, and you know how many arrays are in the starting variable, you can do this:
list($one, $two, $three) = $originalArray;
If you don't know how many arrays are in the original array, or there is more than a handful, I have to wonder why you would want to or need to do this in the first place...

Categories