Search code examples
phpzend-frameworkzend-formzend-framework3

counting data in a multidemensional array


I upload several files with Zend and want to count the $data array to get the filenames.

$data = array_merge_recursive(
                    $this->getRequest()->getPost()->toArray(),
                    $this->getRequest()->getFiles()->toArray()
                    );

I found this code for counting in another post

array_sum(array_map("count", $data))-1;

It gives the right amount of pieces and I can get the names, but it also gives a warning:

Warning: count(): Parameter must be an array or an object that implements Countable

I tried to give the other dimensions like:

array_sum(array_map("count", $data['fieldname']))-1;

But like expected, it doesn't work.

Any help appreciated! The only question is how to get the amount of given filenames.

***edit regarding to the answer I post a screenshot how the $data array looks like, which is correct

enter image description here

And here what the statement with the warning counts (of course 1 is to subtract). Perhaps there is an other possibility to count.

enter image description here

So everything might be nice, but I want to get rid of the warning.


Solution

  • Judging by the added screenshot of the $data array, shouldn't you be doing this the below?

    count($data['PAD_Document_Path'])
    

    Get specific filenames by looping like so

    foreach($data['PAD_Document_Path'] as $key => $value) {
        $name = pathinfo($value)['filename'];
        // do stuff with name
    }
    

    See pathinfo. From that docs page:

    Example #1 pathinfo() Example

    <?php
    $path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
    
    echo $path_parts['dirname'], "\n";
    echo $path_parts['basename'], "\n";
    echo $path_parts['extension'], "\n";
    echo $path_parts['filename'], "\n"; // since PHP 5.2.0
    ?>
    

    The above example will output:

    /www/htdocs/inc
    lib.inc.php
    php
    lib.inc