0

我在操作和数组来过滤一些数据时遇到了麻烦。这是数组的一个示例:

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它是否为空白,并且我还想清理日期以使其看起来更好。有谁知道我怎么能做到这一点?它几乎是循环数组并复制它们的数组,但修改某些元素。谢谢!

4

3 回答 3

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 */ }
});
于 2013-06-14T23:01:46.577 回答
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>";
于 2013-06-14T23:00:36.807 回答
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']));

       }
    }
}
于 2013-06-14T23:02:40.470 回答