Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Why does my array have an index and a key?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Why does my array have an index and a key?

Postby AZDJedi » Tue Nov 20, 2012 1:51 pm

Hi,

I can't seem to figure out, or Google an answer as to why the my code produces the following:

Code: Select all
      $this->db->connect();
      $this->db->query("SELECT Friend FROM `customer-friend` WHERE CustomerID=$customerID");
                     
      $result = array();
      while ( $row = $this->db->nextRecord() ) {
         array_push($result, $row);
      }
      $this->db->disconnect();


The output is as follows

Code: Select all
Array
(
    [0] => Array
        (
            [0] => 1
            [Friend] => 1
        )

    [1] => Array
        (
            [0] => 92
            [Friend] => 92
        )

    [2] => Array
        (
            [0] => 99
            [Friend] => 99
        )
)


My question is why does it duplicate the values for each element?
AZDJedi
New php-forum User
New php-forum User
 
Posts: 6
Joined: Fri Mar 16, 2012 9:20 am

Re: Why does my array have an index and a key?

Postby simplypixie » Tue Nov 20, 2012 11:53 pm

Try changing this
Code: Select all
while ( $row = $this->db->nextRecord() ) {
         array_push($result, $row);
      }

To this
Code: Select all
while ( $row = $this->db->nextRecord() ) {
         $result[] = $row;
      }
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron