删除null元素数组

时间:2012-08-10 07:24:27

标签: php arrays search

我有数组$wynik

如何用空[id]删除所有键? 我想参考具体的元素??

Array
(
    [0] => Array
        (
            [id] => 2531291225
            [ilosc] => 20
        )

    [1] => Array
        (
            [id] => 2531291312
            [ilosc] => 10
        )

    [2] => Array
        (
            [id] => 
            [ilosc] => 
        )

)

解决方案:

foreach( $wynik as $key => $value ) {
    if( is_array( $value ) ) {
        foreach( $value as $key2 => $value2 ) {
            if( empty( $value2 ) ) 
                unset( $wynik[ $key ][ $key2 ] );
        }
    }
    if( empty( $wynik[ $key ] ) )
        unset( $wynik[ $key ] );
}

3 个答案:

答案 0 :(得分:3)

$filtered = array_filter($wynik, function ($v) {
  return !empty($v['id']);
});

答案 1 :(得分:0)

$result=array();
foreach($wynik as $key=>$value)
{
    if($value['id']!=NULL)
        $result=array_merge($result,array($key=>$value));
}
var_dump($result);

答案 2 :(得分:0)

foreach( $wynik as $key => $value ) {
    if( is_array( $value ) ) {
        foreach( $value as $key2 => $value2 ) if(!empty($value2)) $Row[$key2] = $value2; 
    }
}