-1

I am trying to delete an array object based on some criteria found within it's data.

For example, I'd like to delete any "sub" array within the larger array which has no value at the [0] key.

Using the sample array below, the first five (0-4) nested "sub" arrays are good to stay, but "sub" arrays 5-11 ought to be deleted (or "removed" or "unset") because there is nothing the in [0] position.

Thanks in advance for any wisdom.

NOTE: I would like even better to not build a "sub" array if it's missing values, but my data source is really not great.

Array ( [0] => Array ( [0] => Raffle [1] => Pick of Live [2] => [3] => 100 [4] => 150 [5] => Cocktail Only [6] => Struggled a bit, offered some from the stage to hit the minimum )

[1] => Array
    (
        [0] => Raffle
        [1] => Ticket/Tangible
        [2] => $1000 gift certificate to Canlis
        [3] => 25
        [4] => Unlimited
        [5] => Cocktail Only
        [6] => Really don't know how this did, but the box was pretty full of tickets
    )

[2] => Array
    (
        [0] => Game
        [1] => Ticket/Tangible
        [2] => The Horse Race. Everyone is a winner. Receive a gift certificate ranging from $25 to $99.
        [3] => 25
        [4] => Unlimited
        [5] => Cocktail Only
        [6] => Great participation
    )

[3] => Array
    (
        [0] => Other
        [1] => Mystery/ Blind Pull
        [2] => Wine Pull
        [3] => 25
        [4] => 50
        [5] => cocktail only
        [6] => There was about a dozen or so left
    )

[4] => Array
    (
        [0] => Other
        [1] => Ticket/Tangible
        [2] => Centerpiece sales
        [3] => 25
        [4] => 35
        [5] => Pre & During
        [6] => Unknown
    )

[5] => Array
    (
        [0] => 
        [1] => 
    )

[6] => Array
    (
        [0] => 
        [1] => 
    )

[7] => Array
    (
        [0] => 
        [1] => 
    )

[8] => Array
    (
        [0] => 
        [1] => 
    )

[9] => Array
    (
        [0] => 
        [1] => 
    )

[10] => Array
    (
        [0] => 
        [1] => 
    )

[11] => Array
    (
        [0] => 
        [1] => 
    )

)`

  • Please provide the code that you used to try and solve this. – geeves Oct 05 '16 at 22:13
  • An array is not an object. PHP arrays are not multi-dimensional. If unless you can only read the data as a serialized entity then the place to filter the data is at the point of reading it (i.e. in the database if its coming out of a dbms). Your criteria for exclusion are imprecise - "nothing" might mean an non-existent key, null, zero, false, a blank string. You've shown a (curious) input dataset, but not the expected output. – symcbean Oct 05 '16 at 22:39

3 Answers3

1

You could user array_filter to filter your input array by a custom callback function.

$filtered = array_filter($input, function($value){
    // return true iff this sub-array has an not-falsy value at index 0
    return !empty($value[0])
});
simon.ro
  • 2,984
  • 2
  • 22
  • 36
  • just `!empty` should suffice, no need for `isset` – Kevin Oct 05 '16 at 23:17
  • the `isset()`is needed. If `$value` has no index 0, `empty()` will give you a `Notice: Undefined index` – simon.ro Oct 07 '16 at 08:22
  • 1
    you're kidding right? https://3v4l.org/kGIGu i think you should read these, should shed some light http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty and http://kunststube.net/isset/ – Kevin Oct 09 '16 at 22:05
  • you're right. guess i never really understood `empty()` then... i'll edit my answer – simon.ro Oct 10 '16 at 10:45
1

USE array_filter with call back function

$arr=array( 
    array
    (
        '0' => 'Raffle',
        '1' => 'Ticket/Tangible',
        '2' => '$1000 gift certificate to Canlis',
        '3' => '25',
        '4' => 'Unlimited',
        '5' => 'Cocktail Only',
        '6' => "Really don't know how this did, but the box was pretty full of tickets"
    ),
    array
    (
        '0' => 'Raffle',
        '1' => 'Ticket/Tangible',
        '2' => '$1000 gift certificate to Canlis',
        '3' => '25',
        '4' => 'Unlimited',
        '5' => 'Cocktail Only',
        '6' => "Really don't know how this did, but the box was pretty full of tickets"
    ),
    array
    (
        '0' => 'Raffle',
        '1' => 'Ticket/Tangible',
        '2' => '$1000 gift certificate to Canlis',
        '3' => '25',
        '4' => 'Unlimited',
        '5' => 'Cocktail Only',
        '6' => "Really don't know how this did, but the box was pretty full of tickets"
    ),
    array
    (
        '0' => 'Raffle',
        '1' => 'Ticket/Tangible',
        '2' => '$1000 gift certificate to Canlis',
        '3' => '25',
        '4' => 'Unlimited',
        '5' => 'Cocktail Only',
        '6' => "Really don't know how this did, but the box was pretty full of tickets"
    ),
    array
    (
        '0' => 'Raffle',
        '1' => 'Ticket/Tangible',
        '2' => '$1000 gift certificate to Canlis',
        '3' => '25',
        '4' => 'Unlimited',
        '5' => 'Cocktail Only',
        '6' => "Really don't know how this did, but the box was pretty full of tickets"
    ),


    array
        (
            '0'=>'',
            '1'=>'' 
        ),
    array
        (
            0=>'' ,
            1 =>'' 
        )



);
    function check($var){
       return(strlen($var[0])!==0);
   }
    print_r(array_filter($arr, "check"));

Working fiddle http://phpfiddle.org/main/code/fujz-s84c

sumit
  • 15,003
  • 12
  • 69
  • 110
0

I was able to get it to work by doing the below:

    foreach ($array as $key => $value) {
      if ($value[0]== "") {
        unset($array[$key]);
      }
    }