我有一个大阵容 该数组的内容是:
Array
(
[0] => Array
(
[id] => 12
[user_id] => 1
[date] => 2013-10-21 23:01:52
[type] => 1
[quantity] => 0
[value] => 1700
)
[1] => Array
(
[id] => 13
[user_id] => 1
[date] => 2013-10-21 23:01:52
[type] => 0
[quantity] => 0
[value] => 90
)
[2] => Array
(
[id] => 16
[user_id] => 1
[date] => 2013-10-21 23:01:52
[type] => 0
[quantity] => 0
[value] => 0
[3] => Array
(
[id] => 19
[user_id] => 1
[date] => 2013-10-31 02:49:12
[type] => 0
[quantity] => 0
[value] => 0
[bills] => Array
(
[0] => Array
(
[id] => 5
[data_id] => 19
[quantity] => 10
[value] => 15
)
[1] => Array
(
[id] => 5
[data_id] => 19
[quantity] => 20
[value] => 1
)
[2] => Array
(
[id] => 5
[data_id] => 19
[quantity] => 1
[value] => 50
)
)
)
)
我想在foreach中显示这个数组。 所以我有:
echo '<ol>';
foreach ( $this->data as $d )
{
echo '<li><strong>'.$d['name'].'</strong><br /></li>';
if ( $d['bills'] )
{
echo '<ul>';
foreach ( $d['bills'] as $b )
{
echo '<li>';
echo $b['name'];
echo '</li>';
}
echo '</ul>';
}
}
echo '</ol>';
这很简单,直到我想只显示包含键['type'] == 1的数组。 我不知道怎么能这样做。
在MySQL中,它只需要添加'WHERE type = 1'。 我了解PHP数组很抱歉,如果过滤是简单的功能。 最好的欢呼!
答案 0 :(得分:2)
如果您确实需要创建已过滤的数组,则可以使用array_filter()
进行回调:
$filtered = array_filter($this->data, function($element) {
return $element['type'] == 1;
});
否则,最简单的解决方案可能只是将这样的内容放在foreach
循环的顶部:
if($d['type'] != 1) {
continue;
}
答案 1 :(得分:1)
您可以使用IF
语句与continue
一起查看type
是否等于1.如果不是,则可以跳过它。
foreach ( $this->data as $d )
{
if ($d['type'] != 1) continue;