0
 foreach ($this->CsInventory as $value)
    {

     print_r($value) // print 1
    $vname = $value[] = $value['VesselName']; 
    $total = $value[] = $value['Total']; 
    $Box = $value[] = $value['Box']; 

        print_r($value); // print 2

        $rdata .= '<td>'.$vname.'</td>
          <td>'.$total.'</td>
             <td>'.$Box.'</td>';                 
    }

Print 1

Array
(
    [VesselName] => MARIANNE
    [Total] => 13838
    [Box] => 1156
)
Array
(
    [Box] => 154
)
Array
(
    [Box] => 3825
)
Array
(
    [Box] => 50571
)

print 2

Array
(
    [VesselName] => MARIANNE
    [Total] => 15452
    [Box] => 1156
    [0] => MARIANNE
    [1] => 15452
    [2] => 1156
)
Array
(
    [Box] => 2276
    [0] => 
    [1] => 
    [2] => 2276
)
Array
(
    [Box] => 3825
    [0] => 
    [1] => 
    [2] => 3825
)
Array
(
    [Box] => 49235
    [0] => 
    [1] => 
    [2] => 49235
)

i how can i remove an empty value in the array? i try many ways but i can get any solution.. so decide to here in the forum?

mapet
  • 868
  • 1
  • 11
  • 21
  • 2
    possible duplicate of [Remove empty array elements](http://stackoverflow.com/questions/3654295/remove-empty-array-elements) and others: [http://stackoverflow.com/search?q=remove+empty+vaues+array+php](http://stackoverflow.com/search?q=remove+empty+vaues+array+php). Please use the search function before asking. – Gordon Apr 20 '11 at 08:02

3 Answers3

4

I'd try to reduce effort.

foreach ($this->CsInventory as $value) 
    { 
        foreach ($value as $key => $item)
        {
             $value[] = $item;
             $rdata .= "<td>$item</td>";

        }

       print_r($value)                  
    } 

As a general comment, not sure why you're adding anonomous values back to the $values stack, might be better to use a different array.

trickwallett
  • 2,418
  • 16
  • 15
1

If you have specific array elements you want to get rid of, you can use unset($array[$key]);

You could also prevent them getting into the array in the first place by using

if($value['VesselName']) {$vname = $value[] = $value['VesselName'];}

instead of simply

$vname = $value[] = $value['VesselName'];
Spudley
  • 166,037
  • 39
  • 233
  • 307
0
function array_remove_empty($arr){
    $narr = array();
    while(list($key, $val) = each($arr)){
        if (is_array($val)){
            $val = array_remove_empty($val);
            // does the result array contain anything?
            if (count($val)!=0){
                // yes :-)
                $narr[$key] = $val;
            }
        }
        else {
            if (trim($val) != ""){
                $narr[$key] = $val;
            }
        }
    }
    unset($arr);
    return $narr;
}

array_remove_empty(array(1,2,3, '', array(), 4)) => returns array(1,2,3,4)
Mr. Black
  • 11,692
  • 13
  • 60
  • 85