dongyi8795 2018-10-11 00:31
浏览 97
已采纳

如何实现array_filter()以使其更清晰

I need a little help in the PHP, because I'm trying to use the array_filter() to prevent to many lines and make it simply and clear, but I'm struggling in this one, even if I'm reading this documentation: Array_Filter so I need a correct way to implement, because sometimes the getCategoryTree() comes empty or with null values, which I'm trying to not show it, I thought to create other function to show only specific things without category, but I feel that it is not efficient:

What I'm trying is to populate from the url www.foobar.com/checkout/payments, and this is the original code:

$page['eData'] = [
    'codeMoneyFormatter' => $this->moneyFormatter->getcodeMoneyFormatter(),
    'checkout' => [
        'actionField' => ['step' => 3, 'option' => 'Review Order'],
        // ---------------- THIS ONE MUST REFACTOR -------
        'products' => $this->getCartFromOrder($order),
    ],
];

// --------------------- REFACTOR ------------------
$itemData['category'] = $category ? $this->getCategoryTree($category->getId()) : '';
// -------------------------------------------------

This function is where it shows the category, but I don't like it because at all, because in some pages it comes empty/null which it's not correct, so I want to use the array_filter()

// --------------------- ARRAY_FILTER ------------------
$itemData['category'] = array_filter($category ? $this->getCategoryTree($category->getId()) : '');
// -------------------------------------------------

The function that I created to show only specific thing, it's correct because it does not show the category, but I feel that it's unnecessary because its repeating the same from the original one:

$page['eData'] = [
    'codeMoneyFormatter' => $this->moneyFormatter->getcodeMoneyFormatter(),
    'checkout' => [
        'actionField' => ['step' => 3, 'option' => 'Review Order'],
        'products' => $this->getCheckoutFromOrder($order),
    ],
];

It's repeating almost the same functions and it's not the correct standard of DRY (Don't repeat yourself) that's why I thought to implement the array_filter() but how???

  • 写回答

1条回答 默认 最新

  • dqyhj2014 2018-10-11 00:38
    关注

    Array_Filter so I need a correct way to implement, because sometimes the getCategoryTree() comes empty or with null values

    Easy, don't change types and feed array filter something it shouldn't eat.

     array_filter($category ? $this->getCategoryTree($category->getId()) : '');
    

    Should be (something like)

     $data = $category ? $this->getCategoryTree($category->getId()) : [];
     if(!is_array($data)) $data = [];
    
     array_filter($data);
    

    You can probably do it simpler then this. But, type changes in PHP can be troublesome because the language is loosely typed and won't complain to much.

    This can be easly tested

    var_dump(array_filter(''));
    

    output

    <br />
    <b>Warning</b>:  array_filter() expects parameter 1 to be array, string given in <b>[...][...]</b> on line <b>3</b><br />
    NULL
    

    Sandbox

    This on the other hand

     var_dump(array_filter([]));
    

    Simply returns an empty array.

    The last thing I will say is about 80% of the code you put in the question was unnecessary to answer the question and just serves to confuse other users.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要