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 MapReduce实现倒排索引失败
  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题