Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Our Test Array - perhaps form-submitted data?
- $array = array('phone' => array('zip' => array(array('value' => 0,
- 'data' => 'integer',
- 'type' => 'text')),
- 'prefix' => array(array('value' => 123,
- 'data' => 'integer',
- 'type' => 'text')),
- 'last4' => array('newkey' => array( // Added 'newkey' just for testing...
- array('value' => 4567,
- 'data' => 'integer',
- 'type' => 'text')))),
- 'first_name' => array('value' => 'John',
- 'data' => 'string',
- 'type' => 'text'),
- 'last_name' => array('value' => 'Doe',
- 'data' => 'string',
- 'type' => 'text'),
- 'primary' => array('value' => 1,
- 'data' => 'bool',
- 'type' => 'checkbox'));
- /**
- * Flatten and Group Keys
- *
- * This iterates over an array (intended for multi-dimensional arrays) and returns an array
- * containing all of the keys of the array, grouped together.
- *
- * The purpose is for form fields with array NAME attributes and validating them while singling
- * out a particular form field as an invalid or valid field.
- *
- * This is part of my {@link https://github.com/amereservant/PHP-Form-Creator-Validator-Emailer-Class}
- * project and the major rewrite I'm currently working on...
- */
- function flatten_keys($array)
- {
- $cur = Array() ;
- if (is_array($array))
- {
- if (isset($array['value']))
- return null ;
- else
- {
- foreach ($array as $k => $v)
- {
- $fk = flatten_keys($v) ;
- if ($fk === null)
- $cur[] = $k ;
- else if (is_array($fk))
- foreach ($fk as $fk_k => $fk_v)
- $cur[] = array_merge(array($k), is_array($fk_v) ? $fk_v : array($fk_v)) ;
- }
- }
- }
- else
- return false ;
- return $cur ;
- }
- // Test the function ...
- echo '<pre>';
- print_r(flatten_keys($array)) ;
- echo '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement