dongnuoyi8833 2018-01-29 00:25
浏览 55
已采纳

PHP多维数组过滤器

I found this manual page but I'm still not sure what to use. I have this array:

Array (
[unique_key] => 7439db65fe2856a636e3c6d9841b51ed
[thwepof_options] => Array (
[order_date] => Array (
[name] => order_date
[value] => 29-01-2018, monday
[label] => Order date [options] => ) )

So first I just foreach every [thwepof_options] => value, sorted and made them unique (because I need that once):

<?php foreach (WC()->cart->get_cart() as $order_dates => $order_date): ?>
    <?php $options = $order_date['thwepof_options']; ?>
        <?php foreach($options as $option => $dates): ?>
            <?php array_push($myarray, $dates['value']) ?>
        <?php endforeach; ?>
<?php endforeach; ?>
<?php sort($myarray); ?>
<?php $unique = array_unique($myarray); ?>
//later in that code
<?php foreach($unique as $dates => $date_value): ?>
            <tr>
                <h1><?php echo $date_value ?></h1></td>
            </tr>
<?php endforeach; ?>

With WC()->cart->get_cart() method I can get the whole array. I need some kind of filter which removes every value from that array where $date_value isn't equal with the [thwepof_options] => [value] (or show only those where is equal) to make an another foreach loop.

  • 写回答

1条回答 默认 最新

  • doudao8283 2018-01-29 01:00
    关注

    Try this:

    array_filter(
            $unique, // Array to filter
            function($elem){ // Closure performing filtration
                // When this returns FALSE, items will be removed
                return $elem['order_date']['value'] == $date_value;
            }
        );
    

    It is not entirely clear where you are sourcing all of your values because not all code is provided, but I hope this successfully demonstrates an easy way to filter your array.

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

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题