Here I am trying to fetch the income of a seller, but the problem is that I am unable to group the duplicate the array value into one.
Logic Function.
function getMonthlyIncomeOfSellerById($sellerId)
{
$data = array();
$ddd = array();
$query = "SELECT date_format(created_at,'%M'), product_id,sell_discount,SUM(sell_quantity) FROM sellers_sells WHERE seller_id=? AND YEAR(created_at) = YEAR(CURRENT_DATE()) GROUP BY date_format(created_at,'%M'),seller_id,product_id ORDER BY created_at";
$stmt = $this->con->prepare($query);
$stmt->bind_param('s',$sellerId);
$stmt->execute();
$stmt->bind_result($monthName,$productId,$sellDiscount,$sellQuantity);
while ($stmt->fetch())
{
$d['monthName'] = $monthName;
$d['productId'] = $productId;
$d['sellDiscount'] = $sellDiscount;
$d['sellQuantity'] = $sellQuantity;
array_push($data, $d);
}
$stmt->close();
$netProfit = 0;
$maxProfit = 0;
foreach ($data as $dt)
{
$product = $this->getProductById($dt['productId']);
$product['productPrice'] = ($product['productPrice']/100)*$dt['sellDiscount'];
if ($product['productName']==='PRODUCT NAME' && $product['productBrand']==='PRODUCT BRAND' && $product['productSize']==='50ML')
{
$maxPrice = $product['productPrice']*$dt['sellQuantity'];
$price = ($product['productPrice']-10)*$dt['sellQuantity'];
}
else if ($product['productName']==='PRODUCT NAME' && $product['productBrand']==='PRODUCT BRAND' && $product['productSize']==='100ML')
{
$maxPrice = $product['productPrice']*$dt['sellQuantity'];
$price = ($product['productPrice']-15)*$dt['sellQuantity'];
}
else if ($product['productName']==='PRODUCT NAME' && $product['productBrand']==='PRODUCT BRAND' && $product['productSize']==='200ML')
{
$maxPrice = $product['productPrice']*$dt['sellQuantity'];
$price = ($product['productPrice']-30)*$dt['sellQuantity'];
}
$netProfit = $netProfit+$price;
$maxProfit = $maxProfit+$maxPrice;
$dts['monthName'] = $dt['monthName'];
$dts['netProfit'] = $netProfit;
$dts['maxProfit'] = $maxProfit;
array_push($ddd, $dts);
}
print_r($ddd);
}
Getting the out put with duplicate value.
Array
(
[0] => Array
(
[monthName] => January
[netProfit] => 2050
[maxProfit] => 2800
)
[1] => Array
(
[monthName] => March
[netProfit] => 2214
[maxProfit] => 3024
)
[2] => Array
(
[monthName] => March
[netProfit] => 4149
[maxProfit] => 5604
)
[3] => Array
(
[monthName] => March
[netProfit] => 4523
[maxProfit] => 6148
)
)
But here I want to group it with the month, and add all the values into one.
Like this.
Array
(
[0] => Array
(
[monthName] => January
[netProfit] => 2050
[maxProfit] => 2800
)
[1] => Array
(
[monthName] => March
[netProfit] => 10886
[maxProfit] => 14776
)
)
How can I do this?
Thanks
You could group by 'month' by usign the monthName as key. If the key doesn't exists, create a new entry with default values. Then, you could add (+) the current value from the data.
$monthName = $dt['monthName'];
if (!isset($ddd[$monthName])) {
$ddd[$monthName] = [
'monthName' => $monthName,
'netProfit' => 0,
'maxProfit' => 0,
];
}
$ddd[$monthName]['netProfit'] += $netProfit;
$ddd[$monthName]['maxProfit'] += $maxProfit;
Instead of
$dts['monthName'] = $dt['monthName'];
$dts['netProfit'] = $netProfit;
$dts['maxProfit'] = $maxProfit;
array_push($ddd, $dts);
To remove added keys, you could use :
$ddd = array_values($ddd);
Related
Below is the array inside loop:
I want to filter this array by [date-begin] and [date-end]
For eg if I post startdate = 2015-06-29 and enddate = 2015-08-29
then array data between this range should come.
I tried:
1. array_slice
2.foreach(range ($startdate,$enddate) as $data){
echo "Age: {$data}<br />";
}
Array
(
[name] => MCLE 201
[date-begin] => 2015-06-29
[date-end] => 2015-06-29
)
Array
(
[name] => MCLE 201
[date-begin] => 2015-07-29
[date-end] => 2015-07-29
)
Array
(
[name] => MCLE 201
[date-begin] => 2015-08-29
[date-end] => 2015-08-29
)
Array
(
[name] => MCLE 201
[date-begin] => 2015-09-29
[date-end] => 2015-09-29
)
#Aashi you can do it with foreach() like below:
<?php
$yourArray = array(
array(
"name" => "MCLE 201",
"date-begin" => "2015-06-29",
"date-end" => "2015-06-29"
),
array(
"name" => "MCLE 201",
"date-begin" => "2015-07-29",
"date-end" => "2015-07-29"
),
array(
"name" => "MCLE 201",
"date-begin" => "2015-08-29",
"date-end" => "2015-08-29"
),
array(
"name" => "MCLE 201",
"date-begin" => "2015-09-29",
"date-end" => "2015-09-29"
)
);
$startdate = "2015-06-29";
$enddate = "2015-08-29";
$filteredArr = array();
foreach($yourArray as $value) {
if($startdate <= $value["date-begin"] && $enddate >= $value["date-end"]){
$filteredArr[] = $value;
}
}
echo "<pre>";
print_r($filteredArr);
Try this:
$filterArray = array();
foreach($arr as $key=>$val){
if(strtotime($val['date-begin']) >= strtotime($postedDateBegin) && strtotime($val['date-end']) <= strtotime($postedDateEnd)){
$filterArray[] = $val;
}
}
Click here to check output
this is proper solution for you question !
function date_is_between($start_date, $end_date, $date){
$start_date = date('Y-m-d',strtotime($start_date));
$end_date = date('Y-m-d',strtotime($end_date));
$date = date('Y-m-d',strtotime($date));
$match = FALSE;
if (($date => $start_date) && ($date <= $end_date))
$match = TRUE;
return $match;
}
function date_between($element, $start_date, $end_date)
{
$match = FALSE
if(date_is_between($element['date-begin'], $element['date-end'], $element['date-begin']) && date_is_between($element['date-begin'], $element['date-end'], $element['date-end']))
$match = TRUE;
return $match;
}
$filter_array = array_filter($data, "date_between");
i have been troubling to format array correctly
i have this code:
require('simple_html_dom.php');
$table = array();
$html = file_get_html('apc.html');
foreach($html->find('tr') as $row) {
$rack = ltrim($row->find('td',0)->plaintext);
$location = ltrim($row->find('td',1)->plaintext);
$usage = ltrim($row->find('td',2)->plaintext);
$auk = ltrim($row->find('td',3)->plaintext);
$cost = ltrim($row->find('td',4)->plaintext);
$rack = rtrim($rack);
$location = rtrim($location);
$usage = rtrim($usage);
$auk = rtrim($auk);
$cost = rtrim($cost);
$table[$rack][$usage][$auk][$cost] = true;
}
echo '<pre>';
print_r($table);
echo '</pre>';
using simple_html_dom above i can convert html table to an array as follow:
[Rack01] => Array
(
[741,60] => Array
(
[1.409,04] => Array
(
[267,72] => 1
)
)
[110,88] => Array
(
[210,67] => Array
(
[40,03] => 1
)
)
)
[Rack 09] => Array
(
[843,84] => Array
(
[1.603,30] => Array
(
[304,63] => 1
)
)
)
I would like to have result as below:
array(
[0] => array (
[usage] => 'Rack 01',
[usage] => '741,60',
[auk] => '1.409.04',
[cost] => '267,72')
[1] => array (
[usage] => 'Rack 02',
[usage] => 'value???',
[auk] => 'value???',
[cost] => 'value???')
any help would be apreaciate
Something like this. Also note that trim will do both left and right:
foreach($html->find('tr') as $row) {
$rack = trim($row->find('td',0)->plaintext);
$location = trim($row->find('td',1)->plaintext);
$usage = trim($row->find('td',2)->plaintext);
$auk = trim($row->find('td',3)->plaintext);
$cost = trim($row->find('td',4)->plaintext);
$table[] = array('rack' => $rack,
'usage' => $usage,
'auk' => $auk,
'cost' => $cost);
}
I have an array in php like this
Array (
[0] => Array (
[date] => 21/07/2014
[total_booking] => 1
)
[1] => Array (
[date] => 1/08/2014
[total_booking] => 1
)
[2] => Array (
[date] => 2/09/2014
[total_booking] => 2
)
)
if i get value $_POST['from_date'] and $_POST['to_date'] all array other than between this from_date,to_date should be removed.
Edit my code
foreach ($newarray as $newarray){
if ($newarray[Date] < $to_date && $from_date > $newarray[Date])
{
// i want to remove this row from array
}
}
Here's a solution using array_filter:
<?php
$data = array(
array(
'date' => '21/07/2014',
'total_booking' => '1'
),
array(
'date' => '1/08/2014',
'total_booking' => '1'
),
array(
'date' => '2/09/2014',
'total_booking' => '2'
)
);
$from_date = strtotime('01-08-2014'); //$_POST['from_date']
$to_date = strtotime('21-08-2014'); //$_POST['to_date']
$data = array_filter($data, function($array) use ($from_date, $to_date) {
$epoch = strtotime(str_replace('/', '-', $array['date']));
return $epoch >= $from_date && $epoch <= $to_date;
});
$data = array_values($data);
print_r($data);
Output:
Array
(
[0] => Array
(
[date] => 1/08/2014
[total_booking] => 1
)
)
Only one element is outputted because only 1/08/2014 is between 1/08/2014 and 21/08/2014.
For earlier PHP versions:
function filter($array) {
$from_date = strtotime('01-08-2014'); //$_POST['from_date']
$to_date = strtotime('21-08-2014'); //$_POST['to_date']
$epoch = strtotime(str_replace('/', '-', $array['date']));
return $epoch >= $from_date && $epoch <= $to_date;
}
$data = array_filter($data, 'filter');
I use a function called count_days($date1,$date2) that counts the number of days between two dates. But, my question is: the dates come from the DB, in an array like:
Array (
[imac] => Array (
[0] => 2002-10-10
[1] => 2003-11-22
[3] => 2004-11-10
)
[iphone] => Array (
[0] => 2007-09-11
[1] => 2008-05-12
[2] => 2009-06-14
[3] => 2010-06-05
)
)
As you can see the products may have a different number of subarrays. I want to count the days between the first and the next date (and so on!) and then get the average of days.
The DateInterval class is great for this kind of date arithmetic. You can use DateTime::add, DateTime::subtract and DateTime::diff to work with them.
<?php
$types = array(
'imac' => array ('2002-10-10', '2003-11-22', '2004-11-10'),
'iphone' => array ( '2007-09-11', '2008-05-12', '2009-06-14', '2010-06-05'),
);
$typeIntervals = array();
$typeAverage = array();
foreach ($types as $type=>$dates) {
$last = null;
$typeIntervals[$type] = array();
foreach ($dates as $date) {
$current = new DateTime($date);
if ($last) {
$interval = $current->diff($last);
$typeIntervals[$type][] = $interval->days;
}
$last = $current;
}
$typeAverage[$type] = array_sum($typeIntervals[$type]) / count($typeIntervals[$type]);
}
print_r(typeIntervals);
print_r($typeAverage);
This will output:
Array (
[imac] => Array (
[0] => 408
[1] => 354
)
[iphone] => Array (
[0] => 244
[1] => 398
[2] => 356
)
)
Array (
[imac] => 381
[iphone] => 332.66666666667
)
try smth like this ( not tested )
$lastDate = $dbArray[0][0];
$daysArray = array();
foreach ( $dbArray as $value )
{
if ( is_array($value) )
{
foreach ( $value as $v )
{
$daysArray[] = count_days($lastDate, $v);
$lastDate = $v;
}
} else {
//not an array check if it's a date and test it here
}
}
$average = array_sum($daysArray)/count($daysArray);
How to count the item-qty and current code :-
$q = $_POST['item-qty'];
$i = count($q);
$k = 0;
while ($k < $i) {
$select = 'SELECT * FROM location';
$query = $db->rq($select);
$price = $db->fetch($query);
if ($_POST['item-qty'][$k] < 3) {
$get = $price['normal_price'];
$price = $get * $_POST['item-qty'][$k];
$_SESSION['order'][$_POST['item-id'][$k]] = array(
"item-id" => $_POST['item-id'][$k],
"item-qty" => $_POST['item-qty'][$k],
"item-name" => $_POST['item-name'][$k],
"item-price" => $price,
);
} else {
$get = $price['member_price'];
$price = $get * $_POST['item-qty'][$k];
$_SESSION['order'][$_POST['item-id'][$k]] = array(
"item-id" => $_POST['item-id'][$k],
"item-qty" => $_POST['item-qty'][$k],
"item-name" => $_POST['item-name'][$k],
"item-price" => $price,
);
}
}
here the array output
Array
(
[order] => Array
(
[1] => Array
(
[item-id] => 1
[item-qty] => 1
[item-name] => Adidas
[item-price] => 100
)
[2] => Array
(
[item-id] => 2
[item-qty] => 1
[item-name] => Nike
[item-price] => 150
)
)
)
Question :
How to implement other code if item-qty (in all array) is greater than or equal to 3 will use $price['member_price']
let me know :)
I'm guessing you meant the total item-qty of everything?
$qty_sum = 0
foreach($_SESSION['order'] as $order){
$qty_sum += $order['item-qty'];
}