Adding zero to an array if empty values occurs in array - php

This is the array which I am getting after selecting sum(amounts) for each products group by date.
when the dates did not having correspondent amount it is displaying as empty.I need to display it as zero.
Array
(
[2019-05-16] => Array
(
[0] => 499
)
[2019-05-17] => Array
(
[0] => 1998
)
[2019-05-18] => Array
(
[0] => 195
)
[2019-05-19] => Array
(
[0] => 1194
[1] => 999
)
)
But output should like this,
Is there any way to make empty values as 0
Array
(
[2019-05-16] => Array
(
[0] => 499
[1] => 0
)
[2019-05-17] => Array
(
[0] => 1998
[1] => 0
)
[2019-05-18] => Array
(
[0] => 195
[1] => 0
)
[2019-05-19] => Array
(
[0] => 1194
[1] => 999
)
)
This is the script
foreach ($productid as $itemid) {
$query1 = "select date, SUM(amount) as amount from app_product_details where date between '2019-05-14 00:00:00' and '2019-05-20 23:59:59' and appid = $itemid group by date";
$getquery1 = mysqli_query($conn,$query1);
while($getcount1 = mysqli_fetch_assoc($getquery1))
{
$grset1[] = $getcount1;
}
}
foreach($grset1 as $r){
$dates1[$r['date']][] = $r['amount'];
}
Please help me to reach to the output.

Rather than trying to hack your array, you can change your query so that it returns a 0 value when there were none of a given product sold on that day. This should work:
$query1 = "SELECT date, SUM(CASE WHEN appid = $itemid THEN amount ELSE 0 END) AS amount
FROM app_product_details
WHERE date BETWEEN '2019-05-14 00:00:00' AND '2019-05-20 23:59:59'
GROUP BY date";

Related

PHP not showing all data in array from loop

