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]);
}
1
  • Refer link for more details. Commented Aug 10, 2012 at 7:32

3 Answers 3

3
$filtered = array_filter($wynik, function ($v) {
  return !empty($v['id']);
});
Sign up to request clarification or add additional context in comments.

6 Comments

How refer to specific elements in my array??
@damian Sorry, I don't understand your question.
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 you mean, after the array has been stripped of emtpy elements?
Can this be?: foreach( $wynik as $key => $value ) { //print_array($value); $ilosc = $wynik[$key]['ilosc']++; $id = $wynik[$key]['id']++; echo $ilosc .' - '. $id .'<br>'; }
|
0
$result=array();
foreach($wynik as $key=>$value)
{
    if($value['id']!=NULL)
        $result=array_merge($result,array($key=>$value));
}
var_dump($result);

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.