0
[pending_work] => Array
        (
            [0] => Array
                (
                    [tmp_name] => D:\xampp\tmp\php4B33.tmp
                    [error] => 0
                    [name] => Chrysanthemum.jpg
                    [type] => image/jpeg
                    [size] => 879394
                )

            [1] => Array
                (
                    [tmp_name] => 
                    [error] => 4
                    [name] => 
                    [type] => 
                    [size] => 0
                )

            [2] => Array
                (
                    [tmp_name] => 
                    [error] => 4
                    [name] => 
                    [type] => 
                    [size] => 0
                )

            [3] => Array
                (
                    [tmp_name] => 
                    [error] => 4
                    [name] => 
                    [type] => 
                    [size] => 0
                )

        )

This is the above array which is giving array values. As you can see there is only one 'tmp_name' field which is not empty and other are empty. How can i check and count wether atleast one of tmp_name is not empty ?. I have four input types for uploading images and I want to check for atleast one of the image should be uploaded.

3
  • 1
    Iterate over an array with a foreach. You know how to use foreach? Commented Aug 12, 2017 at 10:00
  • 1
    Please try to come up with a user name that is acceptable first. Commented Aug 12, 2017 at 10:01
  • It can help . . php.net/manual/en/function.array-filter.php Commented Aug 12, 2017 at 10:06

4 Answers 4

1

use array_column and array_filter

if(count(array_filter(array_column($array['pending_work'],'tmp_name')))>0){

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

Comments

0

The best approach, offering most flexibility, is a filter strategy:

<?php
$pending_work = [
     [
        'tmp_name' => 'D:\\xampp\\tmp\\php4B33.tmp',
        'error' => 0,
        'name' => 'Chrysanthemum.jpg',
        'type' => 'image/jpeg',
        'size' => 879394
    ],
    [
        'tmp_name' => null,
        'error' => 4,
        'name' => null,
        'type' => null,
        'size' => 0
    ],
    [
        'tmp_name]'=> null,
        'error' => 4,
        'name' => null,
        'type' => null,
        'size' => 0
    ],
    [
        'tmp_name' =>  null,
        'error' => 4,
        'name' => null,
        'type' => null,
        'size' => 0
    ]
];
$filtered_work = array_filter($pending_work, function($entry) {
    return !empty($entry['tmp_name']);
});
print_r($filtered_work);

The output of above code obviously is:

Array
(
    [0] => Array
        (
            [tmp_name] => D:\xampp\tmp\php4B33.tmp
            [error] => 0
            [name] => Chrysanthemum.jpg
            [type] => image/jpeg
            [size] => 879394
        )

)

You can count entries in there or use the result, whatever.

Comments

0

The function array_filter() alone will not remove empty sub-arrays from a multi-dimensional array, An additional function needs to be used with array_filter() function to remove empty elements from multidimensional array in PHP.

  • array_map() function sends the elements of array to the callback function.
  • array_filter() function filters the elements of array using a callback function.

The following code will remove all empty array elements from multidimensional array in PHP:

<?php
$array = array_map('array_filter', $array);
$array = array_filter($array);

Comments

0

Use below code array_filter helps in such conditions.

Click here for demo

$array = [
     [
        'tmp_name' => 'D:\\xampp\\tmp\\php4B33.tmp',
        'error' => 0,
        'name' => 'Chrysanthemum.jpg',
        'type' => 'image/jpeg',
        'size' => 879394
    ],
    [
        'tmp_name' => null,
        'error' => 4,
        'name' => null,
        'type' => null,
        'size' => 0
    ],
    [
        'tmp_name]'=> null,
        'error' => 4,
        'name' => null,
        'type' => null,
        'size' => 0
    ],
    [
        'tmp_name' =>  null,
        'error' => 4,
        'name' => null,
        'type' => null,
        'size' => 0
    ]
];
$result_arary = array_filter($array, function($arr) {
    return !empty($arr['tmp_name']);
});
echo count($result_arary);
print_r($result_arary);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.