Web Analytics Made Easy -
StatCounter Real strange associative behavior for an associative array - CodingForum

Announcement

Collapse
No announcement yet.

Real strange associative behavior for an associative array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Real strange associative behavior for an associative array

    I have an array that contain files/directory content information...

    This is the result of my dynamically built array :

    PHP Code:
        [alex] => Array
            (
                [
    type] => directory
                
    [name] => alex
                
    [date] => 2009-04-08 11:23:24
                
    [size] => 639 Ko
            
    )

        [
    user1] => Array
            (
                [
    type] => directory
                
    [name] => user1
                
    [date] => 2009-04-08 11:23:38
                
    [size] => 42 Ko
            
    )

        [
    0] => Array
            (
                [
    type] => directory
                
    [name] => 111111
                
    [date] => 2009-04-09 09:31:20
                
    [size] => 2 Ko
            
    )

        [
    1] => Array
            (
                [
    type] => directory
                
    [name] => 222222
                
    [date] => 2009-04-09 09:31:25
                
    [size] => 2 Ko
            

    What is wrong are indexes [0] and [1] who should be [111111] and [222222] respectively.

    This is the PHP who fill an array index (this code is in a foreach loop) :

    PHP Code:
    $dirs[utf8_encode($path)] = array(
        
    'type' => 'directory',
        
    'name' => utf8_encode($file),
        
    'date' => @date('Y-m-d H:i:s'$stats['mtime']),
        
    'size' => sizeToHRF(dir_size(USERS_DIRECTORIES_PATH.$pathtrue))
    ); 
    What is really weird is that $path contains '111111' (or '222222') but it seems that a string containing only numbers is converted to the first non associative index available (0, 1, 2, 3...). I tried to cast $path as string with (string) $path without success... If I do $dirs[utf8_encode($path . ' ')] all is right (except that I don't want the trailing space that I added)!!

    What is happening here!?? Many thanks for your help!
    Last edited by AlexV; Apr 9, 2009, 11:34 AM.

  • #2
    We would need more information to help. What is the code that sets $path and are you absolutely sure what is in it at the time the foreach() loop is actually executing for those two entries?
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment


    • #3
      OK I think I've found my problem... Seems to be the array_merge at the end who is the problem...

      This is the whole code:
      PHP Code:
          private function getDirContent()
          {
              
      $files = array();
              
      $dirs = array();

              
      $directory USERS_DIRECTORIES_PATH.$this->node;
              if (
      is_dir($directory))
              {
                  require_once(
      TOOLS_PATH.'directorySize.php');

                  require_once(
      TOOLS_PATH.'fileMimeType.php');

                  if (
      $dh opendir($directory))
                  {
                      while ((
      $file readdir($dh)) !== false)
                      {
                          if (
      $file !== '.' && $file !== '..')
                          {
                              
      $path = ($this->node !== '/' $this->node.'/'.$file $file);

                              
      $stats stat(USERS_DIRECTORIES_PATH.$path);
                              if (
      is_dir(USERS_DIRECTORIES_PATH.$path))
                              {
                                  
      $dirs[utf8_encode($path)] = array(
                                                          
      'type'    => 'directory',
                                                          
      'name'    => utf8_encode($file),
                                                          
      'date'    => @date('Y-m-d H:i:s'$stats['mtime']),
                                                          
      'size'    => sizeToHRF(dir_size(USERS_DIRECTORIES_PATH.$pathtrue))
                                                  );
                              }
                              else
                              {
                                  
      $files[utf8_encode($path)] = array(    'type'    => 'file',
                                                          
      'name'    => utf8_encode($file),
                                                          
      'date'    => @date('Y-m-d H:i:s'$stats['mtime']),
                                                          
      'size'    => sizeToHRF($stats['size']),
                                                          
      'ext'    => substr($file, (strrpos($file'.'))),
                                                          
      'mime'    => getMimeType($file)
                                                  );
                              }
                          }
                      }
                      
      closedir($dh);

                      
      ksort($dirs);
                      
      ksort($files);
                  }
              }

              return 
      array_merge($dirs$files);
          } 

      Comment


      • #4
        OK found out the problem... Seems that array_merge($dirs, $files); screw my '111111' and '222222' indexes.

        $dirs contains the correct version of my first post ([0] is [111111] and [1] is [222222]).

        and

        $files is an empty array (array()).

        array_merge of these two arrays seems to convert 111111 to 0 and 222222 to 1!

        Why!?????? Anyone understand that?
        Last edited by AlexV; Apr 9, 2009, 02:20 PM.

        Comment


        • #5
          Please explain me why indexes 111111 and 222222 are screwed up by array_merge...

          You can easily reproduce it with:
          PHP Code:
          $a = array('foo' => '1''bar' => '2''111111' => '3''222222' => '4');
          $b = array();

          $c array_merge($a$b);

          print_r($c); 

          Comment


          • #6
            Originally posted by TheWolf1 View Post
            Please explain me why indexes 111111 and 222222 are screwed up by array_merge...

            You can easily reproduce it with:
            PHP Code:
            $a = array('foo' => '1''bar' => '2''111111' => '3''222222' => '4');
            $b = array();

            $c array_merge($a$b);

            print_r($c); 
            I'm not sure if this is what you ask. See:

            If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
            http://www.php.net/manual/en/function.array-merge.php

            best regards

            Comment


            • #7
              Yes but how merging a filled array (a) with an empty one (b) can result in something different than "a"? (See my previous code to test t)

              Comment


              • #8
                Originally posted by TheWolf1 View Post
                Yes but how merging a filled array (a) with an empty one (b) can result in something different than "a"? (See my previous code to test t)
                array_merge build a new array from scratch. Prabably loop throu first array and look for the key in second array. If the key is a string is clear but when is numeric they can't decide if is a given key, by you, or represent the position in the array. So they decide to make this decision simple and build a numeric key from scratch.
                I don't know if I'm wrong or not, it's just my opinion,

                best regards

                Comment

                Working...
                X