annotate vendor/dflydev/dot-access-data/src/Dflydev/DotAccessData/Util.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@17 1 <?php
Chris@17 2
Chris@17 3 /*
Chris@17 4 * This file is a part of dflydev/dot-access-data.
Chris@17 5 *
Chris@17 6 * (c) Dragonfly Development Inc.
Chris@17 7 *
Chris@17 8 * For the full copyright and license information, please view the LICENSE
Chris@17 9 * file that was distributed with this source code.
Chris@17 10 */
Chris@17 11
Chris@17 12 namespace Dflydev\DotAccessData;
Chris@17 13
Chris@17 14 class Util
Chris@17 15 {
Chris@17 16 /**
Chris@17 17 * Test if array is an associative array
Chris@17 18 *
Chris@17 19 * Note that this function will return true if an array is empty. Meaning
Chris@17 20 * empty arrays will be treated as if they are associative arrays.
Chris@17 21 *
Chris@17 22 * @param array $arr
Chris@17 23 *
Chris@17 24 * @return boolean
Chris@17 25 */
Chris@17 26 public static function isAssoc(array $arr)
Chris@17 27 {
Chris@17 28 return (is_array($arr) && (!count($arr) || count(array_filter(array_keys($arr),'is_string')) == count($arr)));
Chris@17 29 }
Chris@17 30
Chris@17 31 /**
Chris@17 32 * Merge contents from one associtative array to another
Chris@17 33 *
Chris@17 34 * @param array $to
Chris@17 35 * @param array $from
Chris@17 36 * @param bool $clobber
Chris@17 37 */
Chris@17 38 public static function mergeAssocArray($to, $from, $clobber = true)
Chris@17 39 {
Chris@17 40 if ( is_array($from) ) {
Chris@17 41 foreach ($from as $k => $v) {
Chris@17 42 if (!isset($to[$k])) {
Chris@17 43 $to[$k] = $v;
Chris@17 44 } else {
Chris@17 45 $to[$k] = self::mergeAssocArray($to[$k], $v, $clobber);
Chris@17 46 }
Chris@17 47 }
Chris@17 48
Chris@17 49 return $to;
Chris@17 50 }
Chris@17 51
Chris@17 52 return $clobber ? $from : $to;
Chris@17 53 }
Chris@17 54 }