Below is my array:
Array
(
[3] => Array
(
[2] => 3
[4] => 1
[5] => 2
[6] => 2
)
[5] => Array
(
[2] => 1
[3] => 2
[4] => 3
[6] => 3
)
In this array I want to find the maximum number and if the array contains the same maximum values then we choose one maximum value randomly.
Expected output like below:
Array
(
[3] => Array
(
[2] => 3
)
[5] => Array
(
[4] => 3
)
[6] => Array
(
[2] => 3
)
)
This is what I've tried:
$found = Array( [3] => Array ( [2] => 3 [4] => 1 [5] => 2 [6] => 2 ) [5] => Array ( [2] => 1 [3] => 2 [4] => 3 [6] => 3 ) [6] => Array ( [2] => 3 [3] => 2 [4] => 2 [5] => 3 ))
$fmaxnum = array();
foreach($found as $fk => $fv){
$fmaxnum[$fk] = max($fv);
}
echo "<pre>";print_r($fmaxnum);echo "</pre>"
You will get max value with max() but for index you have to use array_keys()
You can try this:
$found = Array ( '3' => Array ( '2' => 3 ,'4' => 1, '5' => 2, '6' => 2 ),
'5' => Array ( '2' => 1 ,'3' => 2, '4' => 3, '6' => 3 ),
'6' => Array ( '2' => 3 ,'3' => 2, '4' => 2, '5' => 3 )
);
$fmaxnum = array();
foreach($found as $fk => $fv){
$max_key = array_keys($fv, max($fv));
$fmaxnum[$fk] = array(
($max_key[0]) => max($fv) /* This will give small index value */
/* (count($max_key)-1) => => max($fv) // this will give highest index value */
);
}
echo "<pre>";
print_r($fmaxnum);
echo "</pre>";
Simple solution with array_map and array_keys functions:
// supposing $arr is your initial array
$arrMax = array_map(function($v){
$maximum = max($v);
return [array_keys($v, $maximum)[0] => $maximum];
}, $arr);
print_r($arrMax);
The output:
Array
(
[3] => Array
(
[2] => 3
)
[5] => Array
(
[4] => 3
)
[6] => Array
(
[2] => 3
)
)
if you just want to know the highest value as I understood
In this array I want to find the maximum number and if the array
contains the same maximum values then we choose one maximum value
randomly.
you could just do this
$array = [['3','1','2','2'],['1','2','3','3'],['2','1','1','3']];
$result = [];
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
$result[] = $value;
}
echo max($result);
this will foreach all the array and push the values on $result, then you just have one level array and easily use max function
Related
Actual I get
Array
(
[0] => Array
(
[name] => person1
[value] => 11
)
[1] => Array
(
[name] => person2
[value] => 5
)
[2] => Array
(
[name] => person2
[value] => 5
)
[3] => Array
(
[name] => person4
[value] => 10
)
)
Actually i need
Array
(
[0] => Array
(
[name] => person1
[value] => 11
)
//here i want new row index 1 array
[1] => Array
(
[name] => total
[value] => 11 //this value is the sum of index 1
)
[2] => Array
(
[name] => person2
[value] => 5
)
[3] => Array
(
[name] => person2
[value] => 5
)
// here i want add new line index 4 array
[4] => Array
(
[name] => total
[value] => 10 //this value is the sum of index 2 and 3
)
[5] => Array
(
[name] => person4
[value] => 10
)
// here i want add new line index 6 array
[6] => Array
(
[name] => total
[value] => 10 //this value is the sum of index 5
)
)
may this help you i have tried some step making grouping but sum is not correct but code may help you
$a = Array
(
0 => Array
(
'name' => 'person1',
'value' => 11,
),
1 => Array
(
'name' => 'person2',
'value' => 5,
),
2 => Array
(
'name' => 'person2',
'value' => 5,
),
3 => Array
(
'name' => 'person4',
'value' => 10,
)
);
foreach($a as $c){
$d[]=$c['name'];
$e[]=$c['value'];
}
//print_r($d);
$group = array();
foreach($d as $key=>$val){
$group[$val][] = $e[$key];
}
// this loop for check the max number and count total price
$data = array();
$total = 0;
foreach($group as $key=>$val){
for($i=0;$i<count($val);$i++){
$data[]['name'] = $key;
$data[]['value'] = $val[$i];
$suma[] = $val[$i];
}
$sunmb =array_sum($suma);
$data[]['name'] = 'total';
$data[]['value'] = $sunmb;
}
print_r($data);
You can check code run here https://wtools.io/php-sandbox/b1LD
I have below two arrays with strings and numbers,
I want to keep only one strings header and sum numeric value from each key value with another array.
I have tried many of the solutions available online but nothing found as required.
$array1 =
Array
(
[0] => Array
(
[0] => Out Of Warranty
[1] => Total Orders
[2] => Total Qty
[3] => Canceled Orders
)
[1] => Array
(
[0] => Today<br/>(04-26-2020)
[1] => 1
[2] => 1
[3] => 0
)
[2] => Array
(
[0] => Yesterday<br/>(04-25-2020)
[1] => 0
[2] => 0
[3] => 0
)
[3] => Array
(
[0] => This Week<br/>(04-20-2020 - 04-26-2020)
[1] => 22
[2] => 39
[3] => 0
)
[4] => Array
(
[0] => Last Week<br/>(04-13-2020 - 04-19-2020)
[1] => 7
[2] => 7
[3] => 0
)
[5] => Array
(
[0] => This Month<br/>(04-01-2020 - 04-26-2020)
[1] => 29
[2] => 46
[3] => 0
)
[6] => Array
(
[0] => This Year<br/>(01-01-2020 - 04-26-2020)
[1] => 30
[2] => 47
[3] => 0
)
)
$array2 =
Array
(
[0] => Array
(
[0] => Out Of Warranty
[1] => Total Orders
[2] => Total Qty
[3] => Canceled Orders
)
[1] => Array
(
[0] => Today<br/>(04-24-2020)
[1] => 10
[2] => 10
[3] => 0
)
[2] => Array
(
[0] => Yesterday<br/>(04-23-2020)
[1] => 7
[2] => 7
[3] => 0
)
[3] => Array
(
[0] => This Week<br/>(04-20-2020 - 04-24-2020)
[1] => 51
[2] => 51
[3] => 0
)
[4] => Array
(
[0] => Last Week<br/>(04-13-2020 - 04-19-2020)
[1] => 31
[2] => 31
[3] => 0
)
[5] => Array
(
[0] => This Month<br/>(04-01-2020 - 04-24-2020)
[1] => 93
[2] => 93
[3] => 0
)
[6] => Array
(
[0] => This Year<br/>(01-01-2020 - 04-24-2020)
[1] => 1281
[2] => 1281
[3] => 1
)
)
Expected output as Strings should be use only once and numbers should be added to each other.
For example output should be 6 index i.e sum of 6 index from array1 and array2 -
[6] => Array
(
[0] => This Year<br/>(01-01-2020 - 04-26-2020)
[1] => 1311
[2] => 1328
[3] => 1
)
If your arrays are always sorted in the same order:
$newItems = [];
foreach ($array1 as $key => $item) {
$newItems[] = [
$item[0],
$item[1] + $array2[$key][1],
$item[2] + $array2[$key][2],
$item[3] + $array2[$key][3],
];
}
If keys in arrays are in distinct orders:
$newItems = [];
foreach ($array1 as $item) {
$name = $item[0];
$newItems[$name] = $item;
}
foreach ($array2 as $item) {
$name = $item[0];
$newItems[$name][1] += $item[1];
$newItems[$name][2] += $item[2];
$newItems[$name][3] += $item[3];
}
// apply array_values to get 0-indexed array
$newItems = array_values($newItems);
Only iterate the elements that are necessary for your summing logic. Using 2 loops will be the most concise and deliberate way to sum columns [1], [2], and [3].
You can create a new output array, but merely adding the second array values to the first affords a simpler addition-assignment syntax.
Code: (Demo)
for ($i = 1, $size = count($array2); $i < $size; ++$i) { // don't iterate [0] subarray (headers)
for ($col = 1; $col <= 3; ++$col) { // don't iterate [0] element (name)
$array1[$i][$col] += $array2[$i][$col];
}
}
var_export($array1);
Output:
array (
0 =>
array (
0 => 'Out Of Warranty',
1 => 'Total Orders',
2 => 'Total Qty',
3 => 'Canceled Orders',
),
1 =>
array (
0 => 'Today<br/>(04-26-2020)',
1 => 11,
2 => 11,
3 => 0,
),
2 =>
array (
0 => 'Yesterday<br/>(04-25-2020)',
1 => 7,
2 => 7,
3 => 0,
),
3 =>
array (
0 => 'This Week<br/>(04-20-2020 - 04-26-2020)',
1 => 73,
2 => 90,
3 => 0,
),
4 =>
array (
0 => 'Last Week<br/>(04-13-2020 - 04-19-2020)',
1 => 38,
2 => 38,
3 => 0,
),
5 =>
array (
0 => 'This Month<br/>(04-01-2020 - 04-26-2020)',
1 => 122,
2 => 139,
3 => 0,
),
6 =>
array (
0 => 'This Year<br/>(01-01-2020 - 04-26-2020)',
1 => 1311,
2 => 1328,
3 => 1,
),
)
I have the following array
$a = array(0 => 'Item',
1 => 'Wattles',
2 => 'Types',
3 => 'Compost',
4=> 'Estimated',
5 => '123',
6 => 'Actual',
7 => '12',
);
That is sorted with the following code.
echo "<pre>";
print_r($a);
$a_len = count($a);
$fnl = array();
$i = 0;
while($i<$a_len){
$fnl[$a[$i]] = $a[++$i];
$i++;
}
print_r($fnl);
It prints correctly
Array
(
[Item] => Wattles
[Types] => Compost
[Estimated Qty] => 123
[Actual Qty] => 12
)
until i add multiple entries.
Array
(
[0] => Item
[1] => Wattles
[2] => Types
[3] => Compost
[4] => Estimated Qty
[5] => 123
[6] => Actual Qty
[7] => 12
[8] => Item
[9] => Silt Fence
[10] => Types
[11] => Straw
[12] => Estimated Qty
[13] => 45
[14] => Actual Qty
[15] => 142
)
I need to make this add items in a multidimensional array.
$items = array
(
array("Wattles","Silt Fence), //items
array("Compost","Straw"), //types
array(123,45), //estimated quantity
array(12,142) //actual quantity
);
There are a few given numbers. There are exactly 4 entries (8 items) before the list repeats itself.
I have been stuck on this portion for hours, and don't know how to get my code working as I want it to.
To get the expected result with string keys you can do:
foreach(array_chunk($a, 2) as $pairs) {
$result[$pairs[0]][] = $pairs[1];
}
Yields:
Array
(
[Item] => Array
(
[0] => Wattles
[1] => Silt Fence
)
[Types] => Array
(
[0] => Compost
[1] => Straw
)
[Estimated] => Array
(
[0] => 123
[1] => 45
)
[Actual] => Array
(
[0] => 12
[1] => 142
)
)
Then if you want it numerically indexed:
$result = array_values($result);
You have your structure for a multidimensional array wrong. You should construct your array like this:
$a = array(
0 => array(
'Item' => 'Wattles',
'Types' => 'Compost',
'Estimated' => 123,
'Actual' => 12
)
);
Then to add to it:
$a[] = array(
'Item' => 'Silt Fence',
'Types' => 'Straw',
'Estimated' => 45,
'Actual' => 142
);
Render it out to see the results which are what I think you are looking for.
print_r($a);
I can post a link if you want to learn how to sort multidimensional arrays by sub-array values if you need.
Here is my multiple array
Array
(
[3432] => Array
(
[2] => 7.2
[3] => 1.5
[4] => 2.1
)
[11350] => Array
(
[2] => 23.168
[3] => 4.344
[4] => 4.344
)
[12175] => Array
(
[2] => 8.112
[3] => 6.288
[4] => 4.344
)
[12306] => Array
(
[2] => 9.036
[3] => 0.9
[4] => 0
)
)
and i need to find a min sum value.
When i use php min function i get 10.8
Array
(
[2] => 7.2
[3] => 1.5
[4] => 2.1
)
10.8
but the minimun of all array is 9.936
[12306] => Array
(
[2] => 9.036
[3] => 0.9
[4] => 0
)
Try this:
<?php
$input = array(
11350 => array(
2 => 23.168,
3 => 4.344,
4 => 4.344,
),
12175 => array(
2 => 8.112,
3 => 6.288,
4 => 4.344,
),
12306 => array(
2 => 9.036,
3 => 0.9,
4 => 0,
),
);
$sums = array();
foreach ($input as $row) {
$sums[] = array_sum($row);
}
print_r(min($sums));
EDIT
or more PHP way:
print_r(min(array_map('array_sum', $input)));
I need to sum up some values from subarrays in an array.
I have this array
Array
(
[smecid_2] => Array
(
[0] => 1
[1] => SMEC 55.6
[2] => 960
[3] => 864
[4] => 960
[5] => 864
)
[smecid_6] => Array
(
[0] => 3
[1] => SMEC 55.6 ATEX EX
[2] => 1290
[3] => 1161
[4] => 3870
[5] => 3483
)
)
What I want to do is sum up all fields from key [4] of each subarray and be able to echo the total in $total;
In this example $total; would be 4830 (960+3870).
Also, the array could hold more subarrays then these 2, when a user submits more products to order.
<?php
$array = array
(
'smecid_2' => array
(
0 => 1,
1 => 'SMEC 55.6',
2 => 960,
3 => 864,
4 => 960,
5 => 864,
),
'smecid_6' => array
(
0 => 3,
1 => 'SMEC 55.6 ATEX EX',
2 => 1290,
3 => 1161,
4 => 3870,
5 => 3483,
)
);
$sum = 0;
foreach ($array as $subarray)
{
$sum += $subarray[4];
}
echo $sum;
See it in action