1

我有$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]);
}
4

3 回答 3

3
$filtered = array_filter($wynik, function ($v) {
  return !empty($v['id']);
});
于 2012-08-10T07:26:37.310 回答
0
foreach( $wynik as $key => $value ) {
    if( is_array( $value ) ) {
        foreach( $value as $key2 => $value2 ) if(!empty($value2)) $Row[$key2] = $value2; 
    }
}
于 2012-08-10T08:15:01.697 回答
0
$result=array();
foreach($wynik as $key=>$value)
{
    if($value['id']!=NULL)
        $result=array_merge($result,array($key=>$value));
}
var_dump($result);
于 2012-08-10T07:30:33.997 回答