1

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"
  }
5
  • can you show us what is in $rows? Commented Nov 8, 2013 at 19:58
  • Why don't you try a print_r on $rows and/or $row? That might help. Commented Nov 8, 2013 at 20:00
  • Your results make perfect sense since you're assigning x and y both the same value. Did you mean $_rows[$i]['x'] = $column; Commented Nov 8, 2013 at 20:00
  • hello, please use $key variable instead of $i, model (in codegniter concept) should return array of objects or pure array so it is indexed from 0 to inf Commented Nov 8, 2013 at 20:00
  • Next time it would be helpful to add "Codeigniter" to the title of the question. Such as, "How to Add Keys to PHP Array in Codeigniter." At least you did tag it codeigniter, so that's good. Commented Nov 8, 2013 at 20:36

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

// this is the solution. Changing my Model to x and y values and changing the controller part to your solution gives the correct output. Thanks a lot for you help!
Thanks. If this worked you should accept the answer. Another option, too, would be to change the values to something else, so for example, if you plan to use it for something else, it might be better not to use "x" and "y" but "positive" and "stamp," and simply set the values in a different way. This is just the easiest way to do it and set the values you want.
I updated the original answer to reflect that. Either way you should use an alias as its easier to read, use, and remember. Glad I could be of help.
1

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']
    );
}   

2 Comments

this causes an error: Fatal error: Cannot use object of type stdClass as array
That's because codeigniter generates them as objects, so it would actually be $row->x-column-name-here. Based on the output of $rows that was added to the first post, it wouldn't work anyway because the value contains special characters. foreach() will loop through objects though, hence why foreach() will output the columns in $row.
1
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.

3 Comments

I wasn't sure if it was possible to use an object that way. I did know I could create a variable like $pos = SUM(positive) and do it $row->$pos, so plus one for that. Still, the better option is to simply add an alias to them that's more descriptive.
I'd agree for the most part but for all I know that method is parsing a file or something that is abstractly assigning the keys. Sometimes you don't have full control of how the data is presented to you so knowing the string literal object key is useful.
Yeah, that's why I gave you a plus one, its very useful to know if its parsing a file or something similar and there's special characters that normally wouldn't work as an object. Setting an alias is the better option, but it doesn't work in all cases.
0

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.

3 Comments

This does only output the x value.. not the y value.
Its not even a good example either. This is a horrible way to do it. $column is an actual column name, not an integer. But this is just too complex; even if you were going to do it like this, set the "$column%2==0?'x':'y'" to a separate variable BEFORE marking it as the key.
Sorry, I didn't realize until now that it was a codeIgniter specific question. Otherwise it would depend on how the query was ran and/or how the database was setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.