Bizarre trouble setting array elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Bizarre trouble setting array elements

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Bizarre trouble setting array elements

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Bizarre trouble setting array elements

Post 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?"
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Bizarre trouble setting array elements

Post by RobertGonzalez »

Honestly, I don't know. I was half expecting it to not work. Now I am baffled too.
Post Reply