Why does my array have an index and a key?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

AZDJedi
New php-forum User
New php-forum User
Posts: 6
Joined: Fri Mar 16, 2012 9:20 am

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?

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

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;
      }


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

cron