dongyi8795
dongyi8795
2018-10-11 00:31

如何实现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 dqyhj2014 3年前

    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.

    点赞 评论 复制链接分享

为你推荐