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.

5
  • 1
    Please show input example and expected output. Commented Dec 4, 2016 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. Commented Dec 4, 2016 at 1:12
  • I was wondering about that. I'm using var_dump with Xdebug extension. Commented Dec 4, 2016 at 1:16
  • Try var_dump($results['files'][0]) to confirm what's really there. Commented Dec 4, 2016 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! Commented Dec 4, 2016 at 1:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help!
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"

2 Comments

Thanks for the answer. That was a mistake left in from an irrelevant if statement. I removed it from the question for clarity.
Thanks but that was not the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.