I have this array structure (see below) grouped by date from a database query, and I wish to find the total for each group by multiplying the quantity by the transamount.
This is what i have so far:
array (size=3)
'2017-7-03' =>
array (size=3)
0 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'batch1' (length=31)
'quantity' => string '1.0000' (length=6)
'transamount' => string '20.0000' (length=7)
1 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'Eye Test' (length=8)
'quantity' => string '1.0000' (length=6)
'transamount' => string '5.0000' (length=6)
2 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'hub photos' (length=16)
'quantity' => string '1.0000' (length=6)
'transamount' => string '82.0000' (length=7)
'2017-7-04' =>
array (size=9)
0 =>
array (size=4)
'paydate' => string '2017-7-04' (length=9)
'tariff' => string ' Register' (length=21)
'quantity' => string '1.0000' (length=6)
'transamount' => string '20.0000' (length=7)
The aim is to get to total of all transaction grouped by the dates.
This may not work since I'm not sure if you need to access the transactions using the date as a key... But something like this would add an element to each array item with the total.
// loop through the arrays sorted by date
foreach ($groups as $group)
{
$total = 0;
// loop through each transaction
foreach ($group as $transaction)
{
// Add to the total for each purchase group
$total += $transaction->quantity * $transaction->transamount;
}
// put the total into the group array
$group->total = $total
}
With this you should end up with something like
'total' => (calculated value),
'2017-7-03' =>
array (size=3)
0 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'batch1' (length=31)
'quantity' => string '1.0000' (length=6)
'transamount' => string '20.0000' (length=7)
1 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'Eye Test' (length=8)
'quantity' => string '1.0000' (length=6)
'transamount' => string '5.0000' (length=6)
2 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'hub photos' (length=16)
'quantity' => string '1.0000' (length=6)
'transamount' => string '82.0000' (length=7)
'2017-7-04' =>
Related
I have 2 product id and my code is:
$Final=array();
foreach ($ids as $me)
{
$op=DB::table('product')->where(['id'=>$me])->get();
//$Final[]= $op;
array_push($Final,$op);
}
This code returns:
array (size=1)
0 =>
array (size=1)
0 =>
array (size=15)
'id' => string '34' (length=2)
'title' => string 'گوسفند' (length=12)
'title_url' => string 'sheep' (length=5)
'code' => string 'eerer' (length=5)
'code_url' => string 'eerer' (length=5)
'content' => string '<p>sheep</p>
' (length=14)
'cat' => string '68' (length=2)
'price' => string '50000' (length=5)
'product_state' => string '1' (length=1)
'date' => string '' (length=0)
'order_number' => string '0' (length=1)
'Special' => string '0' (length=1)
'View' => string '0' (length=1)
'number_product' => string '1' (length=1)
'discounts' => string '' (length=0)
I need to remove
array (size=2) 0 => array (size=1) 0 =>
$ids => filter id
for get product number for example (22,34)
I Think you should try this.
$Final=array();
foreach ($ids as $me){
$op=DB::table('product')->where(['id'=>$me])->get();
if($op) {
array_push($Final,$op[0]);
}
}
Then you will get these values.
array (size=2)
0 =>
array (size=15)
'id' => string '34' (length=2)
1 =>
array (size=15)
'id' => string '22' (length=2)
If you are using Any framework then framwork provide us some methods to run query with where in to get all the records in single query.
$op=DB::table('product')->whereIn('id'=>$ids)->get();
you will get array of collection for all the products.
I'm trying to unset one record from array but I can't manage it, so I'm asking for someone to help me.
Logic I'm trying to achieve is:
User has an item with possible three categories, he posts a form and than I iterate over array and try to see if category3 matches, if not I go to category2, if not I go to category1. If I find one of the categories that matches array and category asked in form I proceed , but If I don't find any of the categories I unset that record in array.Below you will see an example what I've done now.
I can't get the proper result. Either I unset all the records or unset the record that should stay. I'm trying different conditions for last three days without success. Record I'm talking about is with a key 5.
This is my array:
array (size=5)
0 =>
array (size=9)
'cost_no' => string 'D01' (length=3)
'cost_name' => string 'Carina' (length=6)
'cost_type' => string 'Customs' (length=7)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '5.00' (length=4)
'cost_id' => int 7
'group1' => string '' (length=3)
'group2' => string '' (length=3)
'group3' => string '' (length=3)
1 =>
array (size=9)
'cost_no' => string 'D02' (length=3)
'cost_name' => string 'Banka' (length=5)
'cost_type' => string 'Bank' (length=4)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.15' (length=4)
'cost_id' => int 8
'group1' => string '' (length=3)
'group2' => string '' (length=3)
'group3' => string '' (length=3)
2 =>
array (size=9)
'cost_no' => string 'D03' (length=3)
'cost_name' => string 'Ĺ pediter' (length=9)
'cost_type' => string 'Transport' (length=9)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.50' (length=4)
'cost_id' => int 9
'group1' => string '' (length=3)
'group2' => string '' (length=3)
'group3' => string '' (length=3)
3 =>
array (size=9)
'cost_no' => string 'D04' (length=3)
'cost_name' => string 'Troškovi firme' (length=15)
'cost_type' => string 'Tax' (length=3)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '21.00' (length=5)
'cost_id' => int 10
'group1' => string '' (length=3)
'group2' => string '' (length=3)
'group3' => string '' (length=3)
5 =>
array (size=9)
'cost_no' => string 'C1' (length=2)
'cost_name' => string 'Cost only for this group' (length=24)
'cost_type' => string 'Warehouse' (length=9)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '12.00' (length=5)
'cost_id' => int 12
'group1' => string 'ACC_P' (length=5)
'group2' => string '1401' (length=4)
'group3' => string '' (length=3)
This is foreach and condition I tried to use to unset if non of the categories match ( it has to check from category3 to category 1).
foreach ($cost_array as $key1 => $value1) {
if (isset($form_group3) AND isset($value1['group3']) AND $value1['group3'] != $form_group3) {
if (isset($form_group2) AND isset($value1['group2']) AND $value1['group2'] != $form_group2) {
if (isset($form_group1) AND isset($value1['group1']) AND $value1['group1'] != $form_group1) {
} else {
unset($cost_array[$key1]);
}
} else {
unset($cost_array[$key1]);
}
} else {
unset($cost_array[$key1]);
}
}
Groups user posts from form:
$form_group1 = mysqli_real_escape_string($conn_mysqli, $_POST['group1']);
$form_group2 = mysqli_real_escape_string($conn_mysqli, $_POST['group2']);
$form_group3 = mysqli_real_escape_string($conn_mysqli, $_POST['group3']);
How to my array combine :
$visitor_tracvking_data = json_encode($array);
print_r($visitor_tracvking_data);
Output print_r this:
array (size=6)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
1 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'social_media' => string '3' (length=1)
2 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'search' => string '3' (length=1)
3 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
4 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'firefox' => string '68' (length=2)
5 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'netscape' => string '31' (length=2)
How to out put this type:-
array (size=2)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
'social_media' => string '3' (length=1)
'search' => string '3' (length=1)
1 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
'firefox' => string '68' (length=2)
'netscape' => string '31' (length=2)
Just loop through the array and generate a new array with tracktitle key -
$new = array();
foreach($array as $key_top => $value) {
foreach($value as $key => $val) {
$new[$value['tracktitle']][$key] = $val;
}
}
return $new;
OR for json encoded string -
return json_encode($new);
I have array like this
array (size=6)
0 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '231' (length=3)
'document_title' => string 'Renstra 2014-2018 Distamben COVER' (length=33)
'document_date' => string '2014-10-15 14:09:00' (length=19)
'site' => string 'Propinsi Nusa Tenggara Barat' (length=28)
'url_download' => string 'http://ntbprov.sip-ppid.net/index.php/document/download/231' (length=59)
1 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '7' (length=1)
'document_title' => string 'Data Menara 2014' (length=16)
'document_date' => string '2015-01-08 13:05:00' (length=19)
'site' => string 'Kabupaten Bima' (length=14)
'url_download' => string 'http://bimakab.sip-ppid.net/index.php/document/download/7' (length=57)
how to sort by document_date? I try with usort() but not working.
I am Newbie.
I am trying to merge 2 arrays: 1 multidimensional and another one normal:
Multidimensional array - $_SESSION["products"]
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '1' (length=1)
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '1' (length=1)
'price' => string '2300.50' (length=7)
Simple array - $qty
array (size=2)
0 => string '2' (length=1)
1 => string '3' (length=1)
EXPECTED OUTPUT
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '2' (length=1) // notice the qty change
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '3' (length=1) // notice the qty change
'price' => string '2300.50' (length=7)
I tried:
foreach ($_SESSION["products"] as $cart_itm){
foreach($qty as $qt) {
$cart_itm['qty'] = $qt;
}
}
But did not work, cart_itm['qty'] remained the same (1).
Try with this:
foreach ($_SESSION["products"] as $key => &$cart_itm){
$cart_itm['qty'] = $qty[$key];
}