php - Way to remove/ignore last entry in multidementional array -
i use multifile uploader thats easy use end users, there issue adds field allow user select more files different folders.
the issue produce this
array ( [files] => array ( [name] => array ( [0] => 1450282558_circle-next-arrow-disclosure-glyph.png [1] => 40525882_ml.jpg [2] => pdf-sample.pdf [3] => ) [type] => array ( [0] => image/png [1] => image/jpeg [2] => application/pdf [3] => ) [tmp_name] => array ( [0] => /tmp/phpovbkoc [1] => /tmp/phpncisas [2] => /tmp/phpurks37 [3] => ) [error] => array ( [0] => 0 [1] => 0 [2] => 0 [3] => 4 ) [size] => array ( [0] => 405 [1] => 218829 [2] => 7945 [3] => 0 ) ) ) the last entry in every part empty, there way can around this? want remove/ignore lets [3] under name, type etc. avoide empty entry?
you can use array_slice this. if array stored in $data, works this:
foreach($data["files"] $attr => $file) { $data["files"][$attr] = array_slice($data["files"][$attr], 0, -1); } print_r ($data); or use of by-reference operator (&), can done shorter:
foreach($data["files"] &$file) { $file = array_slice($file, 0, -1); } the output be:
array ( [files] => array ( [name] => array ( [0] => 1450282558_circle-next-arrow-disclosure-glyph.png [1] => 40525882_ml.jpg [2] => pdf-sample.pdf ) [type] => array ( [0] => image/png [1] => image/jpeg [2] => application/pdf ) [tmp_name] => array ( [0] => /tmp/phpovbkoc [1] => /tmp/phpncisas [2] => /tmp/phpurks37 ) [error] => array ( [0] => 0 [1] => 0 [2] => 0 ) [size] => array ( [0] => 405 [1] => 218829 [2] => 7945 ) ) ) the call array_slice has these arguments:
- the array take slice (i.e. sub-array);
- the index slice starts: specify 0;
- the length of slice. -1 takes except last element.
Comments
Post a Comment