-1

I have an array value like this. I tried array_search and it is of no use. Just what I want is to filter only the array values that have status value.

Output

    Array
    (
    [1] => Array
    (
    [author] => Author1
    [book] => Book1
    [status] => 1
    )

    [2] => Array
    (
    [author] => Author2
    [book] => Book2
    )

    [3] => Array
    (
    [author] => Author3
    [book] => Book3
    [status] => 1
    )
    )

Expected Output

Array
    (
    [1] => Array
    (
    [author] => Author1
    [book] => Book1
    [status] => 1
    )

    [3] => Array
    (
    [author] => Author3
    [book] => Book3
    [status] => 1
    )
    )

I will be more happy if the expected output has the proper number sequence. In the above case there are two arrays with array number as [1] and [3] . If possible i need to make it as [1] and [2].

Any help will be very useful.

Thanks, Kimz

2 Answers 2

1

You can do like

foreach($my_array as $arr) {
    if(isset($arr['status']) && $arr['status'] != '') {
        $temp_array[] = $arr;
    }
}
print_r($temp_array);
1
  • Gautam - someone down voted. don't know why. could you upvote my question ;) please. i have marked your answer ;) Commented Aug 1, 2014 at 5:24
1

You could use array_filter

$array = array(['status' => 1], [], ['status' => 1]);

$result = array_filter($array, function($item)
    {
    return !empty($item['status']);
    });

var_dump($result);

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.