1

I have this multi-dimensional array and I want to delete completely if the [earnings] index are empty.

Array
(
    [0] => Array
    (
        [earnings] => 
        [other] => Array()
        [ord] => 2
        [days] => 1
        [total] => 1
    )

    [1] => Array
    (
        [earnings] => The campaign was effectively ended in November 1917.
        [other] => Array
        (
            [campaign] => 1
            [novemb] => 1
            [today] => 1
        )
        [ord] => 1
        [days] => 8
        [total] => 1
    )
)

I like to output something like this:

[1] => Array
(
    [earnings] => The campaign was effectively ended in November 1917.
    [other] => Array
    (
        [campaign] => 1
        [novemb] => 1
        [today] => 1
    )
    [ord] => 1
    [days] => 8
    [total] => 1
)

I tried this but not doesn't work quite well:

 foreach($array as $key=>$test){

 foreach($test as $koval=>$user) { 

      if( empty($user['earnings']) || !file_exists($staff['earnings'])) {
          unset($array[$key][$koval]); }}}

3 Answers 3

2
$array = array_filter($array,function($item) {
    return (!empty($item['earnings']));
});
Sign up to request clarification or add additional context in comments.

Comments

1

You're looping 1 too many times. What you want is this:

foreach($array as $i => $item) {
    if(empty($item['earnings'])) {
        unset($array[$i]);
    }
}

Example


You should look at FuzzyTree's answer below for a cleaner way of doing this.

Comments

0

What you are looking for is array_filter http://php.net/manual/en/function.array-filter.php

$arr = array(
    array(
        'earnings' => null,
        'other'    => array(),
        'ord'      => 2,
        'days'     => 1,
        'total'    => 1,
    ),
    array(
        'earnings' => 'The campaign was effectively ended in November 1917.',
        'other'    => array(
            'campaign' => 1,
            'novemb'   => 1,
            'today'    => 1,
        ),
        'ord'      => 1,
        'days'     => 8,
        'total'    => 1,
    ),
);

var_dump($arr);
//array(2) {
//    [0] =>
//    array(5) {
//        'earnings' =>
//        NULL
//        'other' =>
//        array(0) {
//        }
//        'ord' =>
//        int(2)
//        'days' =>
//        int(1)
//        'total' =>
//        int(1)
//    }
//    [1] =>
//    array(5) {
//        'earnings' =>
//        string(52) "The campaign was effectively ended in November 1917."
//        'other' =>
//        array(3) {
//            'campaign' =>
//            int(1)
//            'novemb' =>
//            int(1)
//            'today' =>
//            int(1)
//        }
//        'ord' =>
//        int(1)
//        'days' =>
//        int(8)
//        'total' =>
//        int(1)
//    }
//}
$filtered = array_filter($arr, function ($val) {
    return isset($val['earnings']) && !empty($val['earnings']);
});
var_dump($filtered);
//array(1) {
//    [1] =>
//    array(5) {
//        'earnings' =>
//        string(52) "The campaign was effectively ended in November 1917."
//        'other' =>
//        array(3) {
//            'campaign' =>
//            int(1)
//            'novemb' =>
//            int(1)
//            'today' =>
//            int(1)
//        }
//        'ord' =>
//        int(1)
//        'days' =>
//        int(8)
//        'total' =>
//        int(1)
//    }
//}

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.