[英]PHP multidimensional array filter
我找到了此手冊頁,但是我仍然不確定該使用什么。 我有這個數組:
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]
不等於[thwepof_options] => [value]
(或僅顯示相等的$date_value
)的數組中刪除每個值,以進行另一個foreach
循環。
嘗試這個:
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;
}
);
目前尚不清楚在哪里獲取所有值,因為沒有提供所有代碼,但是我希望這能成功演示一種簡單的方法來過濾數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.