如果children值为空,则删除包括children的parent数组

时间:2019-05-22 08:28:23

标签: php laravel

我有这个数组结构。

如果我发现[empevalpptwo]为空或为null,则尝试删除整个数组,因此,如果[empevalpptwo]为空,则应删除整个[4] => Array

现在我只是想获取父索引,所以我可以按索引将其删除

是否有任何好的解决方案,例如递归过滤?

$kra = array_column($ppform_plan->toArray(), 'kra');
$emp_eval_pptwo = array_search('', array_column($kra, 'empevalpptwo'));
//should return 4


Array
(
    [4] => Array
        (
            [id] => 50
            [user_id] => 6282
            [specific_user_id] => 6281
            [eval_cat_id] => 2
            [format_cat] => 1
            [title] => This istesting
            [desc] => 
            [weight] => 50
            [bsc_weight_group] => 
            [bsc_rating] => 
            [sequence] => 
            [created_at] => 2019-05-22 10:55:23
            [updated_at] => 2019-05-22 10:55:23
            [kra] => Array
                (
                    [0] => Array
                        (
                            [id] => 77
                            [user_id] => 6282
                            [bsc_id] => 50
                            [index] => 0
                            [kra_title] => ttes lang muna
                            [kra_desc] => 
                            [kra_weight] => 25
                            [sat] => 521
                            [at] => 4
                            [ot] => 535
                            [rating_per_kra] => 
                            [rating_per_kra_cat] => 
                            [net_weighting] => 5
                            [rank] => 1
                            [remarks] => 
                            [indicator_text] => 
                            [created_at] => 
                            [updated_at] => 
                            [empevalpptwo] => 
                        )

                    [1] => Array
                        (
                            [id] => 78
                            [user_id] => 6282
                            [bsc_id] => 50
                            [index] => 1
                            [kra_title] => talga e2 pa o
                            [kra_desc] => 
                            [kra_weight] => 25
                            [sat] => 5
                            [at] => 2
                            [ot] => 4
                            [rating_per_kra] => 
                            [rating_per_kra_cat] => 
                            [net_weighting] => 4
                            [rank] => 2
                            [remarks] => 
                            [indicator_text] => 
                            [created_at] => 

                        [updated_at] => 
                        [empevalpptwo] => 
                    )
            )
    )
)

2 个答案:

答案 0 :(得分:0)

类似于:

<?php

$items =
[
    1 =>
    [
        'foo'=>
        [
            [
                'bar' => '',
            ],
            [
                'bar' => '',
            ]
        ]
    ],
    2 =>
    [
        'foo'=>
        [
            [
                'bar' => 'baz'
            ]
        ]
    ]
];

foreach ($items as $k => $item)
    if(empty(array_filter(array_column($item['foo'], 'bar'))))
        unset($items[$k]);

var_export($items);

输出:

array (
  2 => 
  array (
    'foo' => 
    array (
      0 => 
      array (
        'bar' => 'baz',
      ),
    ),
  ),
)

答案 1 :(得分:0)

您应该尝试以下方法:

$data = array_map('array_filter', $data);
$data = array_filter($data);

已更新

并使用以下命令检查内部值:

$array = array_filter($array, function($v) { return !empty($v['empevalpptwo']); });

原始

Array
(
    [con] => Array
        (
            [address] => Array
                (
                    [city] => ahmedabad
                )

            [personalinfo] => 
        )

)

结果:

Array
(
    [con] => Array
        (
            [address] => Array
                (
                    [city] => ahmedabad
                )

        )

)