0

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!

3 Answers 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 */ }
});
Sign up to request clarification or add additional context in comments.

1 Comment

me too!! Though for google visitors, you have to pass $val as reference in order to change the values of your array. Like so: function(&$val, $key)
0

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

Comments

0

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

       }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.