Advertisement
amereservant

Recursively-Grouped Array Keys Multi-Dimensional Array

Jul 4th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <?php
  2. // Our Test Array - perhaps form-submitted data?
  3. $array = array('phone' => array('zip'       => array(array('value' => 0,
  4.                                                            'data' => 'integer',
  5.                                                            'type' => 'text')),
  6.                                 'prefix'    => array(array('value' => 123,
  7.                                                            'data' => 'integer',
  8.                                                            'type' => 'text')),
  9.                                 'last4'     => array('newkey' => array(     // Added 'newkey' just for testing...
  10.                             array('value' => 4567,
  11.                                                            'data' => 'integer',
  12.                                                            'type' => 'text')))),
  13.                 'first_name' => array('value' => 'John',
  14.                                       'data' => 'string',
  15.                                       'type' => 'text'),
  16.                 'last_name'  => array('value' => 'Doe',
  17.                                       'data' => 'string',
  18.                                       'type' => 'text'),
  19.                 'primary'    => array('value' => 1,
  20.                                       'data' => 'bool',
  21.                                       'type' => 'checkbox'));
  22.  
  23. /**
  24.  * Flatten and Group Keys
  25.  *
  26.  * This iterates over an array (intended for multi-dimensional arrays) and returns an array
  27.  * containing all of the keys of the array, grouped together.
  28.  *
  29.  * The purpose is for form fields with array NAME attributes and validating them while singling
  30.  * out a particular form field as an invalid or valid field.
  31.  *
  32.  * This is part of my {@link https://github.com/amereservant/PHP-Form-Creator-Validator-Emailer-Class}
  33.  * project and the major rewrite I'm currently working on...
  34.  */
  35. function flatten_keys($array)
  36. {
  37.     $cur = Array() ;
  38.     if (is_array($array))
  39.     {
  40.         if (isset($array['value']))
  41.             return null ;
  42.         else
  43.         {
  44.             foreach ($array as $k => $v)
  45.             {
  46.                 $fk = flatten_keys($v) ;
  47.                 if ($fk === null)
  48.                     $cur[] = $k ;
  49.                 else if (is_array($fk))
  50.                     foreach ($fk as $fk_k => $fk_v)
  51.                         $cur[] = array_merge(array($k), is_array($fk_v) ? $fk_v : array($fk_v)) ;
  52.             }
  53.         }
  54.     }
  55.     else
  56.         return false ;
  57.  
  58.     return $cur ;
  59. }
  60.  
  61. // Test the function ...
  62. echo '<pre>';
  63. print_r(flatten_keys($array)) ;
  64. echo '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement