2016-10-05 143 views
-1

我想删除一个数组对象基于它的数据中发现的一些条件。多维PHP阵列操作

例如,我想删除大数组中没有[0]键值的任何“子”数组。

使用下面的示例数组,前五个(0-4)嵌套的“子”数组可以保留,但“子”数组5-11应该被删除(或“删除”或“未设置”)因为[0]中没有任何位置。

在此先感谢您的任何智慧。

注意:如果缺少值,我想要更好地构建“子”数组,但我的数据源真的不是很好。

阵列 ( [0] =>数组 ( [0] =>抽奖 [1] =>现场 [2] => [3] => 100 [4] =的皮克> 150 [5] =>鸡尾酒只有 [6] =>有点挣扎,提供了一些从舞台打最小 )

[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] => 
    ) 

)`

+0

请提供代码,你用来尝试和解决这个问题。 – geeves

+0

数组不是一个对象。 PHP数组不是多维的。如果除非只能将数据作为序列化实体读取,那么过滤数据的地方就是读取数据的地方(即如果数据库从dbms中出来)。您排除的标准是不准确的 - “无”可能意味着不存在的关键字null,zero,false,一个空白字符串。你已经显示了一个(好奇的)输入数据集,但不是预期的输出。 – symcbean

回答

1

您可以使用用户array_filter通过自定义回调函数来过滤输入数组。

$filtered = array_filter($input, function($value){ 
    // return true iff this sub-array has an not-falsy value at index 0 
    return !empty($value[0]) 
}); 
+0

只需'!empty'就足够了,不需要'isset' – Ghost

+0

'isset()'是需要的。如果'$ value'没有索引0,'empty()'会给你一个'Notice:Undefined index' –

+1

你是在开玩笑吧? https://3v4l.org/kGIGu我认为你应该阅读这些,应该摆脱一些光http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty和http:// kunststube。 net/isset/ – Ghost

1

使用array_filter与回调函数

$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")); 

工作拨弄 http://phpfiddle.org/main/code/fujz-s84c

0

我能得到它做以下工作:

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