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");
Related
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);
Here is my array
Array
(
[0] => Array
(
[Asan_Name_Val] => 447
[Actual_Ratio] => 15/00/15,04/05/05
)
[1] => Array
(
[Asan_Name_Val] => 447
[Actual_Ratio] => 10/05/11,00/06/05
)
)
The actual ratio value should sum with it values like 15+10=15,00+05=05,15+11=26 .....
So The desired output I want In this format
Array
(
[Asan_Name_Val] => 447
[Actual_Ratio] => 25/05/26,04/11/10
)
Wrote a hacky solution in PHP.
Will work but strongly advice you to optimize.
<?php
$myArray = array(
0 => array(
'Asan_Name_Val' => '447',
'Actual_Ratio' => '15/00/15,04/05/05'
),
1 => array(
'Asan_Name_Val' => '447',
'Actual_Ratio' => '10/05/11,00/06/05'
)
);
$sums = array_fill(0,6,'0');
foreach($myArray as $arr){
$ratio = $arr['Actual_Ratio'];
$j=0;
for($i=0;$i<6;$i++){
$sums[$i] = sprintf("%02d", $sums[$i]+substr($ratio,$j,2));
$j = $j+3;
}
}
$finalRatio = "$sums[0]/$sums[1]/$sums[2],$sums[3]/$sums[4]/$sums[5]";
$desiredArray['Asan_Name_Val'] = '447';
$desiredArray['Actual_Ratio'] = $finalRatio;
print_r($desiredArray);
This would be a flexible and clean approach:
<?php
$input = [
[
'Asan_Name_Val' => "447",
'Actual_Ratio' => "15/00/15,04/05/05"
],
[
'Asan_Name_Val' => "447",
'Actual_Ratio' => "10/05/11,00/06/05"
]
];
$output = [];
$formatter = new NumberFormatter('de_DE', NumberFormatter::DECIMAL);
array_walk($input, function($entry) use (&$output, $formatter) {
$values = explode("/", $entry['Actual_Ratio']);
foreach($values as &$value) {
$value = $formatter->parse($value);
}
$output[$entry['Asan_Name_Val']][] = $values;
});
array_walk($output, function(&$entry, $key) use ($formatter) {
for ($i = 0; $i < count($entry[0]); $i++) {
$sums[] = $formatter->format(array_sum(array_column($entry, $i)));
}
$entry = [
'Asan_Name_Val' => $key,
'Actual_Ratio' => implode("/", $sums)
];
});
print_r(array_values($output));
The output obviously is:
Array
(
[0] => Array
(
[Asan_Name_Val] => 447
[Actual_Ratio] => 25/5/26,04/11/10
)
)
please help me with a code. I have this multi-dimensional array and need to count the value of usuario_cidade case its same value like this:
array 52 = (2) Cidade_1, (2) Cidade_2, (1) Cidade_3
Array
(
[52] => Array
(
[0] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_1
)
[1] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_1
)
[2] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_2
)
[3] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_3
)
[4] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_2
)
)
)
Try this code:
//Create object array format as per question scenario for testing...
$arrObject1 = new stdClass();
$arrObject1->funcionario_id = '52';
$arrObject1->usuario_cidade = 'Cidade_1';
$arrObject2 = new stdClass();
$arrObject2->funcionario_id = '52';
$arrObject2->usuario_cidade = 'Cidade_1';
$arrObject3 = new stdClass();
$arrObject3->funcionario_id = '52';
$arrObject3->usuario_cidade = 'Cidade_2';
$arrObject4 = new stdClass();
$arrObject4->funcionario_id = '52';
$arrObject4->usuario_cidade = 'Cidade_3';
$arrObject5 = new stdClass();
$arrObject5->funcionario_id = '52';
$arrObject5->usuario_cidade = 'Cidade_2';
//Finalize array...
$varArray = array('52' => array(
$arrObject1, $arrObject2, $arrObject3, $arrObject4, $arrObject5
));
$arrResult = array();
//Loop until main array...
foreach($varArray AS $arrKey => $arrObjVal){
//Loop for object values...
foreach($arrObjVal AS $ocjKey => $objVal){
//Check for specific key(i.e. value of usuario_cidade) exist into result array...
if(array_key_exists($objVal->usuario_cidade, $arrResult)){
//Increment value if exist...
$arrResult[$objVal->usuario_cidade] = $arrResult[$objVal->usuario_cidade] + 1;
}
else {
//Initialize value of result array...
$arrResult[$objVal->usuario_cidade] = 1;
}
}
}
print('<pre>');
print_r($arrResult);
print('</pre>');
This will give result:
[Cidade_1] => 2
[Cidade_2] => 2
[Cidade_3] => 1
Hope this help you!
Try this..
$your_array = array();
$usuario_cidade = array();
foreach ($your_array as $key => $values){
foreach($values as $value){
$usuario_cidade[$key][$value->usuario_cidade]=isset($usuario_cidade[$key][$value->usuario_cidade]) ? $usuario_cidade[$key][$value->usuario_cidade] : '0' + 1;
}
}
print_r($usuario_cidade);
Hey kindly check this code it is given the same answer as the conditions are given.
$arr = array();
$arr2 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_1' );
$arr3 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_1' );
$arr4 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_2' );
$arr5 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_3' );
$arr6 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_2' );
$arr = [$arr2,$arr3,$arr4,$arr5,$arr6];
$cidaed = array('Cidade_1' => 0, 'Cidade_2' => 0 , 'Cidade_3' => 0 );
$count = 0;
//echo var_dump($arr);
foreach ($arr as $key => $value) {
foreach ($value as $keys => $values) {
if($keys == 'usuario_cidade')
{
$cidaed[$values] += 1;
}
}
}
echo var_dump($cidaed);
The answer for above will be.
array(3) { ["Cidade_1"]=> int(2) ["Cidade_2"]=> int(2) ["Cidade_3"]=> int(1) }
Can you please check this.
$main_array[52] = array(
0 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_1'
),
1 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_1'
),
2 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_2'
),
3 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_3'
),
4 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_2'
)
);
$check_array = array();
$count_array = array();
foreach ($main_array as $main){
foreach($main as $data){
if(in_array($data['usuario_cidade'], $check_array)){
$count_array[$data['usuario_cidade']] = $count_array[$data['usuario_cidade']] + 1;
}else{
array_push($check_array,$data['usuario_cidade']);
$count_array[$data['usuario_cidade']] = 1;
}
}
}
foreach($count_array as $key => $value){
echo $key.'='.$value.'<br />';
}
echo "<pre>"; print_r($count_array);
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 have an array which contains following values.
array(
'dates' => array(
(int) 0 => '2013-04-22',
(int) 1 => '2013-04-23',
),
'publisherName' => array(
(int) 0 => 'Comp1',
(int) 1 => 'Comp2',
),
'loaded' => array(
(int) 0 => (int) 2189,
(int) 1 => (int) 37,
),
'clicks' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
),
'ctr' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
)
)
What I want to produce is getting company based data on different dates like the array below.
How am I able to create an array which is like;
array (
'2013-04-22'=>array(
'publisherName'=>'Comp1',
'loaded'=>2189,
'clicks'=>0,
'ctr'=>0),
'2013-04-23'=>array(
'publisherName'=>'Comp2',
'loaded'=>37,
'clicks'=>0,
'ctr'=>0)
...
)
Which contains daily stats but which comes from publishername field.
Any ideas?
Here's an example that doesn't rely on any keys, the only requirement is that date is the first array in your original array:
// $array is your original array
$dates = array_shift($array);
$output = array();
foreach ($array as $k => $a) {
foreach ($a as $i => $v) {
$output[$i][$k] = $v;
}
}
$output = array_combine($dates, $output);
Let the initial array be $a and the desired array $b;
Code:
$b = array();
$count = 0;
foreach($a['dates'] as $date) {
$b[$date] = array(
'name' => $a['publisherName'][$count],
'loaded'=> $a['loaded'][$count],
'clicks'=> $a['clicks'][$count],
'ctr'=> $a['ctr'][$count]
);
$count++;
}
Result:
Array
(
[2013-04-22] => Array
(
[name] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[name] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)
<?php
$data = $yourarray;
$new = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($new);
?>
$newArr = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($newArr);
$new_arr = your array;
$finalArray = array();
foreach($new_arr['dates'] as $key => $value){
$finalArray[$value] = array(
'publisherName'=>$new_arr['publisherName'][$key],
'loaded'=>$new_arr['loaded'][$key],
'clicks'=>$new_arr['clicks'][$key],
'ctr'=>$new_arr['ctr'][$key]
);
}
Output :
Array
(
[2013-04-22] => Array
(
[publisherName] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[publisherName] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)