Page 1 of 1

Bizarre trouble setting array elements

Posted: Fri Apr 25, 2008 6:03 pm
by pickle
Hi all,

This one has me completely baffled. I'm simply trying to build an array from a result set. Pretty simple & straightforward. However, it seems only the last two elements are being stored in the array. The first however-many-other elements there are just disappear into thin air.

Here's my code snippet

Code: Select all

 
//This code is run in an object, so I'm accessing $this a lot
 
//$this->DB->execute simply executes the query & returns a mysqli result set
$result = $this->DB->execute($query,"trying to gather $type");
if($result && $this->DB->numRows($result) > 0)
{       
    //$row is an array - the result of calling mysqli_result::fetch_assoc()
    while($row = $this->DB->getData($result))
    {
        //the function this code is in accepts 1 argument,type, with the value "children" or "siblings"
        //that argument is used to determine which variable these values get stored in
        $this->{$type}[$row['id']] = array('menu_title'=>$row['menu_title'],
                                        'publish'=>$row['publish'],
                                        'type'=>$row['type'],
                                        'url'=>$row['url']);
    }
}
What's especially bizarre is if I insert a $this->{$type}[$row['id']] = 'test' line in before the current $this->{$type}... line, the last 3 elements show up. If I insert 2 extra dummy lines, the last 4 elements show up.

On each iteration I can print_r() the row & it's populated just fine.

I have never seen anything like this - does anyone have any idea at all?

Thanks.

Re: Bizarre trouble setting array elements

Posted: Tue Apr 29, 2008 4:02 pm
by RobertGonzalez
Fancy me this for a second if you will:

Code: Select all

 
    //$row is an array - the result of calling mysqli_result::fetch_assoc()
    $tmp = array();
    while($row = $this->DB->getData($result))
    {
        //the function this code is in accepts 1 argument,type, with the value "children" or "siblings"
        //that argument is used to determine which variable these values get stored in
       
        $tmp[$row['id']] = array('menu_title'=>$row['menu_title'],
                                        'publish'=>$row['publish'],
                                        'type'=>$row['type'],
                                        'url'=>$row['url']);
    }
    var_dump($tmp);
    $this->{$type} = $tmp;
    var_dump($this->{$type});
 
What does this do, if anything?

Re: Bizarre trouble setting array elements

Posted: Tue Apr 29, 2008 4:14 pm
by pickle
That properly puts the values I want into the array I want. If I take out the var_dump() calls, that is a solution.

In the words of Ryan Reynolds in Harold and Kumar go to Whitecastle:

"But why?"

Re: Bizarre trouble setting array elements

Posted: Tue Apr 29, 2008 4:18 pm
by RobertGonzalez
Honestly, I don't know. I was half expecting it to not work. Now I am baffled too.