How can I add cummalatively this array below :
array
Product 1 =>
array
0 => float 8065.45
Product 2 =>
array
0 => float 8065.45
array
Product 1 =>
array
0 => float 8065.45
array
0 => float 158.65
Product 2 =>
array
0 => float 8065.45
array
0 => float 11736.37
I am currently using array_sum for this but I think its not working.
use foreach and array_sum to sum it.
$sum = 0;
foreach($products as $product)
{
foreach($product as $v)
{
$sum += array_sum($v);
}
}
By "adding cummulatively", I think you mean appending.
So, giving you example:
<?php
$products = [
'Product 1' => [8065.45],
'Product 2' => [8065.45]
];
var_dump($products);
$products['Product 1'][] = 158.65;
$products['Product 2'][] = 11736.37;
var_dump($products);
It should returns:
array(2) {
["Product 1"] => array(1) {
[0]=> float(8065.45)
}
["Product 2"] => array(1) {
[0]=> float(8065.45)
}
}
array(2) {
["Product 1"] => array(2) {
[0]=> float(8065.45)
[1]=> float(158.65)
}
["Product 2"] => array(2) {
[0]=> float(8065.45)
[1]=> float(11736.37)
}
}
You can also use the array_push() function.
Related
I have an array like this :
array(3) {
["FL_1"] => array(3) {
["MIC_1"] => array(1) {
["SP_4"] => float(7)
}
["MIC_13"] => array(1) {
["SP_16"] => float(4)
}
["MIC_6"] => array(1) {
["SP_74"] => float(4)
}
}
["FL_2"] => array(2) {
["MIC_1"] => array(1) {
["SP_5"] => float(4)
}
["MIC_13"] => array(1) {
["SP_17"] => float(4)
}
["MIC_6"] > array(1) {
["SP_75"] => float(4)
}
}
["FL_3"] => array(2) {
["MIC_1"] => array(1) {
["SP_5"] => float(89)
}
["MIC_13"] => array(1) {
["SP_18"] => float(1)
}
["MIC_6"] > array(1) {
["SP_78"] => float(21)
}
}
}
For each FL_X, I need to keep only one MIC_X that follow the conditions below :
1- This MIC_X needs to be the same for each FL_X
2- This MIC_X needs to have the lowest possible SP_Xvalue
From this example I need to get the following array
array(3) {
["FL_1"] => array(1) {
["MIC_13"] => array(1) {
["SP_16"] => float(4)
}
}
["FL_2"] => array(1) {
["MIC_13"] => array(1) {
["SP_17"] => float(6)
}
}
["FL_3"] => array(1) {
["MIC_13"] => array(1) {
["SP_18"] => float(1)
}
}
}
Any help on how to do this would be much appreciated.
Thank you !
Here's one possible solution. It uses array_walk_recursive to find the SP_X key associated with the minimum SP_X value, then it traverses the array to find the MIC_X key associated with that SP_X key and value, and finally it uses array_map and array_filter to extract only those MIC_X key values from the original array:
// find the minimum SP_X value and its key
$min_sp = PHP_INT_MAX;
$min_key = '';
array_walk_recursive($array, function ($v, $k) use (&$min_sp, &$min_key) {
if ($v < $min_sp) {
$min_sp = $v;
$min_key = $k;
}
});
// find the MIC_X key corresponding to the min SP_X value
$mic_key = '';
foreach ($array as $fl) {
foreach ($fl as $mic => $sp) {
if (isset($sp[$min_key]) && $sp[$min_key] == $min_sp) {
$mic_key = $mic;
break 2;
}
}
}
// filter the array to get all the MIC_X values
$out = array_map(function ($fl) use ($mic_key) {
return array_filter($fl, function ($mic) use ($mic_key) {
return $mic == $mic_key;
}, ARRAY_FILTER_USE_KEY);
}, $array);
print_r($out);
Output:
Array
(
[FL_1] => Array
(
[MIC_13] => Array
(
[SP_16] => 4
)
)
[FL_2] => Array
(
[MIC_13] => Array
(
[SP_17] => 4
)
)
[FL_3] => Array
(
[MIC_13] => Array
(
[SP_18] => 1
)
)
)
Demo on 3v4l.org
I'm fairly new to PHP and I'm having some trouble with arrays and combining data. I have the following array which has been created from a foreach loop:
array(1) {
[36868]=> int(3)
}
array(1) {
[2112]=> int(3)
}
array(1) {
[35901]=> int(3)
}
array(1) {
[6496]=> int(3)
}
array(1) {
[87]=> int(3)
}
array(1) {
[36868]=> int(3)
}
array(1) {
[68]=> int(3)
}
array(1) {
[9068]=> int(3)
}
array(1) {
[47]=> int(3)
}
The key in each array is a user ID, so I need to preserve this, but I only want one instance of each key and where there are duplicate keys, sum the values. Like so:
array(1) {
[36868]=> int(6)
}
array(1) {
[2112]=> int(3)
}
array(1) {
[35901]=> int(3)
}
array(1) {
[6496]=> int(3)
}
array(1) {
[87]=> int(3)
}
array(1) {
[68]=> int(3)
}
array(1) {
[9068]=> int(3)
}
array(1) {
[47]=> int(3)
}
The I've tried looping through the array:
foreach ($array as $key => &$value) {
if ($value[0] == $value[1]) {
$value[1] += $value[1];
}
}
But with no luck. I've also tried rendering the arrays differently i.e. [userid]=>1,[score]=>3 and I feel like I'm going round in circles a bit, so any help would be hugely appreciated.
$data <-- this is your original array
$result = array_reduce(
$data,
function($carry, $item) {
foreach ($item as $id => $score) {
if (array_key_exists($id, $carry)) {
$carry[$id] += $score;
} else {
$carry[$id] = $score;
}
}
return $carry;
},
[]
);
If you are sure that each item only contains 1 entry you could also simplify the callback to not use foreach:
$result = array_reduce(
$data,
function ($carry, $item) {
$score = reset($item);
$id = key($item);
if (array_key_exists($id, $carry)) {
$carry[$id] += $score;
} else {
$carry[$id] = $score;
}
return $carry;
},
[]
);
You could also keep using foreach instead:
/** foreach to create a $data array like described below and afterwards do this: **/
$result = [];
foreach($data as $row) {
$score = reset($row);
$id = key($row);
if (array_key_exists($id, $result)) {
$result[$id] += $score;
} else {
$result[$id] = $score;
}
}
This will take an array $data like this:
array(
array('1' => 3),
array('1' => 3),
array('2' => 3),
);
and creates the variable $result like this:
array(
'1' => 6,
'2' => 3,
);
Here is a clean method that will not produce Notices. When merge-summing array data the efficient method is to generate temporary keys and use the very fast isset() function. I could have used current() and key() to access the lone subarray element, but the second foreach control structure is actually faster and more compact. (Ref:
https://stackoverflow.com/a/21219594/2943403 )
Code: (Demo)
$array = [
[36868 => 3],
[2112 => 3],
[35901 => 3],
[6496 => 3],
[87 => 3],
[36868 => 3],
[68 => 3],
[9068 => 3],
[47 => 3]
];
$result = [];
foreach ($array as $subarray) {
foreach ($subarray as $k => $v) {
if (!isset($result[$k])) {
$result[$k] = $subarray;
} else {
$result[$k][$k] += $v;
}
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
36868 => 6,
),
1 =>
array (
2112 => 3,
),
2 =>
array (
35901 => 3,
),
3 =>
array (
6496 => 3,
),
4 =>
array (
87 => 3,
),
5 =>
array (
68 => 3,
),
6 =>
array (
9068 => 3,
),
7 =>
array (
47 => 3,
),
)
i'm trying to sort an array by the value of a sub-key in DESC order but I'm stuck.
I've could make it with ksort but it was in ascending order..
Here's my array :
array_by_lang => array(
[no] => array(
[3-1] => array(//some informations),
[3-10] => array(//informations),
[3-7] => array(//informations),
[5-1] => array(//informations)
)
)
what i want to obtain is something like :
array_by_lang => array(
[no] => array(
[5-1] => array(//informations),
[3-10] => array(//some informations),
[3-7] => array(//informations),
[3-1] => array(//informations)
)
)
Is that possible ? Thanks a lot
I think, you need "reversing natural sort by key". Just with array_multisort and array_reverse (see also natsort):
$array_by_lang = array(
'no' => array(
'3-1' => array('info_1'),
'3-10' => array('info_2'),
'3-7' => array('info_3'),
'5-1' => array('info_4'),
)
);
array_multisort(array_keys($array_by_lang['no']),SORT_NATURAL, $array_by_lang['no']);
$array_by_lang['no'] = array_reverse($array_by_lang['no']); // reverse natural order - "DESC"
var_dump($array_by_lang);
Output
array(1) {
["no"]=>
array(4) {
["5-1"]=>
array(1) {
[0]=>
string(6) "info_4"
}
["3-10"]=>
array(1) {
[0]=>
string(6) "info_2"
}
["3-7"]=>
array(1) {
[0]=>
string(6) "info_3"
}
["3-1"]=>
array(1) {
[0]=>
string(6) "info_1"
}
}
}
This might help -
$a = array(
'3-1' => array('//some informations'),
'3-10' => array('//informations'),
'3-7' => array('//informations'),
'5-1' => array('//informations')
);
## Array for keys
$temp= array();
foreach(array_keys($a) as $v) {
$t = explode('-', $v);
$temp[$t[0]][] = $t[1];
}
## Sort the keys
foreach($temp as &$ar) {
rsort($ar);
}
krsort($temp);
## Final array
$final= array();
foreach($temp as $k => $f) {
foreach($f as $v) {
$key = $k . '-' . $v;
$final[$key] = $a[$key];
}
}
var_dump($final);
Output
array(4) {
["5-1"]=>
array(1) {
[0]=>
string(14) "//informations"
}
["3-10"]=>
array(1) {
[0]=>
string(14) "//informations"
}
["3-7"]=>
array(1) {
[0]=>
string(14) "//informations"
}
["3-1"]=>
array(1) {
[0]=>
string(19) "//some informations"
}
}
DEMO
I have an array like this:
array(5) {
[0]=> array(1) { ["go-out"]=> string(7) "#0d4b77" }
[1]=> array(1) { ["cycling"]=> string(7) "#1472b7" }
[2]=> array(1) { ["diving"]=> string(7) "#1e73be" }
[3]=> array(1) { ["exploring"]=> string(7) "#062338" }
[4]=> array(1) { ["eating"]=> string(7) "#f79e1b" }
}
Let's say I have the first value like 'cycling', so how can I find the '#147217' value?
I have been trying a lot of combinations of
foreach ( $array as $key => list($key1 ,$val)) {
if ($key1 === $id) {
return $val;
}
}
But no luck.
Ideas?
You can use array_column -
array_column($your_array, 'cycling');
DEMO
You should also add the checks for key's existence.
you may still make one loop
$id = "cycling";
foreach($array as $val)
if(isset($val[$id])) echo $val[$id];
Demo on Evail.in
I have reformated tour code, try this, that works:
$array = array(
0 => array("go-out" => "#0d4b77"),
1 => array("cycling" => "#1472b7"),
2 => array("diving" => "#1e73be"),
3 => array("exploring" => "#062338"),
4 => array("eating" => "#f79e1b")
);
$id = "cycling";
foreach ($array as $key => $entry) {
if ($entry[$id]) {
echo $entry[$id];
}
}
$array = array(
0 => array("go-out" => "#0d4b77"),
1 => array("cycling" => "#1472b7"),
2 => array("diving" => "#1e73be"),
3 => array("exploring" => "#062338"),
4 => array("eating" => "#f79e1b")
);
$search = "cycling";
foreach ($array as $key => $entry)
if (isset($entry[$search]))
echo $entry[$search];
That works.
Nice day.
I am trying to get the count and values of stocklevels from the following
Array ( [barcodes] => Array ( [barcode] => 10011010009 ) [basePrice] => 25.00 [customFields] => Array ( ) [description] => CBC-CBCJSW [discontinued] => false [PLU] => CBC Boy's Shirt 20 [priceLevels] => Array ( ) [primaryCategory] => CBC- Boys Shirts [productLineName] => CBC Boy's Shirt 20 [promptPOSOperatorForPrice] => false [sellByWeightOption] => false [stock] => Array ( [stockLevel] => Array ( [0] => Array ( [outletExportCode] => Level1 [stockCount] => 500.000000 ) [1] => Array ( [outletExportCode] => Level2 [stockCount] => 1000.000000 ) ) ) [taxes] => Array ( [taxName] => GST ) )
I have tried the following and i get 1 as the count all the time even if there are 2 levels in the array:
count($ra['stock']['stockLevel']['stockCount']);
count($ra['stock']['stockLevel']);
Also, i am trying to loop through the results to get teh stockcount but it doesnt seem to work:
$stockLevelArr[] = $ra['stock'];
$totalStock = 0;
for($i=0;$i<count($stockLevelArr);$i++){
echo $ra['productLineName'] . ' - Stock outside IF: ' . (int) $stockLevelArr[$i]['stockLevel']['stockCount'];
echo '<br>';
if (isset($stockLevelArr[$i]['stockLevel']['stockCount'])) {
$totalStock = $totalStock + $stockLevelArr[$i]['stockLevel']['stockCount'];
}
}
echo 'Stock count: ' . $totalStock;
echo '<br>';
var dump
array(13) { ["barcodes"]=> array(1) { ["barcode"]=> string(11) "10011010009" } ["basePrice"]=> string(5) "25.00" ["customFields"]=> array(0) { } ["description"]=> string(10) "CBC-CBCJSW" ["discontinued"]=> string(5) "false" ["PLU"]=> string(18) "CBC Boy's Shirt 20" ["priceLevels"]=> array(0) { } ["primaryCategory"]=> string(16) "CBC- Boys Shirts" ["productLineName"]=> string(18) "CBC Boy's Shirt 20" ["promptPOSOperatorForPrice"]=> string(5) "false" ["sellByWeightOption"]=> string(5) "false" ["stock"]=> array(1) { ["stockLevel"]=> array(2) { [0]=> array(2) { ["outletExportCode"]=> string(13) "Tara Uniforms" ["stockCount"]=> string(10) "500.000000" } [1]=> array(2) { ["outletExportCode"]=> string(3) "CBC" ["stockCount"]=> string(11) "1000.000000" } } } ["taxes"]=> array(1) { ["taxName"]=> string(3) "GST" } }
I will appreciate any help.
UPDATE: Following worked. Thanks everyone for your replies and assistance.
$totalStock = 0;
$times_found = 0;
$stock = $ra['stock'];
foreach($stock['stockLevel'] as $stock_level) {
if(isset($stock_level['stockCount'])) {
$times_found++;
$totalStock = $totalStock + $stock_level['stockCount'];
}
}
I'm assuming you mean you want to know how many times stockCount is within the array? If so, I'd use array to loop through each index of the array and increment a variable each time it's found. For example:
$times_found = 0;
foreach($ra['stock'] as $stock) {
foreach($stock['stockLevel'] as $stock_level) {
if(isset($stock_level['stockCount'])) {
$times_found++;
}
}
}
You should count recursive to get all the keys in the count:
count($ra, COUNT_RECURSIVE);
See also the php manual https://www.php.net/manual/en/function.count.php
And I think you should change your for loop to this:
<?php
$totalStock = 0;
foreach ($ra['stock'] as $stock) {
echo $ra['productLineName'] . ' - Stock outside IF: ' . (int) $stock['stockLevel']['stockCount'];
echo '<br>';
if (isset($stock['stockLevel']['stockCount'])) {
$totalStock = $totalStock + $stock['stockLevel']['stockCount'];
}
}
echo 'Stock count: ' . $totalStock;
echo '<br>';
Instead of the for loop, dont know why you did use that, you better use the foreach in this example.