我无法操作和数组来过滤一些数据。这是一个数组的例子:
Array
(
[0] => Array
(
[0] => Array
(
[accounts] => Array
(
[name] =>
)
[accounts] => Array
(
[id] => 84352
[a_id] => 552
[customer_number] => 1593428
[password] => asdasdasdasd
[completed] =>
[created_date] => 2012-04-11 01:00:03.429465
[expiration_date] => 2012-10-11 09:45:12.100404
)
)
)
[1] => Array
(
[1] => Array
(
[users] => Array
(
[name] =>
)
[accounts] => Array
(
[id] => 106101
[a_id] => 574
[customer_number] => 429381
[password] => dsadasdsdad
[completed] =>
[created_date] => 2012-09-08 15:40:44.702644
[expiration_date] => 2012-09-22 00:00:00
)
)
)
.....many more
我知道我需要使用嵌套的foreach循环,但困难的部分是我想解析name
以查看它是否为空,我还想清理日期以使其看起来更好。谁知道我怎么能做到这一点?它几乎循环数组并复制它们的数组,但修改了某些元素。谢谢!
答案 0 :(得分:1)
array_walk_recursive($array, function($val, $key) {
if ($key == "name" && !$val) { /* name is blank, do something */ }
if ($key == "created_date") { /* do something */ }
if ($key == "expiration_date") { /* do something */ }
});
答案 1 :(得分:0)
为什么不试试这个:
$accounts = array();
foreach ($array as $a) {
foreach ($a as $b) {
if (isset($b['users']['name'])) {
if (empty($b['accounts']['name'])) {
//name is blank
}
}
if (isset($b['accounts']['created_date'])) {
$b['accounts']['created_date'] = date('d m Y', strtotime($b['accounts']['created_date']));
}
if (isset($b['accounts']['expiration_date'])) {
$b['accounts']['expiration_date'] = date('d m Y', strtotime($b['accounts']['created_date']));
}
$accounts[] = $b;
}
}
//display new array
echo "<pre>";
print_r($accounts);
echo "</pre>";
答案 2 :(得分:0)
尝试这样做......
foreach($array as $val){
foreach($val as $val2) {
if(empty($val2['users']['name'])){
echo "NAME IS EMPTY";
//DO WHATEVER HERE...
}
//Process your dates
$val2['accounts']['created_date'] = date('Y-m-d', strtotime($val2['accounts']['created_date']));
$val2['accounts']['created_date'] = date('Y-m-d', strtotime($val2['accounts']['expiration_date']));
}
}
}