Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

For a graph I'm working on, I'm trying to generate a nested array. I'm querying the database to try and get all the data from a certain column in my database as an array, but i'm not really quite sure how to do it. Currently it giving me the last row in my database.

Here is my code:

    function get_data()
{
    $this->db->select('ItemName, QuantitySold');
    $query = $this->db->get('transactions');

    $results = array();

    foreach ($query->result_array() as $row)
        {
           $row['QuantitySold'];
           $row['ItemName'];
        }

        $results = array(
            'name' => 'shirt',
            'data' => $row['QuantitySold']
            );
        $test = json_encode($results);
        echo $test;
        return $results;
}

It is generating this result:

    {"name":"shirt","data":"9"}

And I need it to look something like this:

    {'name':'shirt','data':array(1, 2, 3,...)}

Database Structure: http://d.pr/i/cQaW

Rows: http://d.pr/i/8vp2

share|improve this question
    
Are you sure that's the output you want? Where are the 3 and the 1 supposed to come from? Your database structure might help us understand better. –  John V. Mar 9 at 2:38
    
It doesn't matter how many, I need the data to be an array. Like {'name':'shirt', 'data':array(1,2,3...)} –  Jake Ols Mar 9 at 2:39
    
The 3 9 and 1 were just examples –  Jake Ols Mar 9 at 2:41
    
I more meant where are the supposed to come from, not where do those specific values come from. Sorry for the confusion. Could you post what your database's columns look like and a sample row (or two) to help us understand your question better? –  John V. Mar 9 at 2:43
    
@JohnV. Just posted. –  Jake Ols Mar 9 at 2:46

2 Answers 2

up vote 0 down vote accepted

Here is what I would do, note how I take the rows and store them differently than you currently do:

function get_data()
{
    $this->db->select('ItemName, QuantitySold');
    $query = $this->db->get('transactions');

    $results = array();

    foreach ($query->result_array() as $row)
    {
        $results[$row['ItemName']][] = $row['QuantitySold'];
    }

    $test = json_encode($results);
    echo $test;
    return $results;
}

Note that this isn't quite how you want the output, but it might be easier to process this way.

Output example:

{'shirt': [1, 2, 3], 'notshirt': [1, 2, 3]}

EDIT (for format):

function get_data()
{
    $this->db->select('ItemName, QuantitySold');
    $query = $this->db->get('transactions');

    $results = array();

    foreach ($query->result_array() as $row)
    {
        if(!isset($results[$row['ItemName']]))
            $results[$row['ItemName']] = array('name' => $row['ItemName'], 'data' => array());
        $results[$row['ItemName']]['data'][] = $row['QuantitySold'];
    }

    //Rekey arrays so they aren't associative
    $results = array_values($results);

    $test = json_encode($results);
    echo $test;
    return $results;
}

Output example:

{{name: 'shirt', data: [1, 2, 3], {'name': 'notshirt', data: [1, 2, 3]}}
share|improve this answer
    
Because i'm using it to create a graph, I need the 'name':'shirt' and 'data':[1,2,3..] array structure. I'll see what I can do with this though, thanks! –  Jake Ols Mar 9 at 3:54
    
I'll make an edit that might help format the array that way. –  John V. Mar 9 at 4:05
    
thanks, that would be wonderful. –  Jake Ols Mar 9 at 4:07
    
Something seems to be broken, i'm not getting any output. –  Jake Ols Mar 9 at 4:31
    
Woops, try it now. –  John V. Mar 9 at 4:33
 $result = array();

 foreach ($query->result_array() as $row)
 {
   //if new item, initiate array in order not to get an exception
   if( !isset($result[$row['ItemName']]) ) {
     $result[$row['ItemName']]['data'] = array();
     $result[$row['ItemName']]['name'] = $row['ItemName'];
   }
   //group data by item name

   $result[$row['ItemName']]['data'][] = $row['QuantitySold'];
 }

 echo json_encode($result);
share|improve this answer
    
I definitely like your approach, but the code doesn't seem to be working. –  Jake Ols Mar 9 at 3:57
    
try it now, I fixed some syntax error and initiated empty $result array for your convenience –  castt Mar 9 at 4:09
    
The array structure is off, I'll work through this though. –  Jake Ols Mar 9 at 4:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.