【问题标题】:PHP multidimensional array filterPHP多维数组过滤器
【发布时间】:2018-07-07 15:38:22
【问题描述】:

我找到了this manual page,但我仍然不确定要使用什么。我有这个数组:

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

所以首先我只对每个[thwepof_options] => value 进行 foreach,对它们进行排序并使其独一无二(因为我需要一次):

<?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; ?>

使用WC()-&gt;cart-&gt;get_cart() 方法,我可以获得整个数组。我需要某种过滤器来删除该数组中$date_value 与[thwepof_options] =&gt; [value] 不相等的每个值(或仅显示那些相等的值)以创建另一个foreach 循环。

【问题讨论】:

    标签: php arrays wordpress function filter


    【解决方案1】:

    试试这个:

    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;
            }
        );
    

    由于并未提供所有代码,因此并不完全清楚所有值的来源,但我希望这成功展示了一种过滤数组的简单方法。

    【讨论】:

      猜你喜欢
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 2021-07-14
      • 1970-01-01
      相关资源
      最近更新 更多