0

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.

pia-sophie
  • 505
  • 4
  • 21

1 Answers1

1

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
rkeet
  • 3,406
  • 2
  • 23
  • 49
  • $test=count($data['PAD_Document_Path']); works gives the amount of 3 which is ok for my testscenario $countpads =$this->getRequest()->getFiles()->count(); doesn't work, gives the amount of 1 – pia-sophie Oct 10 '18 at 16:06
  • 1
    Alright, thanks for giving that a go. Have removed it from the answer. – rkeet Oct 10 '18 at 17:57