質問

I'm having trouble manipulating and array to filter some data. Here's an example of the array:

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

I know I need to use nested foreach loops, but the difficult part is that I wants to parse name to see if it's blank and I also want to clean up the dates to make it look nicer. Anyone know how I could accomplish this? It's pretty much looping the array and copying they array, but modifying certain elements. Thanks!

役に立ちましたか?

解決

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 */ }
});

他のヒント

Why not try this:

$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>";

Try doing it like this...

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']));

       }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top