I'm trying to build an array for feeding my Graph. I use the code below:
$rows = $this->Website_model->getGraphDataPositives();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
foreach ($row as $column => $value) {
$_rows[$i]['x'] = $value;
$_rows[$i]['y'] = $value;
$i++;
}
}
This results in the following response:
array(48) {
[0]=>
array(2) {
["x"]=>
string(7) "3283581"
["y"]=>
string(7) "3283581"
}
[1]=>
array(2) {
["x"]=>
string(10) "2013-10-16"
["y"]=>
string(10) "2013-10-16"
}
So it isn't okay yet.. it should say:
array(48) {
[0]=>
array(2) {
["x"]=>
string(7) "3283581"
["y"]=>
string(7) "2013-10-16"
}
[1]=>
array(2) {
["x"]=>
string(10) "1512116"
["y"]=>
string(10) "2013-10-17"
}
Can anyone tell me what I need to adjust in order to get the right output?
/////////////////////////////////
this is what's in $rows (a part of the output)
array(24) {
[0]=>
object(stdClass)#169 (2) {
["SUM(positive)"]=>
string(7) "3283581"
["DATE(stamp)"]=>
string(10) "2013-10-16"
}
[1]=>
object(stdClass)#160 (2) {
["SUM(positive)"]=>
string(7) "1512116"
["DATE(stamp)"]=>
string(10) "2013-10-17"
}
I think the first step here is to fix your output array from the Codeigniter model. Did you create:
$rows = $this->Website_model->getGraphDataPositives();
If so, you should be able to easily go into the function and change the output of the select statement.
Where you find "SUM(positive)" and "DATE(stamp)" in the function getGraphDataPositives(), change it to this (rough example, I don't know what the function looks like):
SUM(positive) AS x
DATE(stamp) AS y
Now you can just run it this way instead:
$rows = $this->Website_model->getGraphDataPositives();
$_rows = array();
foreach ($rows as $i => $row) {
foreach ($row as $column => $value) {
$_rows[$i][$column] = $value;
}
}
Also notice that I removed the $i = 0 and $i++ and replaced $key wit $i. Much easier that way.
Let me know if this helps.
EDIT: I accidentally kept the second $_rows[$i][$column] = $value; in there; that's not needed anymore. You only need one, and the way you have it set up its setting the same value to both entries.
EDIT 2: Just wanted to note that the above example may not be the best option, the best option would be to give more description aliases.
SUM(positive) AS positive
DATE(stamp) AS timestamp
Then set the values like this:
foreach ($rows as $i => $row) {
$_rows[$i]['x'] = $row->positive;
$_rows[$i]['y'] = $row->timestamp;
}
Either option will work, the first is just a little easier.
It would be easier if i knew the column names from SQL but here goes:
$rows = $this->Website_model->getGraphDataPositives();
$_rows = array();
foreach ($rows as $row) {
$_rows[] = array(
'x'=>$row['x-column-name-here'],
'y'=>$row['y-column-name-here']
);
}
foreach ($rows as $key => $row) {
// here $key is index, $row is the object
foreach ($row as $column => $value) {
// here $column will be "SUM(positive)" and the value will be 3283581
// for the first iteration. Since both are assigned $value
// $_rows[$i]['x'] and $_rows[$i]['y'] will be identical
$_rows[$i]['x'] = $value;
$_rows[$i]['y'] = $value;
$i++;
}
}
If you just use the object columns as you defined you should be ok:
foreach ($rows as $row) {
$_rows[$i]['x'] = $row->{'SUM(positive)'};
$_rows[$i]['y'] = $row->{'DATE(stamp)'};
}
You're also not using $key so might as well get rid of that as well.
Your example is kind of basic but I think this should solve its issues:
foreach ($rows as $key => $row) {
foreach ($row as $column => $value) {
$_rows[$key][$column%2==0?'x':'y'] = $value;
}
}
Provided you're using PDO::FETCH_NUM, ie your $rows are numerically indexed.
Related
I have following code in my controller:
$data= Yii::app()->db->createCommand()
->select('region_id')
->from('user_rights')
->where('user_group_id='.$findRegion['user_group_id'])
->queryAll();
foreach($data as $key=>$value){
$array_o[$key] = $value;
}
var_dump($array_o); returns following value:
array(2) { [0]=> array(1) { ["region_id"]=> string(4) "1703" } [1]=> array(1) { ["region_id"]=> string(4) "1706" } }
But, I need to get similar to following value:
array(2) { [0]=> string(4) "1703" [1]=> string(4) "1706" }.
How can I do it?
Just set the proper value right from the beginning:
foreach ($data as $key => $value){
$array_o[$key] = $value['region_id'];
}
You can use queryColumn() method
So it is just enough set statment
$data= Yii::app()->db->createCommand()
->select('region_id')
->from('user_rights')
->where('user_group_id='.$findRegion['user_group_id'])
->queryColumn();
and delete your foreach statment.
Try to make it like this
foreach($data as $key=>$value){
$array_o[$key] = $value['region_id'];
}
In your foreach do this:
$array_o[$key] = $value['region_id'];
I want to create a list of variables from a foreach loop. I have an array like so
array(5) {
[0]=> string(105) "http://a4.mzstatic.com/us/r30/Purple5/v4/06/f2/02/06f20208-1739-4fda-c87f-171d73b912a7/screen480x480.jpeg"
[1]=> string(105) "http://a1.mzstatic.com/us/r30/Purple1/v4/53/89/0b/53890b90-c6a5-4db3-cfb9-d6314bf9cfd1/screen480x480.jpeg"
[2]=> string(105) "http://a3.mzstatic.com/us/r30/Purple1/v4/8f/31/b3/8f31b351-c9d7-e545-0ace-e09fcb390264/screen480x480.jpeg"
[3]=> string(105) "http://a5.mzstatic.com/us/r30/Purple3/v4/e7/5f/de/e75fde7b-dc5f-5b26-e531-abf07c409317/screen480x480.jpeg"
[4]=> string(105) "http://a1.mzstatic.com/us/r30/Purple3/v4/27/ce/c6/27cec64e-7e6d-7135-cc76-d0132151dadb/screen480x480.jpeg"
}
and I'd like to assign each to it's own variable. I want the final output to be something like
$var0 = "first array url";
$var1 = "second array url";
etc...
I think I want something like this but it's not quite the right syntax.
foreach ($array as $key => $value) {
$var[$key] = $value;
}
Any ideas?
foreach($array as $key => $value) {
${"var{$key}"} = $value;
}
The function you are searching for is extract.
Just use it after your foreach loop given in your example.
$vars = [];
foreach ($array as $i => $value) {
$vars['var'.$i] = $value;
}
extract($vars);
I want to buld a Json object to feed my graphs. I have got the following code to change my PHP object.
$rows = $this->Website_model->getGraphData();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
foreach ($row as $column => $value) {
$_rows[$i][] = $value;
}
$i++;
}
$rows = $_rows;
echo json_encode(array("sEcho" => intval($sEcho), "data" => $rows));
die();
My current ouput looks like this:
array(24) {
[0]=>
array(3) {
[0]=>
string(7) "3283581"
[1]=>
string(10) "2013-10-16"
}
It should look something like this:
{"y":15,"x":"2012-11-19"},{"y":18,"x":"2012-11-19"} etc etc
How can I add the Y and X to my data and take care i will get the right output to feed my graph?
/////////////////////////////////////////////////////
I tried the following:
Now i'm using the following code:
$rows = $this->Website_model->getGraphDataPositives();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
foreach ($row as $column => $value) {
$_rows[$i]['x'] = $value;
$_rows[$i]['y'] = $value;
$i++;
}
}
This results in the following response:
array(48) {
[0]=>
array(2) {
["x"]=>
string(7) "3283581"
["y"]=>
string(7) "3283581"
}
[1]=>
array(2) {
["x"]=>
string(10) "2013-10-16"
["y"]=>
string(10) "2013-10-16"
}
So it isn't okay yet.. it should say:
array(48) {
[0]=>
array(2) {
["x"]=>
string(7) "3283581"
["y"]=>
string(7) "2013-10-16"
}
[1]=>
array(2) {
["x"]=>
string(10) "1512116"
["y"]=>
string(10) "2013-10-17"
}
This would create an array like you wish :
$rows = $this->Website_model->getGraphData();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
$_rows[$i]['y'] = $rows[0];
$_rows[$i]['x'] = $rows[1];
$i++;
}
Your example data is not correct. You have array(3), but you show only 2 elements. Also in the output data you have 3283581 and the y values are 15 and 18. So there is no way I can guess how to convert those values.
I have got the following query which returns Dates (string value) and Numbers (integer value)
public function getGraphDataPositives() {
$query = 'SELECT
DATE(stamp) AS x, SUM(positive) AS y
FROM data
WHERE companyId = 3
GROUP BY DATE(stamp)';
$currentDate = date("%Y-m-d%");
$query = $this->db->query($query);
return $query->result();
}
But when I'm dumping my result in my controller I get an array with only strings. The values should be integer in order to use them for a graph.
Just the first piece of array:
array(25) {
[0]=>
object(stdClass)#169 (2) {
["x"]=>
string(10) "2013-10-16"
["y"]=>
string(7) "3283581"
}
[1]=>
object(stdClass)#160 (2) {
["x"]=>
string(10) "2013-10-17"
["y"]=>
string(7) "1512116"
}
I'm handling this array to create a Json object:
$_rows = array();
foreach ($rows as $i => $row) {
foreach ($row as $column => $value) {
$_rows[$i][$column] = $value;
}
}
$rows = $_rows;
echo json_encode(array("className" => ".main.l1","data" => $rows));
But the JSON object contains the values as a string, not like an integer like desired. What should I change?
Sample of JSON output:
{"className":".main.l1","data":[{"x":"2013-10-16","y":"3283581"},{"x":"2013-10-17","y":"1512116"},{"x":"2013-10-18","y":"3967"},{"x":"2013-10-19","y":"1094"},{"x":"2013-10-20","y":"853"},{"x":"2013-10-21","y":"1205"},{"x":"2013-10-22","y":"2618700"},{"x":"2013-10-23","y":"3928291"},{"x":"2013-10-24","y":"3670318"},{"x":"2013-10-25","y":"3347369"},{"x":"2013-10-26","y":"2525573"},{"x":"2013-10-27","y":"3224612"},{"x":"2013-10-28","y":"3992964"},{"x":"2013-10-29","y":"3949904"},{"x":"2013-10-30","y":"3568618"},{"x":"2013-10-31","y":"3104696"},{"x":"2013-11-01","y":"3246932"},{"x":"2013-11-02","y":"2817758"},{"x":"2013-11-03","y":"3198856"},{"x":"2013-11-04","y":"3952957"},{"x":"2013-11-05","y":"3934173"},{"x":"2013-11-06","y":"3878718"},{"x":"2013-11-07","y":"3642822"},{"x":"2013-11-08","y":"3388646"},{"x":"2013-11-09","y":"376763"}]}
Can anyone help me out on this one?
try something like this
try to check if value is Integer.If it is integer than parse it to string
foreach ($rows as $i => $row) {
foreach ($row as $column => $value) {
if(is_numeric($value)){
$_rows[$i][$column] = intval($value);
}else{
$_rows[$i][$column] = $value;
}
}
}
This code should explain it all..
foreach($this->sections as $k => $section){
$preview = array();
foreach ($section['fields'] as $k => $type) {
$preview[] = $type['type'];
}
if ($preview == 'header_preview'){
echo $preview;
// I will loop content here based on the $section loop NOT the $type loop
}
}
I just need to get each $section['fields'] then outside that loop, which again gets a list of all the $section['fields'] then use one of those field types to create an if statement. The above is non working, I will show you working code here.
foreach($this->sections as $k => $section){
foreach ($section['fields'] as $k => $type) {
if ($type['type'] == 'header_preview'){
//I work but im in a nested loop I need out
}
}
//The main loop here.. the above loop is just to setup data to use inside this loop? Make sense? I hope!
}
I hope this makes sense...
Snippet of var_dump $this->sections
array(26) { ["general"]=> array(3) { ["title"]=> string(7) "General" ["icon"]=> string(106) "img/icons/sub.png" ["fields"]=> array(5) { [0]=> array(6) { ["id"]=> string(10) "responsive" ["type"]=> string(6) "switch" ["title"]=> string(35) "Responsive" ["desc"]=> string(10) "Responsive" ["options"]=> array(2) { [1]=> string(2) "On" [0]=> string(3) "Off" }
It could be possible $k has been duplicated and therefore the loop doesn't know what to do. You can try changing $k to $x, see if it works.
foreach($this->sections as $k => $section){
$preview = array();
foreach ($section['fields'] as $x => $type) {
$preview[] = $type['type'];
}
foreach($preview as $elem){
if ($elem == 'header_preview'){
echo($elem);
}
}
}
Hmm... Maybe
foreach($this->sections as $k => $section){
$preview = array();
foreach ($section['fields'] as $x => $type) {
$preview[] = $type['type'];
}
if(!in_array('header_preview', $preview)){
// Here $preview DOES NOT contain 'header_preview'
// Do stuff
}
}
$header_preview=false;
foreach($this->sections as $k => $section){
$header_preview=false;// reset the var $header_preview
foreach ($section['fields'] as $k => $type) {
if ($type['type'] == 'header_preview') $header_preview = true;
}
if($header_preview==true){
//here you can add or extract from this array that has `header_preview` value for one of it's field type
}
}