1

I have the array $wynik below.

How can I delete all keys with empty [id]??

I would like to refer to specific elements.

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

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

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

Solution:

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]);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
damian
  • 55
  • 7
  • Refer [link](http://stackoverflow.com/questions/3654295/remove-empty-array-elements) for more details. – Vinay Aug 10 '12 at 07:32

3 Answers3

3
$filtered = array_filter($wynik, function ($v) {
  return !empty($v['id']);
});
Yoshi
  • 54,081
  • 14
  • 89
  • 103
  • How refer to specific elements in my array?? – damian Aug 10 '12 at 09:22
  • @damian Sorry, I don't understand your question. – Yoshi Aug 10 '12 at 09:36
  • I do update the database. For each element in the array I need to do an if statement. How? if (id = id && ilosc < ilosc ){} – damian Aug 10 '12 at 09:44
  • @damian you mean, after the array has been stripped of emtpy elements? – Yoshi Aug 10 '12 at 09:46
  • Can this be?: foreach( $wynik as $key => $value ) { //print_array($value); $ilosc = $wynik[$key]['ilosc']++; $id = $wynik[$key]['id']++; echo $ilosc .' - '. $id .'
    '; }
    – damian Aug 10 '12 at 09:53
  • @damian well, just traverse the now filtered array (e.g. `$filtered` from above) like you normally would (for example using `foreach`) and do whatever needs to be done with those values. – Yoshi Aug 10 '12 at 09:54
0
$result=array();
foreach($wynik as $key=>$value)
{
    if($value['id']!=NULL)
        $result=array_merge($result,array($key=>$value));
}
var_dump($result);
Wenhuang Lin
  • 214
  • 2
  • 8
0
foreach( $wynik as $key => $value ) {
    if( is_array( $value ) ) {
        foreach( $value as $key2 => $value2 ) if(!empty($value2)) $Row[$key2] = $value2; 
    }
}
RDK
  • 4,540
  • 2
  • 20
  • 29