我知道这个问题已经问过无数次了,我已经搜索了足够多的结果,但到目前为止还没有成功。
假设我有这样一个数组类型:
1 ~~~
1 ###
Array ( [macro_ratio] => 1.1 [foodCategoryId] => 37 ) @@@
2 ###
Array ( [macro_ratio] => 1.2 [foodCategoryId] => 2 ) @@@
3 ###
Array ( [macro_ratio] => 1.3 [foodCategoryId] => 3 ) @@@
4 ###
Array ( [macro_ratio] => [foodCategoryId] => 4 ) @@@
5 ###
Array ( [macro_ratio] => [foodCategoryId] => 5 ) @@@
6 ###
Array ( [macro_ratio] => [foodCategoryId] => 6 ) @@@
7 ###
Array ( [macro_ratio] => [foodCategoryId] => 7 ) @@@
8 ###
Array ( [macro_ratio] => [foodCategoryId] => 8 ) @@@
9 ###
Array ( [macro_ratio] => [foodCategoryId] => 9 ) @@@
10 ###
Array ( [macro_ratio] => [foodCategoryId] => 38 ) @@@
11 ###
Array ( [macro_ratio] => [foodCategoryId] => 10 ) @@@
12 ###
Array ( [macro_ratio] => [foodCategoryId] => 11 ) @@@
13 ###
Array ( [macro_ratio] => [foodCategoryId] => 12 ) @@@
14 ###
Array ( [macro_ratio] => [foodCategoryId] => 13 ) @@@
15 ###
Array ( [macro_ratio] => [foodCategoryId] => 14 ) @@@
16 ###
Array ( [macro_ratio] => [foodCategoryId] => 15 ) @@@
17 ###
Array ( [macro_ratio] => [foodCategoryId] => 16 ) @@@
18 ###
Array ( [macro_ratio] => [foodCategoryId] => 17 ) @@@
19 ###
Array ( [macro_ratio] => [foodCategoryId] => 39 ) @@@
20 ###
Array ( [macro_ratio] => [foodCategoryId] => 18 ) @@@
21 ###
Array ( [macro_ratio] => [foodCategoryId] => 19 ) @@@
22 ###
Array ( [macro_ratio] => [foodCategoryId] => 20 ) @@@
23 ###
Array ( [macro_ratio] => [foodCategoryId] => 21 ) @@@
24 ###
Array ( [macro_ratio] => [foodCategoryId] => 22 ) @@@
25 ###
Array ( [macro_ratio] => [foodCategoryId] => 23 ) @@@
26 ###
Array ( [macro_ratio] => [foodCategoryId] => 24 ) @@@
27 ###
Array ( [macro_ratio] => [foodCategoryId] => 25 ) @@@
28 ###
Array ( [macro_ratio] => [foodCategoryId] => 40 ) @@@
29 ###
Array ( [macro_ratio] => [foodCategoryId] => 41 ) @@@
30 ###
Array ( [macro_ratio] => [foodCategoryId] => 26 ) @@@
31 ###
Array ( [macro_ratio] => [foodCategoryId] => 27 ) @@@
32 ###
Array ( [macro_ratio] => [foodCategoryId] => 28 ) @@@
33 ###
Array ( [macro_ratio] => [foodCategoryId] => 29 ) @@@
34 ###
Array ( [macro_ratio] => [foodCategoryId] => 30 ) @@@
35 ###
Array ( [macro_ratio] => [foodCategoryId] => 34 ) @@@
36 ###
Array ( [macro_ratio] => [foodCategoryId] => 35 ) @@@
37 ###
Array ( [macro_ratio] => [foodCategoryId] => 36 ) @@@
2 ~~~
1 ###
Array ( [macro_ratio] => 2.1 [foodCategoryId] => 37 ) @@@
2 ###
Array ( [macro_ratio] => 2.2 [foodCategoryId] => 2 ) @@@
3 ###
Array ( [macro_ratio] => 2.3 [foodCategoryId] => 3 ) @@@
4 ###
Array ( [macro_ratio] => [foodCategoryId] => 4 ) @@@
5 ###
Array ( [macro_ratio] => [foodCategoryId] => 5 ) @@@
6 ###
Array ( [macro_ratio] => [foodCategoryId] => 6 ) @@@
7 ###
Array ( [macro_ratio] => [foodCategoryId] => 7 ) @@@
8 ###
Array ( [macro_ratio] => [foodCategoryId] => 8 ) @@@
9 ###
Array ( [macro_ratio] => [foodCategoryId] => 9 ) @@@
10 ###
Array ( [macro_ratio] => [foodCategoryId] => 38 ) @@@
11 ###
Array ( [macro_ratio] => [foodCategoryId] => 10 ) @@@
12 ###
Array ( [macro_ratio] => [foodCategoryId] => 11 ) @@@
13 ###
Array ( [macro_ratio] => [foodCategoryId] => 12 ) @@@
14 ###
Array ( [macro_ratio] => [foodCategoryId] => 13 ) @@@
15 ###
Array ( [macro_ratio] => [foodCategoryId] => 14 ) @@@
16 ###
Array ( [macro_ratio] => [foodCategoryId] => 15 ) @@@
17 ###
Array ( [macro_ratio] => [foodCategoryId] => 16 ) @@@
18 ###
Array ( [macro_ratio] => [foodCategoryId] => 17 ) @@@
19 ###
Array ( [macro_ratio] => [foodCategoryId] => 39 ) @@@
20 ###
Array ( [macro_ratio] => [foodCategoryId] => 18 ) @@@
21 ###
Array ( [macro_ratio] => [foodCategoryId] => 19 ) @@@
22 ###
Array ( [macro_ratio] => [foodCategoryId] => 20 ) @@@
23 ###
Array ( [macro_ratio] => [foodCategoryId] => 21 ) @@@
24 ###
Array ( [macro_ratio] => [foodCategoryId] => 22 ) @@@
25 ###
Array ( [macro_ratio] => [foodCategoryId] => 23 ) @@@
26 ###
Array ( [macro_ratio] => [foodCategoryId] => 24 ) @@@
27 ###
Array ( [macro_ratio] => [foodCategoryId] => 25 ) @@@
28 ###
Array ( [macro_ratio] => [foodCategoryId] => 40 ) @@@
29 ###
Array ( [macro_ratio] => [foodCategoryId] => 41 ) @@@
30 ###
Array ( [macro_ratio] => [foodCategoryId] => 26 ) @@@
31 ###
Array ( [macro_ratio] => [foodCategoryId] => 27 ) @@@
32 ###
Array ( [macro_ratio] => [foodCategoryId] => 28 ) @@@
33 ###
Array ( [macro_ratio] => [foodCategoryId] => 29 ) @@@
34 ###
Array ( [macro_ratio] => [foodCategoryId] => 30 ) @@@
35 ###
Array ( [macro_ratio] => [foodCategoryId] => 34 ) @@@
36 ###
Array ( [macro_ratio] => [foodCategoryId] => 35 ) @@@
37 ###
Array ( [macro_ratio] => [foodCategoryId] => 36 ) @@@
我想从这个数组中省略/跳过Array ( [macro_ratio] => [foodCategoryId] => 36 ) @@@
sort of elements的意思是如果[macro_ratio] =>
为空,那么我想跳过数组的整个元素
我正在做的代码是:
foreach ($_POST['mealData'] as $key => $value) {
print_r($key);
echo " ~~~<br>";
foreach ($value as $key => $value) {
print_r($key);
echo " ###<br>";
print_r($value);
echo " @@@<br>";
if (isset($value['macro_ratio']) === true && empty($value['macro_ratio']) === true && isset($value['foodCategoryId']) === true && empty($value['foodCategoryId']) === true) {
print_r($value);
}
}
}
如果可以的话试试
foreach ($_POST['mealData'] as $key => $value) {
print_r($key);
echo " ~~~<br>";
if(!empty($value['macro_ratio'])){
foreach($value as $key2 => $value2) {
print_r($value2);
}
}
}
macro_ratio和foodCategoryId都是键,您可以在第一个循环后访问它们。if语句将检查macro_ratio是否设置且不为空。修改你的代码,它应该工作
$data = array( // first loop to access this level
array( // second loop to access this level,
'macro_ratio' => 1.2 // check if index macro_ratio is set and not empty
'foodCategoryId' => 2 ),
array(
'macro_ratio' => 2
'foodCategoryId' => 4 )
)
foreach ($_POST['mealData'] as $k1 => $v1) {
foreach ($value as $k2 => $v2) {
check if index macro_ratio is set and not empty
if ( isset($value2['macro_ratio']) && !empty($value2['macro_ratio']) ) {
echo $value2['foodCategoryId'];
}
}
}
希望这有效…这是最简单的方法。
<?php
$result = array();
foreach ($_POST['mealData'] as $key => $value){
if( empty($value["macro_ratio"]) )
continue;
else
$result[$key] = $value;
}
var_dump($result);
这是另一种方法。在foreach循环之前过滤数组。array_filter和一个自定义回调可以做到这一点。
这将通过检查数组中的每个元素来返回一个经过过滤的数组,以确保['macro_ratio']
的长度大于0。使用strlen而不是empty仍然允许值为0。
$filteredArray = array_filter($array, function($value) {
return (strlen($value['macro_ratio']));
});