a function to test a array for having a value?

Discussion in 'PHP' started by mahmood, Jan 1, 2006.

  1. #1
    I have a multidimentional array and I am trying to find out if the array contains any value or not. both empty() and isset() return true even when the array contains noting.

    for example:
    
    $list[] = "";
    $list[] = "";
    $list[] = "";
    
    PHP:
    I am looking for a function to tell me the items in list are empty or "" or whatever.


    .
     
    mahmood, Jan 1, 2006 IP
  2. seopup

    seopup Peon

    Messages:
    274
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hope this is what you need
     
    seopup, Jan 1, 2006 IP
  3. n0other

    n0other Peon

    Messages:
    146
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This will work only with arrays of simple structure. One of the universal solutions would be to 'flatten' the multidimensional array, count the number of empty elements in it and compare to the total count of elements.

    
    $list[] = "";
    
    $list[] = "";
    
    $list[] = ""; 
    
    $list[] = $list;
    
    # (c) Yassin Ezbakhe <yassin88 at gmail dot com>, php.net doc notes.
    function array_values_recursive($array)
    {
       $arrayValues = array();
    
       foreach ($array as $value)
       {
           if (is_scalar($value) OR is_resource($value))
           {
                 $arrayValues[] = $value;
           }
           elseif (is_array($value))
           {
                 $arrayValues = array_merge($arrayValues, array_values_recursive($value));
           }
       }
    
       return $arrayValues;
    }
    
    $v = array_values_recursive($list);
    
    $empty = 0;
    
    foreach ($v as $val) {
    
    	if (empty($val)) {
    	
    		$empty++;	
    		
    	}
    	
    }
    
    if ($empty == sizeof($v)) {
    
    	echo 'We have an empty array here!';
    	
    }
    
    PHP:
     
    n0other, Jan 1, 2006 IP