0

I'm trying to add an array to another array at key inside a foreach loop. Here is my function. Here is my function:

    $dirs = scandir( $dir );
    $results = array( 'folders' => [] );

    foreach ( $dirs as $dir ) {

            if ( is_dir( $this::uploads_path() . '/' . $dir ) ) {
                array_push( $results['folders'], $dir );    
            } else {

                $file = array(
                    'filename' => $dir,
                    'filesize' => 1000
                );

                $results['files'][] = $file;

            }

    }

    return $results;

It seems however I try, I get keys with empty values. Like this:

 'files' => 
    array (size=7)
      0 => 
        array (size=2)
          ...
      1 => 
        array (size=2)
          ...
      2 => 
        array (size=2)
          ...
      3 => 
        array (size=2)
          ...
      4 => 
        array (size=2)
          ...
      5 => 
        array (size=2)
          ...
      6 => 
        array (size=2)
          ...

I've also tried delaying adding the array by building a new array with the file information and adding it to $results outside the foreach loop. Thanks for the help. Would love an explanation as to why I'm getting empty values rather than just a fixed code example.

  • 1
    Please show input example and expected output. – cFreed Dec 4 '16 at 0:46
  • What are you using to print array? I'm not aware of anything that'll give you "size=7" and "size=2", for example. Not var_export, print_r, nor var_dump. Whatever it is, "size =2" implies the sub-arrays are in fact there with 2 elements such as with keys filename and filesize ... but instead it prints ... for brevity. – BareNakedCoder Dec 4 '16 at 1:12
  • I was wondering about that. I'm using var_dump with Xdebug extension. – heller_benjamin Dec 4 '16 at 1:16
  • Try var_dump($results['files'][0]) to confirm what's really there. – BareNakedCoder Dec 4 '16 at 1:17
  • Wow.... Looks like its some kind of abbreviation. It's all there. I just started using Xdebug, is that a normal facet of Xdebug (that array was also nested in another array with some code). Sorry for the dumb question everyone! – heller_benjamin Dec 4 '16 at 1:19
0

See Xdebug documentation for var_dump

Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes ... and places limits on the amount of array elements/object properties, maximum depth and string lengths.

As you said above, your are using var_dump and xdebug. It is limiting the depth of arrays its printing. So instead of seeing your real data, xdebug is truncating it to ... (after indicating size=2).

Looks like you can set xdebug.var_display_max_depth to increase the depth.

0

just remove the } before return.

After I remove, I've test it.

kris-roofe@krisroofe-Rev-station:~$ php test.php array(2) { ["folders"]=> array(2) { [0]=> string(1) "." [1]=> string(2) ".." } ["files"]=> array(25) { [0]=> array(2) { ["filename"]=> string(9) ".ICE-unix" ["filesize"]=> int(100) } [1]=> array(2) { ["filename"]=> string(10) ".Test-unix"

  • Thanks for the answer. That was a mistake left in from an irrelevant if statement. I removed it from the question for clarity. – heller_benjamin Dec 4 '16 at 1:06
  • it is the issue, correct it then it works. – Kris Roofe Dec 4 '16 at 1:07
  • Thanks but that was not the issue – heller_benjamin Dec 4 '16 at 1:08
  • i test it, works well when i remove it. – Kris Roofe Dec 4 '16 at 1:10
  • show more code, and var_dump dirs and you result – Kris Roofe Dec 4 '16 at 1:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.