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']);
}
Related
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.
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";
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.
I'm busy with sorting the structure of a menu in my application. Once the menu is reorderd by the user, the values (say; Menu item 1, Menu item 2, etc) are still in the same place.
Now I have two arrays, one that holds the way they are sorted (Array 1) and one that holds the values of the menu items. (Array 2)
Example of both arrays;
(Array 1, that holds the keys)
Array
(
[0] => 1
[1] => 2
[2] => 0
)
The above array's values are the keys for the new array.
(Array 2, holds the values)
Array
(
[0] => value_0
[1] => value_1
[2] => value_2
)
So I thought it would be best to create a new array which consist out of;
The values of Array 1
The values of Array 2
However, i'm running into a problem. I want the values in array 2 to stick to their keys. So lets say I change the position of value_0 to the last, the new array would look like this;
Array
(
[1] => value_1
[2] => value_2
[0] => value_0
)
Is there a way to achieve this or am I doing it completely wrong?
Edit
Ok, so multidemensional array it is. However i'm having problems creating one.
Array 1 and Array 2 both come from the database. Array 1 with the sorting order and Array 2 contains the values. Now, the values in array 2 are stored like this; value1,value2,value3. So to be able to work with them I explode on , (comma).
The results on the fetchs are both different;
For the first array it returns as many as how many values there are.
(So if there are 3 values, it will return 3 different positions.)
For the second array it will return 18 records, since this is tied to
other menu items (sub menu's etc).
So for the first array I do;
while ($row = mysql_fetch_assoc($result_query_test)) {
$positions[] = $row['position'];
}
For the second array I do;
while ($row = mysql_fetch_assoc($result_values)) {
$array_values = explode(',', $row['values']);
}
From then on i'm having problems creating the multidimensinonal array;
while ($row = mysql_fetch_assoc($result_values)) {
$array_values = explode(',', $row['values']);
foreach ($positions as $new_key) {
foreach ($array_values as $value) {
$new_array[] = array('key' => $new_key, 'value' => $value);
}
}
}
Edit two:
This is what I use now;
(Since $all_values is a multidimensional array because I have to explode on the values beforehand.)
foreach ($all_values as $values) {
foreach ($values as $key => $value) {
$new_array[] = array('key' => $positions[$key], 'value' => $value);
}
}
This is what the $new_array returns;
Array
(
[0] => Array
(
[key] => 0
[value] => value_0
)
[1] => Array
(
[key] => 2
[value] => value_2
)
[2] => Array
(
[key] => 1
[value] => value_1
)
[3] => Array
(
[key] => 0
[value] => value_0
)
[4] => Array
(
[key] => 2
[value] => value_2
)
[5] => Array
(
[key] => 1
[value] => value_1
)
Now I need to get the values and implode them with comma's. However, since not every 3 values (value_0, value_1, value_3) are together I can't do that now.
In this example there are 3 keys, (0,1,2) which should be a different array along with their values, like you did in your example:
Array (
[0] = Array (
[key] = 1,
[value] = value_1
),
[1] = Array (
[key] = 2,
[value] = value_2
),
[2] = Array (
[key] = 0,
[value] = value_0
)
)
Why not make a multidimensional array?
$arrayThree =
Array (
[0] = Array (
[key] = 1,
[value] = value_1
),
[1] = Array (
[key] = 2,
[value] = value_2
),
[2] = Array (
[key] = 0,
[value] = value_0
)
)
No matter what order they're in, the key and value are always the set.
foreach ($arrayThree as $tempArray)
{
echo $tempArray['key'];
echo $tempArray['value'];
}
Create Array
$arrayOne = array();
$arrayTwo = array();
$arrayThree = array();
$query = 'SELECT key FROM table1 ';
$result = mysql_query($query) or die(mysql_error());
while($data = mysql_fetch_assoc($result))
{
$arrayOne[] = $data['key'];
}
$query = 'SELECT value FROM table2 ';
$result = mysql_query($query) or die(mysql_error());
while($data = mysql_fetch_assoc($result))
{
$arrayTwo[] = $data['value'];
}
foreach($arrayOne as $key => $value)
{
$arrayThree[] = array('key' => $value, 'value' => $arrayTwo[$key]);
}
You can always use the mysqli or PDO versions, if you're using them.
Example Data
//THESE MIMIC YOUR SELECT RESULTS
$test_keys = array(1,2,3);
$test_values = array('value_1', 'value_2', 'value_3');
//DEFAULTS
$arrayOne = array();
$arrayTwo = array();
$arrayThree = array();
//WHILE FIRST SELECT
for($i=0;$i<count($test_keys);$i++)
{
$arrayOne[] = $test_keys[$i];
}
//WHILE SECOND SELECT
for($i=0;$i<count($test_values);$i++)
{
$arrayTwo[] = $test_values[$i];
}
//MAKE THE FINAL ARRAY
foreach($arrayOne as $key => $value)
{
$arrayThree[] = array('key' => $value, 'value' => $arrayTwo[$key]);
}
//CHECK THE OUTPUT FOR THE NEW ARRAY
echo '<pre>'.print_r($arrayThree,true).'</pre>';
Example Output
Array
(
[0] => Array
(
[key] => 1
[value] => value_1
)
[1] => Array
(
[key] => 2
[value] => value_2
)
[2] => Array
(
[key] => 3
[value] => value_3
)
)
Imploded List
$implodeValues = array_map(function($item) { return $item['value']; }, $arrayThree);
$implodeVariable = implode(',', $implodeValues);
echo $implodeVariable;
Implode Output
value_1,value_2,value_3
I think you can obtain what you want doing this:
$new = array();
for($i = 0, $len = count($array1), $i < $len, ++$i) {
$new[$array1[$i]] = $array2[$i];
}
now $new contains the values in the order you want them
I have an array that's output is this:
Array ( [winners] =>
Array ( [0] => Gold Member
[1] => CROTCH SNIFFER
[2] => TEAM #1 )
[prizeTotal] => 20 )
Array ( [winners] =>
Array ( [0] => TEAM #1
[1] => CROTCH SNIFFER )
[prizeTotal] => 60 )
Array ( [winners] =>
Array ( [0] => Gold Member
[1] => TEAM #1 )
[prizeTotal] => 30 )
Array ( [winners] =>
Array ( [0] => TEAM #1
[1] => TEAM #2
[2] => SCREW-NUT-BOLT )
[prizeTotal] => 90 )
Please forgive the names...it's not my DB.
I can not change the way the array is show here.
With that being said how can I group and sum?
1. For each winners array there is a prizeTotal below the team names. That prize total should be the value of each teamName above it.
Example
Array ( [winners] =>
Array ( [0] => Gold Member
[1] => CROTCH SNIFFER
[2] => TEAM #1 )
[prizeTotal] => 20 )
Gold Member should have 20
CROTCH SNIFFER should have 20
TEAM #1 should have 20 AND
Array ( [winners] =>
Array ( [0] => TEAM #1
[1] => CROTCH SNIFFER )
[prizeTotal] => 60 )
TEAM #1 should have 60
CROTCH SNIFFER sould have 60....etc....
Then I want to group by team name and sum so that I can display...
CROTCH SNIFFER = 80
Gold Member = 50
TEAM #1 = 200
Team #2 = 90.
Thanks in advance...
Assuming you can collect all your arrays into one i.e. :
$arrays = array($array1,$array2,....,$arrayn);
then
$grouping = array();
foreach($arrays AS $array)
{
foreach($array['winners'] AS $k=>$v)
{
//check if the array key is a number, will contain a team, and if that
//them is not alreay listed
if(is_numeric($k) && !array_key_exists($v,$grouping))
$grouping[$v] = 0;
//sum the prize to the team's sum
$grouping[$v] += intval($array['winners']['prizeTotal']);
}
}
//the following is just for debugging
print_r($grouping);
this should produce something like:
$grouping[TEAM #1] = 200
$grouping[TEAM #2] = 90
$grouping[CROTCH SNIFFER] = 200
...
You need to make a new array to hold your results, then for each of your existing arrays, check if you're already processed one for that team
If you have, then simply add the prizeTotal to the prizeTotal stored in your new array's entry for that team.
If not, simply add the team's entry to your new array.
I have made a function that imitates mysql SUM() and GROUP BY. I hope it will fit your needs:
$in_a = array(
array("a" => 0,"b"=>0,"s"=> 1),
array("a" => 0,"b"=>0,"s"=> 2),
array("a" => 1,"b"=>1,"s"=> 1),
array("a" => 0,"b"=>1,"s"=> 1),
array("a" => 0,"b"=>1,"s"=> 1),
array("a" => 1,"b"=>0,"s"=> 1),
array("a" => 0,"b"=>1,"s"=> 1),
array("a" => 1,"b"=>1,"s"=> 1),
array("a" => 1,"b"=>0,"s"=> 1),
);//input array exaple
$group_by_a = array("a","b");//input array will be grouped by these
$sum_a = array("s"); //'s' values of input will be summed
$out_a = array(); //this is the output array
foreach($in_a as $in_i => $in)
{
$add = false;
foreach($out_a as $out_i => $out)
{
$add = true;
foreach($group_by_a as $group_by)
if($in[$group_by] != $out[$group_by])
{
$add = false;
break;
}
if($add)
{
foreach($sum_a as $sum)
$out_a[$out_i][$sum] += $in[$sum];
break;
}
}
if(!$add)
{
foreach($group_by_a as $group_by)
$out_a[$in_i][$group_by] = $in[$group_by];
foreach($sum_a as $sum)
$out_a[$in_i][$sum] = $in[$sum];
}
}
the result:
Array
(
[0] => Array
(
[a] => 0
[b] => 0
[s] => 3
)
[2] => Array
(
[a] => 1
[b] => 1
[s] => 2
)
[3] => Array
(
[a] => 0
[b] => 1
[s] => 3
)
[5] => Array
(
[a] => 1
[b] => 0
[s] => 2
)
)