Summing the values of two arrays in php - php

There is a problem:
After submitting a form I receive the indexed array as a SQL-query result and the goal is to sum each next array values with the values of previously received arrays and put the sums into result array
Example:
// 1st query result
$product_amount = [0 => 0.36,
1 => 0.14,
2 => 0.42]
// 2nd query result
$product_amount = [0 => 0.28,
1 => 0.12,
2 => 0.40]
// n-th query result
$product_amount = [0 => 0.16,
1 => 0.14,
2 => 0.42]
// the sum
$total = [0 => 0.80,
1 => 0.40,
2 => 1.24]
How to do it?
The code that creates an array:
function doseCount($num){
return ($num/100) * intval($_POST['portion']); //getting multiplier for productAmount() function
}
function productAmount(){
global $link; //connection to the MySQL server
if(isset($_POST['product'])){
$product_amount_query = 'SELECT va.vitamin_a, va.vitamin_b1, va.vitamin_b2, va.vitamin_b3, va.vitamin_b5,
va.vitamin_b6, va.vitamin_bc_b9, va.vitamin_b12, va.vitamin_c, va.vitamin_d, va.vitamin_e, va.biotin, va.vitamin_k,
va.flavonoids, va.lipoic_acid FROM products_list p, vitamins_amount va WHERE p.id = va.product_id AND p.name = ?';
$product_amount_stmt = mysqli_prepare($link, $product_amount_query);
mysqli_stmt_bind_param($product_amount_stmt, "s", $_POST['product']);
mysqli_stmt_execute($product_amount_stmt);
$result = mysqli_stmt_get_result($product_amount_stmt);
$product = mysqli_fetch_array($result, MYSQLI_NUM);
return $product_final_result = array_map('doseCount', $product);
mysqli_stmt_close($product_amount_stmt);
mysqli_close($link);
}
}
$product_amount = productAmount();
After submit $product_amount array gets new data and this new value must be summed with previous.

You could accomplish this by combining array_map and array_sum. If you're using PHP 5.6 or greater, which you should, you could write something like this:
$total = array_map(function (...$vals) {
return array_sum($vals);
}, ...$results);
Assuming that $results contains all of your $product_amount arrays.
If you want to build $total incrementally instead of passing all query results at once, you can do this:
// Initialize $total array
$total = [0, 0, 0];
// Update $total for each query
$total = array_map(function (...$vals) {
return array_sum($vals);
}, $total, $product_amount);

This problem can be solved with proper use of sessions:
if (empty($_SESSION['product_amount'])){
$_SESSION['product_amount'] = $product_amount;
}else{
foreach ($_SESSION['product_amount'] as $key => $value) {
$_SESSION['product_amount'][$key] += $product_amount[$key];
}
}
Special thanks to Mawia HL for his help.

Related

How generate column using for loop

