简体   繁体   中英

ARRAY PHP How to put variables in array

Hi guys i would like to ask help on my problem..

I have 4 variables made from my query:

$sql1 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW()) AND order_action = 'Online Transaction' "));

$sql2 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW()) AND order_action = 'Walkin Transaction' "));

$sql3 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW())-1 AND  order_action = 'Online Transaction' "));

$sql4 = mysql_num_rows(mysql_query("SELECT * FROM tbl_reservation WHERE YEAR(date_added) = YEAR(NOW())-1 AND  order_action = 'Walkin Transaction' "));

I would like to put them in an array..to be exactly on my expectations.

Here is my sample code:

$stack = array($sql1, $sql2);
array_push($stack, $sql3, $sql4);
$query = print_r($stack);
$result = $query;


print json_encode($result);

It display like this on my browser:
Array ( [0] => 0 [1] => 8 [2] => 0 [3] => 1 ) true

But i want to display it like this: [{"0","1"}, {"2","3"}]

I am planning to make a line graph on this.. I have a Chart.min.js and jquery.min.js

If any way.. The whole point.. i want to make a line graph that would compare my data from current year to last year.

Help me pls. :(

Give a try

<?php
$a = array(0,8,0,1) ;
echo "<pre>";print_r(json_encode(array_chunk($a, 2)));
?>

with that. You can get this result

[[0,8],[0,1]]

Btw, Why you need to do that with your array ? .

Edited

After i check your question again, So you want get the order

<?php
$stack = array(0,8,0,1) ;

$array2= array();
foreach ($stack  as $key => $val) {
   $array2[] = $key;
}

echo "<pre>";print_r(json_encode(array_chunk($array2, 2)));

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM