How to sum value of array if some values are same? - php

I have following array
Array (
[0] => 1995,17500
[1] => 2052,17500
[2] => 2052,17500
[3] => 2236,30000
[4] => 2235,15000
[5] => 2235,40000
);
I have exploded the value with comma ,.
foreach($array as $key=>$value) {
$newar = explode(',', $value);
}
So I have similar first value i.e $newar["0"]. For example 2235 and 2052. And I would like to have sum of second value i.e $newar["1"].
How can I achieve following result with PHP.
Array (
[0] => 1995, 17500
[1] => 2052, 35000
[2] => 2036, 30000
[3] => 2235, 55000
)

You can create a new array key/index based and use array_key_exists to check if the key already exists. If so sum the value, if not create the key. This is not the exact result you want, but it's neater.
$newar = []; //New Array
foreach($array as $value) {
$explArr = explode(',', $value);
if(array_key_exists($explArr[0], $newar)){ //Check if the key already exists
$newar[$explArr[0]] += $explArr[1]; //Sum value to existing key
} else {
$newar[$explArr[0]] = $explArr[1]; //Create key
}
}
Your result array will look like this:
Array (
[1995] => 17500
[2052] => 35000
[2036] => 30000
[2235] => 55000
)

Related

How can I add a value from one array to a new empty associative array in PHP?

I want to count how many times a number exists in my array, but I want to do it like this.
I have an empty array like this:
$aNumberArray = array();
And I have an array like this:
$aArray = (4,4,5,7,4,8,7,9,4,3);
This is my code so far:
foreach ($aArray as $value) {
if (in_array($value, $aNumberArray)) {
// increase value in $aNumerArray.
}else{
// add $value from $aArray to $aNumberArray as key and as value add 1.
}
}
I want to know how I can add the value from $aArray to $aNumberArray as a key and I want to add number 1 as value. When It excist it most increase the value from $aArray.
Here you go:
<?php
$aNumberArray = array();
$aArray = array(4,4,5,7,4,8,7,9,4,3);
foreach ($aArray as $value) {
if (!isset($aNumberArray[$value])) {
$aNumberArray[$value] = 0;
}
$aNumberArray[$value] += 1;
}
print_r($aNumberArray);
Will give you:
Array
(
[4] => 4
[5] => 1
[7] => 2
[8] => 1
[9] => 1
[3] => 1
)
Check output of it
$aArray = [4,4,5,7,4,8,7,9,4,3]; // correct this array format
print_r(array_count_values($aArray));
Output
Array
(
[4] => 4
[5] => 1
[7] => 2
[8] => 1
[9] => 1
[3] => 1
)
Demo
array_count_values — Counts all the values of an array

Combine values by key in single array

Nothing really does exactly what I want to achieve. I have the following array:
Array(
[0] => Array
(
[chicken] => 7
)
[1] => Array
(
[cheese] => 9
)
[2] => Array
(
[marinade] => 3
)
[3] => Array
(
[cookbook] => 7
)
[4] => Array
(
[chicken] => 11
)
[5] => Array
(
[cheese] => 6
)
[6] => Array
(
[marinade] => 12
)
)
I want to sum all values by their key. If the key is multiple times in the array, like chicken, I want to sum the values.
array
(
[chicken] => 18,
[cheese] => 16
... etc
)
So you'd first need a loop to iterate through the first array to get the second-level arrays. Then you can get the current key and value from each of those arrays, summing the values in a new array associated by the key.
// where the sums will live
$sum = [];
foreach($array as $item) {
$key = key($item);
$value = current($item);
if (!array_key_exists($key, $sum)) {
// define the initial sum of $key as 0
$sum[$key] = 0;
}
// add value to the sum of $key
$sum[$key] += $value;
}
Here simple example i hope it's help you.
$result = array();
foreach($data as $key => $value)
{
$valueKey = key($value);
$result[$valueKey] += $value[$valueKey];
}

php array index entries auto populate

