wordpress 按深度子数组列值筛选多维数组

uyto3xhc  于 9个月前  发布在  WordPress
关注(0)|答案(1)|浏览(154)

我找到了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进行排序,并使它们唯一(因为我只需要一次):

<?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()->cart->get_cart()方法我可以得到整个数组。我需要某种过滤器来删除数组中$date_value不等于[thwepof_options] => [value]的所有值(或者只显示那些相等的值),以创建另一个foreach循环。

mi7gmzs6

mi7gmzs61#

试试这个:

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

字符串
由于没有提供所有的代码,因此并不完全清楚您从哪里获取所有的值,但我希望这成功地演示了一种过滤数组的简单方法。

相关问题