[Flow] array_find_deep Utility

Regine Rosewich regine.rosewich at hoellenberg.biz
Thu Jun 12 11:59:44 CEST 2014


Hi,

would it be possible to add the following function to the Utility->Arrays
Class of TYPO3.Flow?

Regine

/**

 * Searches a value in an array no matter if the array consists of
associated arrays

 * and no matter how deep the associations are structured

 * 

 * Example:

 * $a = array(

         * 'key1' => array(

         *   'key2' => array(

      *      'key3' => 'value',

      *      'key4' => array(

      *          'key5' => 'value2'

      *       )

      *    )

      *  ));

 *

 * var_dump(array_find_deep($a, 'value'));

 * array

 *   0 => string 'key1' (length=4)

 *   1 => string 'key2' (length=4)

 *   2 => string 'key3' (length=4)

 *

 * var_dump(array_find_deep($a, 'value2'));

 * array

 *   0 => string 'key1' (length=4)

 *   1 => string 'key2' (length=4)

 *   2 => string 'key4' (length=4)

 *   3 => string 'key5' (length=4)

 * 

 * var_dump(array_find_deep($a, 'value3'));

 * array

 *   empty

 *  

 * @param mixed $needle the value to search for

 * @param array $haystack the array potentially consisting of a structure of
associated arrays

 * @param array $keys

 * 

 * @return array an array with informations where in the array structure the
value(s) have been found

 */

static public function array_find_deep($needle, array $haystack, $keys =
array()) {

if (count($haystack) > 0) {

    foreach($haystack as $key => $value) {

        if (is_array($value)) {

            $sub = self::array_find_deep($needle, $value, array_merge($keys,
array($key)));

            if (count($sub)) {

                return $sub;

            }

        } elseif ($value === $needle) {

            return array_merge($keys, array($key));

        }

    }

}

    return array();

}







More information about the Flow mailing list