Outputting data to an array
public function GetProfitByMonth($d1, $d2){
$sql=odbc_exec($this->connections, "select con.ID, con.ShortName as NameAK,
ROUND(ISNULL(SUM(tik.TotalSub)-SUM(tik.TotalAG), 0), 2) as Profit,
MONTH(tik.DEALDATE) as month_num
from Counteragent as con
left join Tickets as tik on tik.AgentID=con.ID
where con.ValueType in (2,3) and con.Active='1' and (DEALDATE between '".$d1."' and '".$d2."')
group by con.ID, con.ShortName, MONTH(tik.DEALDATE)
order by con.ShortName, MONTH(tik.DEALDATE) ;");
$tblResult=array();
while ($row = odbc_fetch_array($sql)) {
$tblResult[]=$row;
}
odbc_free_result($sql);
Array
(
[0] => Array
(
[Profit] => 11218.30
[month_num] => 8
)
[1] => Array
(
[Profit] => 1152.15
[month_num] => 8
)
....
[4] => Array
(
[Profit] => 119837.81
[month_num] => 8
)
)
I need to display only month_num => Profi from the array
did this way
$tblResult = array();
while ($row = odbc_fetch_array($sql)) {
$tblResult[$row['mon_num']] = $row['Profit'];
}
odbc_free_result($sql);
return $tblResult;
but as a result in the array shows only the last value:
Array
(
[8] => 119837.81
)
How to make an array show all the data?
This is because month_num = 8 for all entries in your original array.
Your while loops first two iterations effectively evaluate to:
$tblResult[8] = 11218.30
$tblResult[8] = 1152.15
To get the result in the form you want, you need month_num to be unique.

Find value from multidimensional array from given two values

I have the following array.
Array
(
[0] => Array
(
[product] => p1
[item] => q1
[date] => d1
[status] => N
)
[1] => Array
(
[product] => p2
[item] => q2
[date] => d2
[status] => Y
)
[2] => Array
(
[product] => p1
[item] => q3
[date] => d3
[status] => N
)
)
From this array, I want date from above with the given product and value.
For Example, I have product = p2 and item = q2 as a string. Now from this array, I want a date from this array which has product = p2 and item = q2.
So I need output from the above example is: d2
I tried array_column and array_search but can't find value from product and item both.
Please help me out with this in PHP.
You can iterate over your products and check required criteria with if statement.
foreach ($products as $product) {
if ('p2' === $product['product'] && 'q2' === $product['item']) {
var_dump($product['date']);
}
}
The conditions are as a string of type
$strCond = "product = p2 and item = q2";
given?
This is converted into an array which can be processed better:
parse_str(str_replace(' and ','&',$strCond),$cond);
/*
$cond = array (
'product_' => " p2",
'item_' => " q2",
);
*/
array_filter() with a closure which cleverly uses array_intersect_assoc() delivers the result.
$result = array_filter($input,function($row) use($cond){
return array_intersect_assoc($cond, $row) === $cond;
});
Result:
array (
1 =>
array (
'product' => "p2",
'item' => "q2",
'date' => "d2",
'status' => "Y",
),
)
As a rule, with such multidimensional arrays there are always several data records in the result. You can get the date of the first record like this:
$firstDate = reset($result)['date']; //d2
Note: The algorithm was taken from PHP class tableArray.
One way of solving with this class:
$result = tableArray::create($input)
->filterEqual($cond)
->fetchRow()
;
/*
$result = array (
'product' => "p2",
'item' => "q2",
'date' => "d2",
'status' => "Y",
)
*/

How to group multidimensional array by more keys

this is my array source.
//array for all reception
$reception = array(
array('code'=>'AB202','order'=>'111111','qty'=>'50.000'),
array('code'=>'AB202','order'=>'111111','qty'=>'50.000'),
array('code'=>'AB202','order'=>'222222','qty'=>'50.000'),
array('code'=>'AB300','order'=>'666666','qty'=>'100.000'),
array('code'=>'AB300','order'=>'666666','qty'=>'120.000'),
array('code'=>'AB300','order'=>'777777','qty'=>'120.000')
);
the code AB202 equals order 111111 AND 222222
the order 111111 equals qty=>50, qty=>50
the order 222222 equals qty=>50
the code AB300 equals order 666666 AND 777777
the order 666666 equals qty=>100, qty=>120
the order 777777 equals qty=>120
I want group by code and order like this
Array
(
[AB202] => Array //array code
(
[111111] => Array //array order
(
[0] => 50.000 // qty
[1] => 50.000
)
[222222] => Array
(
[0] => 50.000
)
)
[AB300] => Array
(
[666666] => Array
(
[0] => 100.000
[1] => 120.000
)
[777777] => Array
(
[0] => 120.000
)
)
)
loop thru all receptions, then check if code key or order key exists, make one if not, then append each qty
$newArr = array();
foreach ($reception as $code) {
if (!array_key_exists($code['code'], $newArr)){
$newArr[$code['code']] = array();
}
if (!array_key_exists($code['order'], $newArr[$code['code']])) {
$newArr[$code['code']][$code['order']] = array();
}
array_push($newArr[$code['code']][$code['order']], $code['qty']);
}

PHP - Array does not turn into two-dimensional array

I need to make my array better.
I am getting data from database and i have milestones and milestone_parts. i want two-dimensional array. I need data of milestones in the first dimension and milestone_parts in the second dimension.
With this code:
$query = "
SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`,
b.omschrijving AS `milestonefase_omschrijving`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '99'
";
$result= $db->query($dbh, $query);
while ($row = $db->fetchassoc($result))
{
$stone = array($row['milestone_verkocht_id'], $row['milestone_id'], $row['milestone_titel'], $row['client']);
$fase = array($row['milestonefase_milestone_id'],$row['milestonefase_id'],$row['milestonefase_titel']);
$stone[] = $fase;
echo '<pre>'; print_r($stone); echo '</pre>';
}
I get this as result
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 10
[2] => string
)
)
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 11
[2] => string
)
)
but I need (with names) this:
Array
(
[milestone_verkocht_id] => 99 // This is project id
[milestone_id] => 6
[milestone_title] => string
[client] => string
[10] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 10
[milestone_title] => string
)
[11] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 11
[milestone_title] => string
)
[12] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 12
[milestone_title] => string
)
)
Can you help me or do you have a solution? Help me please!
you can cycle each field returned by the query, checking the field name and making new arrays
$stones = array();
while ($row = $db->fetchassoc($result)) {
$fase = array();
$stone = array('milestones' => array());
foreach ($row as $k => $v) {
if (strpos($k, 'milestonefase_') === 0) {
$fase[$k] = $v;
} else {
$stone[$k] = $v;
}
}
if(!isset($stones[$stone['milestone_id']])) {
$stones[$stone['milestone_id']] = $stone;
}
$stones[$stone['milestone_id']]['milestones'][$fase['milestonefase_id']] = $fase;
}
echo '<pre>'.print_r($stones, true).'</pre>';
Edit
made some changes in order to match the request. Now we use $stones to store the information we already have on a milestone, adding to it the different "$fase" returned from the query
Probably a more clean way is to retrieve all the information with two different queries one for milestones and the other for the fases
Edit2
Added a sub-array for the milestone fases

Loops into a multidimensional array with PHP

I have this array in PHP:
[data] => Array
(
[BOO_item_quantity] => Array
(
[0] => 1
[1] => 6
)
[BOO_item_id] => Array
(
[0] => 18
[1] => 13
)
[BOO_item_price] => Array
(
[0] => 3
[1] => 0
)
)
How is it possible to loop into this to get something like this please ?
Quantity = 1 - Item_Id = 18 - Price = 3
Quantity = 6 - Item_Id = 13 - Price = 0
Thanks.
You can use a foreach statement to loop over the elements the first sub-array:
$data = array(
'BOO_item_quantity' => array(1,6),
'BOO_item_id' => array(18,13),
'BOO_item_price' =>array(3,0)
);
foreach($data['BOO_item_quantity'] as $index=>$value) {
print 'Quantity = '.$value.' - Item_Id = '.$data['BOO_item_id'][$index].' - Price = '.$data['BOO_item_price'][$index].'<br />';
}
See it in action at phpfiddle.

Categories