The following code gives the array below, however I need it formatted differently (stated after the array), so it will work for a javascript function I've already written.
$sql = "SELECT towhich, duedate, amount FROM sales WHERE email = '$email' ORDER BY duedate ASC";
$result = mysqli_query($conn, $sql);
$dbarray = array();
while($row = mysqli_fetch_assoc($result)) {
$dbarray[] = $row;
}
$graph = array();
$cnt = 0;
foreach($dbarray as $key => $values){
$orderdate = explode('-', $values['duedate']);
$month = $orderdate[1];
$graph[$month][$cnt] = array (
0 => $values['amount'],
1 => $values['towhich']
);
$cnt ++;
}
//Now it's grouped by date
Array output:
array(5)
{
["02"]=> array(2)
{
[0]=> array(2) { [0]=> string(2) "10" [1]=> string(9) "the co op" }
[1]=> array(2) { [0]=> string(2) "30" [1]=> string(9) "the co op" }
}
["03"]=> array(1)
{
[2]=> array(2) { [0]=> string(2) "50" [1]=> string(9) "the co op" }
}
["04"]=> array(1)
{
[3]=> array(2) {[0]=> string(2) "40" [1]=> string(9) "the co op" }
}
["05"]=> array(2)
{
[4]=> array(2) {[0]=> string(2) "10" [1]=> string(9) "the co op" }
[5]=> array(2) { [0]=> string(2) "10" [1]=> string(9) "the co op" }
}
["06"]=> array(1)
{
[6]=> array(2) { [0]=> string(2) "10" [1]=> string(9) "the co op" }
}
}
The key index value for array should not be, for example, ['02'], but, being the first in the containing array, [0], like normal; and '03' should be [1].
I've looked around a lot, indeed it helped with what bit of code I've produced, however all answers seem to deal with changing the key value further inside the large array. I'm new with multidimensional arrays, btw. Thanks in advance.
In case you're wondering why I've done it like so, so far, it's because each first array should correspond to a different month; that's why I've ordered by date and all that.
If I got you right, you can use: array_values :
$graph = array_values($graph);
so "02" will be 0, "03" will be 1 , ... etc.
$key in your foreach will be the relative record number that the row occurs in the query.
$graph = array();
$cnt = 0;
foreach($dbarray as $key => $values){
$orderdate = explode('-', $values['duedate']);
$month = $orderdate[1];
$graph[$month][$cnt] = array (
$graph[$key] = array (
$month,
$values['amount'],
$values['towhich']
);
$cnt ++;
}
Related
I have this array :
array(8) {
[0]=>
array(2) {
["ticket_type"]=>
string(9) "normal"
["ticket_price"]=>
string(5) "82.00"
}
[1]=>
array(2) {
["ticket_type"]=>
string(9) "cheaper"
["ticket_price"]=>
string(5) "62.00"
}
[2]=>
array(2) {
["ticket_type"]=>
string(9) "normal"
["ticket_price"]=>
string(6) "182.00"
}
[3]=>
array(2) {
["ticket_type"]=>
string(9) "cheaper"
["ticket_price"]=>
string(6) "162.00"
}
[4]=>
array(2) {
["ticket_type"]=>
string(9) "normal"
["ticket_price"]=>
string(6) "103.00"
}
[5]=>
array(2) {
["ticket_type"]=>
string(9) "cheaper"
["ticket_price"]=>
string(5) "63.00"
}
[6]=>
array(2) {
["ticket_type"]=>
string(9) "normal"
["ticket_price"]=>
string(6) "203.00"
}
[7]=>
array(2) {
["ticket_type"]=>
string(9) "cheaper"
["ticket_price"]=>
string(6) "163.00"
}
}
I want to get the min and max price for "normal" and "cheap" ticket category, there will be more ticket categories, so can't hard code it, will get from DB, how can I do that? I'm using PHP 5.6 now, need export as array or json, Let me know if need more details.
Thank you
You can do it at the SQL level:
SELECT MIN(ticket_price) AS min_ticket_price, MAX(ticket_price) as max_ticket_price, ticket_type
FROM {your_table_name}
WHERE {your_conditions}
GROUP BY ticket_type
If you still want to do it in PHP you can just loop the array and to sort the ticket types then get the min and max values.
foreach($arr as $ticket){
$new[$ticket["ticket_type"]][] = $ticket["ticket_price"];
}
// loop the new array to only get the max and min values of each ticket type
foreach($new as $key => $n){
$res[$key]["max"] = max($n);
$res[$key]["min"] = min($n);
}
output:
array(2) {
["normal"]=>
array(2) {
["max"]=>
string(6) "203.00"
["min"]=>
string(5) "82.00"
}
["cheaper"]=>
array(2) {
["max"]=>
string(6) "163.00"
["min"]=>
string(5) "62.00"
}
}
https://3v4l.org/RcCHZ
Here is a solution to do on the php level:
$prices = [
["ticket_type"=>"normal", "ticket_price"=>"82.00"],
["ticket_type"=>"cheaper", "ticket_price"=>"62.00"],
["ticket_type"=>"normal", "ticket_price"=>"182.00"],
["ticket_type"=>"cheaper", "ticket_price"=>"162.00"],
["ticket_type"=>"normal", "ticket_price"=>"103.00"],
["ticket_type"=>"cheaper", "ticket_price"=>"63.00"],
["ticket_type"=>"normal", "ticket_price"=>"203.00"],
["ticket_type"=>"cheaper", "ticket_price"=>"163.00"],
];
// STEP:1 group by ticket types
$grouped_array = [];
foreach($prices as $price) {
$ticket_type = $price["ticket_type"];
$grouped_array[$ticket_type][] = $price["ticket_price"];
}
/*
$grouped_array = [
"normal" => ["82.00", "182.00", "103.00", "203.00"],
"cheaper => ["62.00", "162.00", "63.00", "163.00"]
];
*/
function minMax($type_array) {
$min_max = ['min'=> min($type_array), 'max'=>max($type_array)];
return $min_max;
}
// STEP 2: find min and max in each category
$min_max = [];
foreach($grouped_array as $type => $prices) {
$min_max[$type] = minMax($prices);
}
/*
$min_max = [
"normal" => ["min"=>"82.00", "max"=>"203.00"],
"cheaper => ["min"=>"62.00", "max"=>"163.00"]
];
*/
However, doing it on the database layer is best in your case. this answers "How to get max and min value in a multidimensional array".
I am really struggling filtering my data.I know how to access parts of the array but am struggling on how to filter. I know how to do it in SQL SUM & count) but not when it comes to PHP arrays please help.
I need to filter some of my data stored in the variable $res.
I need to see how many times each 'REF' appears & total 'LCL_D_CBM' for each ref all of this where the "TERR" is equal to "TERR1".
foreach ($res as $res1){
if ($res1["TERR"] == "TERR1"){
//the individual ref
//$res1['REF'];
// the coutn of how many times each ref appears
//echo count($res1['REF']);
//sum
//echo array_sum($res1['LCL_D_CBM']);
}
}
need result to be like -
CEA
3
2561
CEF
456
135
var_dump($res); gives me below
array(350) {
[0]=> array(7) { [0]=> string(3) "CEA" ["REF"]=> string(3) "CEA" [1]=> string(5) "1.080" ["LCL_D_CBM"]=> string(5) "1.080" [2]=> string(3) "WF2" ["AREA_CODE"]=> string(3) "WF2" ["TERR"]=> string(5) "TERR1" }
[1]=> array(7) { [0]=> string(3) "CEA" ["REF"]=> string(3) "CEA" [1]=> string(5) "2.000" ["LCL_D_CBM"]=> string(5) "2.000" [2]=> string(4) "HU13" ["AREA_CODE"]=> string(4) "HU13" ["TERR"]=> string(5) "TERR1" }
[2]=> array(7) { [0]=> string(3) "CEF" ["REF"]=> string(3) "CEA" [1]=> string(5) "2.448" ["LCL_D_CBM"]=> string(5) "2.448" [2]=> string(4) "TW16" ["AREA_CODE"]=> string(4) "TW16" ["TERR"]=> string(5) "TERR2" } ..... etc
You can solve it like this. Not a pretty solution, but should do the work. (I did not test this, so it might contain a spelling error or something):
$sum_lcl = 0;
$unique_refs = array();
foreach ($res as $res1){
if (!array_key_exists($res1['REF'], $unique_refs)) {
$unique_refs[$res1['REF']] = array();
}
$unique_refs[$res1['REF']]['SUM'] = ( array_key_exists('SUM', $unique_refs[$res1['REF']]) ) ? ( intval( $unique_refs[$res1['REF']]['SUM'] ) + 1 ) : 1;
$unique_refs[$res1['REF']]['LCL_SUM'] = ( array_key_exists('LCL_SUM', $unique_refs[$res1['REF']]) ) ? ( floatval( $unique_refs[$res1['REF']]['LCL_SUM'] ) + floatval( $res1['LCL_D_CBM'] ) ) : 1;
}
foreach( $unique_refs AS $unique => $data ){
$title = $unique;
$sum = $data['SUM'];
$lcl_sum = $data['LCL_SUM'];
echo "{$title}<br />{$sum}<br />{$lcl_sum}";
}
I have a multi dimensional array built dynamically.
Is it possible to get the value of an element by using another element.
e.g : search with idQ = 26 and get in return value its neighbor element values like :: idA=>49 and A=>500-10000
[0]=>
array(5) {
["idQA"]=>
string(3) "194"
["idQ"]=>
string(2) "26"
["Q"]=>
string(58) "Imposition supérieur
à 2500€ d’impôts annuel"
["idA"]=>
string(2) "49"
["A"]=>
string(10) "5000-10000"
}
[1]=>
array(5) {
["idQA"]=>
string(3) "173"
["idQ"]=>
string(2) "22"
["Q"]=>
string(20) "Si oui, laquelle(s):"
["idA"]=>
string(2) "32"
["A"]=>
string(7) "Voiture"
}
Example array :: https://gist.github.com/anonymous/9234703
Yes, like:
$founded = 0;
foreach($my_array[1] as $key => $val) {
if ($val == "something_to_search") {
$founded = $key;
break;
}
}
$before = $my_array[1][$founded-1];
$after = $my_array[1][$founded+1];
I am trying to do a custom WP_query loop and put the results into an array of associate arrays.. It is not working too well.. I realize the array_push overwrites any arrays that have the same indexes so I have the index increment +1 in the loop so they are not identical..however it still is not working.. My results show correct only on the first index (zero).. Here is my code:
<?php
$permlink='permalink';
$excerpt='exerpt';
$title='title';
$id='id';
$finalarray=array();
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
array_push($finalarray, $newitem);
}
$count=0;
foreach($finalarray as $item){
echo $count.':'.'<br>';
echo $item[$count]['title'.$count];
echo $item[$count]['id'.$count];
echo $item[$count]['permalink'.$count];
$count++;
}
var_dump($finalarray);
?>
Any my results show :
0:
title0id0permalink01:
2:
3:
4:
5:
6:
7:
8:
9:
10:
array(11) { [0]=> array(1) { [0]=> array(4) { ["id0"]=> string(3) "id0" ["title0"]=> string(6) "title0" ["excerpt0"]=> string(7) "exerpt0" ["permalink0"]=> string(10) "permalink0" } } [1]=> array(1) { [0]=> array(4) { ["id1"]=> string(3) "id1" ["title1"]=> string(6) "title1" ["excerpt1"]=> string(7) "exerpt1" ["permalink1"]=> string(10) "permalink1" } } [2]=> array(1) { [0]=> array(4) { ["id2"]=> string(3) "id2" ["title2"]=> string(6) "title2" ["excerpt2"]=> string(7) "exerpt2" ["permalink2"]=> string(10) "permalink2" } } [3]=> array(1) { [0]=> array(4) { ["id3"]=> string(3) "id3" ["title3"]=> string(6) "title3" ["excerpt3"]=> string(7) "exerpt3" ["permalink3"]=> string(10) "permalink3" } } [4]=> array(1) { [0]=> array(4) { ["id4"]=> string(3) "id4" ["title4"]=> string(6) "title4" ["excerpt4"]=> string(7) "exerpt4" ["permalink4"]=> string(10) "permalink4" } } [5]=> array(1) { [0]=> array(4) { ["id5"]=> string(3) "id5" ["title5"]=> string(6) "title5" ["excerpt5"]=> string(7) "exerpt5" ["permalink5"]=> string(10) "permalink5" } } [6]=> array(1) { [0]=> array(4) { ["id6"]=> string(3) "id6" ["title6"]=> string(6) "title6" ["excerpt6"]=> string(7) "exerpt6" ["permalink6"]=> string(10) "permalink6" } } [7]=> array(1) { [0]=> array(4) { ["id7"]=> string(3) "id7" ["title7"]=> string(6) "title7" ["excerpt7"]=> string(7) "exerpt7" ["permalink7"]=> string(10) "permalink7" } } [8]=> array(1) { [0]=> array(4) { ["id8"]=> string(3) "id8" ["title8"]=> string(6) "title8" ["excerpt8"]=> string(7) "exerpt8" ["permalink8"]=> string(10) "permalink8" } } [9]=> array(1) { [0]=> array(4) { ["id9"]=> string(3) "id9" ["title9"]=> string(6) "title9" ["excerpt9"]=> string(7) "exerpt9" ["permalink9"]=> string(10) "permalink9" } } [10]=> array(1) { [0]=> array(4) { ["id10"]=> string(4) "id10" ["title10"]=> string(7) "title10" ["excerpt10"]=> string(8) "exerpt10" ["permalink10"]=> string(11) "permalink10" } } }
So it looks like the values are in the array, however, only the first index ( zero ) prints correctly.. any suggestions? Also, is there any way i can push an associate array and it not be over written so I dont have to increment the index?
There are a few problems here.
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
array_push($finalarray, $newitem);
}
You are pushing an array, containing an array, containing 4 elements into $finalarray. There is no need for the outer array(). You can just do $newitem = array(...). array_push appends to end of the array, incrementing the index for you.
Second, in your foreach, your $count variable is not needed at all. When using foreach, $item is the element of the array. No need to look it up by the index.
If you want the index, however, foreach can give it to you.
foreach($finalarray as $count=>$item){
echo $count.':'.'<br>';
echo $item['title'.$count];
echo $item['id'.$count];
echo $item['permalink'.$count];
}
Your original code wasn't working because in each $item there was one array. That array had one item. In each element, the inner aray was always at index 0. Incrementing $count made it so only the 1st element worked, the others didn't because $item[1] didn't exist, it was always $item[0].
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
$finalarray[] = $newitem;
unset($newitem);
}
Since you don't care about the key of the final array just add it on using $finalarray[]
I'm trying to add another column of data to each row in a foreach loop. It's purpose is to remember the element of data importeded from XML processed to an multidimensional array. It's stuck as a scalar though the var_dumps looks fine.
<?php
$KEY = 0;
foreach ($eventsArray as $keyMe){
$thisKey['KEY'][0] = strval($KEY);
$keyedArray = array_merge($keyMe, $thisKey);
$KEY++;
}
// Prep for multisort
foreach ($keyedArray as $key => $value){
$date[$key] = $value['DATE'];
$title[$key] = $value['TITLE'];
$link[$key] = $value['LINK'];
$slide[$key] = $value['SLIDE'];
$location[$key] = $value['LOCATION'];
$time[$key]= $value['TIME'];
$KEY[$key] = $value['KEY']; // Warning: Cannot use a scalar value as an array
}
/* var_dump(
array(7) {
["DATE"]=> array(1) { [0]=> string(10) "2012-12-18" }
["TITLE"]=> array(1) { [0]=> string(20) "Event Title" }
["LINK"]=> array(1) { [0]=> string(38) "aLinkLocation.htm" }
["SLIDE"]=> array(1) { [0]=> string(2) "16" }
["LOCATION"]=> array(1) { [0]=> string(8) "Location of Event" }
["TIME"]=> array(1) { [0]=> string(3) "8am" }
["KEY"]=> array(1) { [0]=> string(2) "23" }
}
*/