I have two arrays
$arrayOne = ( [0] => 4892 [1] => 98508 [2] => 7834 [3] => 47826 )
$arrayTwo = ( [1] => Car [2] => Computer )
Notice the elements of arrayTwo does not start at 0, but which is what i want because it will be used to pair with arrayOne, ie. Car matches with 98508.
I want to populate the second array where there are no entries with a string for example arrayTwo output:
$arrayTwo = ([0] => its empty [1] => Car [2] => Computer [3] => its empty
How can i achieve this desired output?
Loop the first and check for the key. If it doesn't exist, set it:
foreach($arrayOne as $key => $val) {
if(!isset($arrayTwo[$key])) { $arrayTwo[$key] = 'its empty'; }
}
foreach ($arrayOne as $key => $value){
if (!array_key_exists($key, $arrayTwo)){
$arrayTwo[$key] = 'its empty';
}
}

How can I remove a key value from foreach in codeigniter?

I have an array like this. Array name is amount
Array
(
[0] => Array
(
[amount] => 0
)
[1] => Array
(
[amount] => 0
)
[2] => Array
(
[amount] => 0
)
[3] => Array
(
[amount] => 207.2
)
[4] => Array
(
[amount] => 1458.8
)
[5] => Array
(
[amount] => 207.2
)
)
Here I want to remove last key value from list when I display it into a table.
foreach(amount as key => values)
{
print_r($values);
echo"<br>";
}
When this print it should be show without last key element. like this
0
0
0
207.21
1458.8
I hope somebody will help me to solve this problem.
Hopefully
If you want to remove the last value if the array, you can:
First option, you can use array_pop to remove the last value.
//Assign the values on a temp array
$tempAmount = $amount;
//Remove the last value of the temp array.
array_pop( $tempAmount );
//You can loop as usual the temp array
http://php.net/manual/en/function.array-pop.php
Second option: You can use a condition, like:
foreach($amount as $key => $values)
{
if ( $key < count( $amount ) - 1 ) {
print_r($values);
echo"<br>";
}
}
You can use array_slice function to extract a sub-array from the index 0 and having size the size of old array - 1:
$newArray = array_slice($oldArray, 0, count($oldArray) -1);

Convert complex numerical array to associative array

I have an array data that look like this :
Array (
[0] => Array (
[0] => Name:
[1] => John W.
[2] => Registration ID:
[3] => 36
)
[1] => Array (
[0] =>Age:
[1] => 35
[2] => Height:
[3] => 5'11"
)
[3] => Array (
[0] => Sex:
[1] => M
[2] => Weight:
[3] => 200lbs
)
[4] => Array (
[0] => Address
)
[5] => Array (
[0] => 6824 crestwood dr delphi, IN 46923
))
And I want to convert it to associative array like this :
Array(
['Name']=> John W.
['Registration ID']=> 36
['Age']=> 35
['Height'] => 5'11''
['Sex']=>M
['Weight']=>200lbs
['Address']=>6824 crestwood dr delphi, IN 46923
)
I have no idea at all how to do this, since the supposed to be array column header were also in sequence, so it makes difficult to convert this array.
Any help I appreciate, thx.
Given your origin array is called $origin , you can do it like this:
$merged = array();
foreach($origin as $val) {
$merged = array_merge($merged, $val);
}
$tot = count($merged) - 1;
for ($i=0;$i<$tot;$i+=2) {
$result[$merged[$i]] = $merged[$i+1];
}
var_dump($result); // To test the resulting array
Firstly, I use array_merge() to flatten the $origin array to only one dimension/depth, so we later iterate it (stepping each 2 items per iteration) and assigning each pair of items ($i and $i+1) to the resulting array.
Looks like, for the first 3 children, you can just assign the even value to the previous element as key. Then, assign the fourth one as key for fifth element.
$result = array();
foreach ($array as $key => $value)
{
if ($key < 4) {
$elements = array_values($value);
$result[$elements[0]] = $elements[1];
$result[$elements[2]] = $elements[3];
}
if ($key == 4)
$fifthkey = $value;
if ($key == 5)
$result[$fifthkey] = $value;
}
Also, note that you have to escape your height string quotes.

Categories