Welcome Guest, Not a member yet? Register   Sign In
single query returning many arrays instead of one
#1

[eluser]stef25[/eluser]
im loving CI development so far but there are a few, what i think are basic concepts that i have not figured out yet

model:

Code:
function get_all_tags()
        {
            $this->db->select('tag_name');
            return $this->db->get('tags')->result_array();
        }

controller:

Code:
//get all tags
$data['existing_tags'] = $this->Questions->get_all_tags();    
print_r($data['existing_tags']); exit;

prints out:

Code:
Array
(
    [0] => Array
        (
            [tag_name] => accomodation
        )

    [1] => Array
        (
            [tag_name] => work
        )

    [2] => Array
        (
            [tag_name] => flights
        )

    [3] => Array
        (
            [tag_name] => nightlife
        )

)

is there a way to consolidate this in to one single array? i find myself doing quite alot of foreaches and working with arrays in arrays ... it feels like it could be simpler?

EDIT: okay, brainfart. should use $existing_tags = $this->Questions->get_all_tags(); and fixed!
#2

[eluser]slowgary[/eluser]
This would also solve itself inside your view.
#3

[eluser]stef25[/eluser]
actually, this still returns a bunch of arrays inside an array. if the query returns 10 values, there are 10 arrays within the main array.

is there no way the query can return one single array with 10 elements in it, instead of 10 arrays with each one element?
#4

[eluser]slowgary[/eluser]
No. Each database row will have it's own array of fields. All the rows combined make up your result, which in itself is another array. So you'll always have an array of arrays.

Just do this in your view and get over it ;-P :
Code:
foreach($existing_tags as $tag)
{
     echo $tag['tag_name'];
}

I understand that you're asking for only one column per row, so maybe it makes sense to you that it would come back as a single array, but in cases where you are asking for multiple columns it needs to come back as a multidimensional array. For this reason, your results will always be returned as multidimensional. Would you expect the database class to differentiate between the two and return either a single array or multidimensional array depending on the number of columns per row? I wouldn't. Also, what happens if you write your code to handle the single array, then decide later to fetch another column in your query? Then your code breaks because it's been expecting a single array and now it's getting a multidimensional one.

Moral of the story:

Don't be afraid of multidimensional arrays.
They like you.
Be one.

- Out.
#5

[eluser]slowgary[/eluser]
If you REALLY REALLY REALLY need it to be a single array (but I don't recommend it), you could probably use something like MySQL's GROUP CONCAT or something to get all of your results returned in one row, then in CI use row_array() instead of result_array(). This is NOT a good idea though, so I will officially cast my vote for candidate number 1, which is a single foreach() with the 'as' keyword.

Good luck with that.
#6

[eluser]stef25[/eluser]
i hear you, problem is that im not sending this to a view. im trying to find elements from this multidimensional array that occur in another array.

that requires 2 if not 3 nested foreach's (with further if(in_array) checks in there). that's what i want to avoid.

to get around this im using a function that flattens the first array, but that keeps giving undefined function errors (thread)




Theme © iAndrew 2016 - Forum software by © MyBB