How to make this function produce column occLvl_ loop 3 time and using explode to fetch each value from sql CONCAT. So the result will become like this.
[{
"accommodationID": "LA56",
"occLvl_0": "40.00",
"occLvl_1": "70.00",
"occLvl_2": "90.00"
}]
function getOccLevel(){
global $ehorsObj;
$occArray = array();
$sql = "SELECT accommodationID, GROUP_CONCAT(occLevelDesc) AS occLevels
FROM tblSamAccOccLevels
WHERE ACTIVE = 'y'
GROUP BY accommodationID
ORDER BY accommodationID ASC, occLevelDesc ASC ";
$GetResult = $ehorsObj->FetchData($sql, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
while ($row = $GetResult->fetch()){
$occArray[] = array(
'accommodationID' => $row['accommodationID'],
);
//seem the method below is not working
for ($j = 0; $j < 3; $j++) {
$occArray["occLvl_".$j] = explode(",", $row['occLevels'])
}
}
header("Content-type: application/json");
$result = json_encode($occArray);
echo $result;
}
Result of the query
accommodationID occLevels
LA56 40.00, 70.00, 90.00
Making numerically named object properties/variables (occLvl_0 etc.) is generally a bad idea as it makes it difficult to work with them in any regular manner (e.g. using a loop). It is better practice to put the values into an array:
while ($row = $GetResult->fetch()){
$occArray[] = array(
'accommodationID' => $row['accommodationID'],
'occLvl' => explode(",", $row['occLevels'])
);
}
This will give you an output JSON that looks something like:
[
{
"accommodationID": "LA56",
"occLvl": [
40,
70,
90
]
},
{
"accommodationID": "PQ45",
"occLvl": [
30,
60,
100
]
},
...
]
And in your JS you can then iterate over the occLvl array to get the values.
If you need the data in the form you describe, then you need to iterate over the exploded occLevels value to generate the individual values, pushing them with the accommodationID into a new array and then pushing that array to $occArray:
while ($row = $GetResult->fetch()){
$this_occ = array(
'accommodationID' => $row['accommodationID'],
);
foreach (explode(",", $row['occLevels']) as $key => $occLvl) {
$this_occ["occLvl_$key"] = $occLvl;
}
$occArray[] = $this_occ;
}
Explode returns an array. You should explode before the loop and iterate through the result of that explode in the loop

PHP - Sum array by defined condition of value

Please I need help to summarize a values of array by some conditions
i have this result query :
I want to calcul the Sum of calls for each user, i try with like this, but its show me the sum of all query [Users]
$array_out_ = array();
$usercalls = $usertalk = $userpause = $userwait = $userdispo = $userdead = 0;
foreach ($queryusers2 as $user) {
$current_user = $user['user'];
$usercalls += $user['calls'];
$usertalk += $user['talk'];
$userpause += $user['pause'];
$userwait += $user['wait'];
$userdispo += $user['dispo'];
$userdead += $user['dead'];
$array_out_[$current_user]['calls'] = $usercalls;
$array_out_[$current_user]['talk'] = $usertalk;
$array_out_[$current_user]['pause'] = $userpause;
$array_out_[$current_user]['wait'] = $userwait;
$array_out_[$current_user]['dispo'] = $userdispo;
$array_out_[$current_user]['dead'] = $userdead;
}
This method return me $usercalls (305 for agent29 ) and (436 for agent117)
But i want 305 ofr agent 29 and only 131 for the agent117
Thanks
I recommend you change the query SQL to something like this, where you can group and sum your data.
SELECT
tn.user,
SUM(tn.calls) AS usercalls,
SUM(tn.talk) AS usertalk,
SUM(tn.pause) AS userpause,
SUM(tn.wait) AS userwait,
SUM(tn.dispo) AS userdispo,
SUM(tn.dead) AS userdead
FROM tablename tn
GROUP BY tn.user
And this will return what you need:
agent29, 305, ...
agent117, 436, ...
Your values $usercalls,$usertalk,... are not re-initialized. and are cumulated for all users. Your code works only for the first user and only because your list is ordered by users.
You could initialize an array and sum directly inside it:
$array_out_ = array();
foreach ($queryusers2 as $user)
{
$current_user = $user['user'];
if (!isset($array_out_[$current_user])) {
$array_out_[$current_user] = [
'calls' => 0, 'talk' => 0, 'pause' => 0, 'wait' => 0,
'dispo' => 0, 'dead' => 0
];
}
$array_out_[$current_user]['calls'] += $user['calls'];
$array_out_[$current_user]['talk'] += $user['talk'];
$array_out_[$current_user]['pause'] += $user['pause'];
$array_out_[$current_user]['wait'] += $user['wait'];
$array_out_[$current_user]['dispo'] += $user['dispo'];
$array_out_[$current_user]['dead'] += $user['dead'];
}
See also the answer of Enric Borrallo Rodriguez

Getting a an array from for loop and use another for loop with that array in PHP

I have a for loop, and will form two arrays in the loo
foreach ($data as $key => $value) {
........
........
$user_insert[] = [
'keyy' => $value,
'key' => $value,
....
...
...
];
$someArray1[] = [
/*'user_id' => $insert_id,*/
'key1' => $value1,
'keyy' => $value,
.......
........
];
}
the count of $user_insert[] array is 4, the count of $someArray1 is 15.
after this for loop, I need to insert $user_insert array data to the database and use that inserted_id to insert next array $someArray1
foreach($user_insert as $insert_user){
$unique_user_insert = array_unique($insert_user);
//dd($unique_user_insert);
$insert_id = DB::table('users')->insertGetId($unique_user_insert);
foreach ($someArray1 as $someArray) {
$someArray['user_id'] = $insert_id;
DB::table('table_name')->insert($someArray);
}
}
So the problem here is the data in the second loop is inserting 60 times(4 * 15). I need to insert only 15 rows.
The data($someArray1) is coming from the first for loop, but I need to add a user_id to that array which I get after the insert operation in second for loop.
So how can i insert only 15 rows.
I'm going to assume that you are able to access your $someArray1 using the $insert_id value to find the appropriate user data.
foreach($user_insert as $insert_user){
$unique_user_insert = array_unique($insert_user);
$insert_id = DB::table('users')->insertGetId($unique_user_insert);
// Get the user information you need as $someArray1 should be user_id=>data
$userData = $someArray[$insert_id];
$userData['user_id'] = $insert_id;
// No need for an inner loop, just access the necessary properties of the loop you created earlier.
DB::table('table_name')->insert($userData);
}
Your tags indicate that you are using Laravel 5 too. If you are using the eloquent ORM, some of the insertion and ID retrieval can be cleaned up by creating Models for your DB tables.
Actually, each time you process a line from $user_insert, you loop over $someArray1 instead of fetching just the line you need.
The thing is to understand the line you need. As much as I can understand your piece of code, I would say the easiest (most readable) way of doing it is by using a for loop, not a foreach one :
for( $i = 0, $iMax = count( $user_insert ); $i < $iMax; ++$i ){
$insert_user = $user_insert[$i];
// Put your `$user_insert` insert code here
$someArray1[$i]['user_id'] = $insert_id;
DB::table('table_name')->insert( $someArray[$i] ); // Note the [$i] here
}
You also may do that with foreach by requesting indices :
foreach( $user_insert as $i => $insert_user ){
$unique_user_insert = array_unique($insert_user);
//dd($unique_user_insert);
$insert_id = DB::table('users')->insertGetId($unique_user_insert);
// Now use $i requested above :
$someArray1[$i]['user_id'] = $insert_id;
DB::table( 'table_name' )->insert( $someArray1[$i] );
}

PHP Array select result from other array

I have a sql table with some category, i get them in a array.. all fine but when i try to get data from another table foreach category, always return me for first category selected.
This is my code:
$gameguidecategoryes = array();
$gameguides = array();
$dbselectgameguidecategoryes = new DB_MSSQL;
$dbselectgameguidecategoryes->Database=$newweb_db;
$dbselectgameguidecategoryes->query("Select GameGuideCatNr,GameGuideCatName_$languageid as GameGuideCatName,GameGuideCatImage from GameGuide_Category where GameGuideCatVisible = 1 order by GameGuideCatOrder asc");
for($i=0;$i < $dbselectgameguidecategoryes->num_rows();++$i) {
if ($dbselectgameguidecategoryes->next_record()){
$GameGuideCatNr = $dbselectgameguidecategoryes->f('GameGuideCatNr');
$GameGuideCatName = $dbselectgameguidecategoryes->f('GameGuideCatName');
$GameGuideCatImage = $dbselectgameguidecategoryes->f('GameGuideCatImage');
}
$gameguidecategoryes_temp = array(
'ggcname' => $GameGuideCatName,
'ggcimg' => $GameGuideCatImage,
);
$gameguidecategoryes[$i] = $gameguidecategoryes_temp;
$dbselectgameguide = new DB_MSSQL;
$dbselectgameguide->Database=$newweb_db;
$dbselectgameguide->query("Select GameGuideID,GameGuideName_$languageid as GameGuideName from GameGuide_Content where GameGuideCat = $GameGuideCatNr and GameGuideVisible = 1 order by GameGuideOrder asc");
for($ii=0;$ii < $dbselectgameguide->num_rows();++$ii) {
if ($dbselectgameguide->next_record()){
$GameGuideID = $dbselectgameguide->f('GameGuideID');
$GameGuideName = $dbselectgameguide->f('GameGuideName');
}
$gameguides_temp = array(
'ggid' => $GameGuideID,
'ggn' => $GameGuideName,
);
$gameguides[$ii] = $gameguides_temp;
}
}
Why $gameguides return data only from first category?
Thank you
Your second loop keeps getting trashed by the first loop. e.g. Consider what happens:
You fetch your categories, and (let's pretend) there's 4 of them.
You store some information in $gameguidecategoryes[0]
You run the second query, get some content for category #0, say, 3 records
That gets stored in $gameguides[0], [1], [2]
Your outer loop ticks again, and you start on categoryes[1]
The inner loop ticks again, you get 4 records, and now you're storing them into the SAME again: $gameguides[0], [1], [2], [3], etc...
You've now trashed the data you fetched in the first loop, and will
do so for every category you fetch.
This code is very inefficient. You should learn how to use JOINs, and fetch into a single structure, e.g.
SELECT category.id, category.name, ...., content.id, content.name
FROM categories
LEFT JOIN content ON categories.id = content.category_id
ORDER BY ...
and then something like
$data = array();
while($row = fetch row from db) {
if (!isset($data[$row['category.id']]) {
$data[$row['category.id']] = array(
'name' => $row['category.name'],
'content' => array()
);
}
$data[$row['category.id']]['content'][] = array(
... save content data here
);
};
Better work on clean code
$gameguidecategoryes = $gameguides = $gameguidescategoryids = array();
$dbselectgameguidecategoryes = new DB_MSSQL;
$dbselectgameguidecategoryes->Database=$newweb_db;
$dbselectgameguidecategoryes->query("Select GameGuideCatNr,GameGuideCatName_$languageid as GameGuideCatName,GameGuideCatImage from GameGuide_Category where GameGuideCatVisible = 1 order by GameGuideCatOrder asc");
while ($dbselectgameguidecategoryes->next_record()) {
$gameguidescategoryids[] = $dbselectgameguidecategoryes->f('GameGuideCatNr');
$gameguidecategoryes[] = array(
'ggcname' => $dbselectgameguidecategoryes->f('GameGuideCatName'),
'ggcimg' => $dbselectgameguidecategoryes->f('GameGuideCatImage'),
);
}
if (count($gameguidescategoryids)) {
$dbselectgameguide = new DB_MSSQL;
$dbselectgameguide->Database=$newweb_db;
$dbselectgameguide->query("Select GameGuideID,GameGuideName_$languageid as GameGuideName from GameGuide_Content where GameGuideCat IN (".implode(',', $gameguidescategoryids).") and GameGuideVisible = 1 order by GameGuideOrder asc");
while ($dbselectgameguide->next_record()){
$gameguides[] = array(
'ggid' => $dbselectgameguide->f('GameGuideID'),
'ggn' => $dbselectgameguide->f('GameGuideName'),
);
}
}

using array_count_values in 2 dimensional array

So I have array with patch number and seasons next to it, and I'm trying to count how many patches are in a season so I want to count how many $season[x][1] == 1 or 2 etc.
$patches_get = $conn->prepare("SELECT Patch_No FROM info ORDER BY Created DESC");
$patches_get->execute();
$patchesresult = $patches_get->get_result();
while($data1 = $patchesresult->fetch_assoc()){
$patches[]=$data1["Patch_No"];
}
function getseasons($patches){
$seasons = array();
foreach($patches as $patch){
if(substr($patch,0,1)!=1){
$seasons[] = array($patch, substr($patch,0,1));
}
//Checking first number if 1 it is season 1 or 2 or 3
elseif(substr($patch,0,1)==1){
if(substr($patch, 6,3)>151&&substr($patch, 6,3)<155){
$seasons[] = array($patch, 3);
}
elseif(substr($patch, 6,3)>125&&substr($patch, 6,3)<151){
$seasons[] = array($patch, 2);
}
elseif(substr($patch, 6,3)>32&&substr($patch, 6,3)<126){
$seasons[] = array($patch, 1);
}
}
}
return $seasons;
}
$seasons = getseasons($patches);
var_dump($seasons);
$fr_c=array_count_values($seasons);
echo $fr_c['1'];
Here is also a var_dump of how my array would look like http://i.imgur.com/lV1APvV.png
If I"m reading your question right, you essentially want to count the inner array without iterating through the outer? If that's the case, look into array_column()
For example, let's say I have the following array
$myArr = [
['item' => 'value1'],
['item' => 'value2']
]
Calling array column like so
array_column($myArr, 'item'); // returns an array of just the values that you can iterate through.
See here
If this isn't the answer you're looking for, please disregard.

Categories