public static function setBySelector($array, $selector, $value, $overwrite = false)
    {
        // make sure the array is an array in case we got here through a subsequent call
        // so arraySetElementBySelector(array(), 'item[subitem]', 'value');
        // will call arraySetElementBySelector(null, 'subitem', 'value');
        if (! is_array($array)) {
            $array = array();
        }
        list($container, $subselector) = self::getSelectorParts($selector);
        if (! $subselector) {
            if ($container !== '*') {
                if ($overwrite === true || ! array_key_exists($container, $array)) {
                    $array[$container] = $value;
                }
            }

            return $array;
        }

        // if we have a subselector the $array[$container] must be an array
        if ($container !== '*' && ! array_key_exists($container, $array)) {
            $array[$container] = array();
        }
        // we got here through something like *[subitem]
        if ($container === '*') {
            foreach ($array as $key => $v) {
                $array[$key] = self::setBySelector($array[$key], $subselector, $value, $overwrite);
            }
        } else {
            $array[$container] = self::setBySelector($array[$container], $subselector, $value, $overwrite);
        }

        return $array;
    }
public static function setBySelector($array, $selector, $value, $overwrite = false)
    {
        // make sure the array is an array in case we got here through a subsequent call
        // so arraySetElementBySelector(array(), 'item[subitem]', 'value');
        // will call arraySetElementBySelector(null, 'subitem', 'value');
        if (! is_array($array)) {
            $array = array();
        }
        list($container, $subselector) = self::getSelectorParts($selector);
        if (! $subselector) {
            if ($container !== '*') {
                if ($overwrite === true || ! array_key_exists($container, $array)) {
                    $array[$container] = $value;
                }
            }

            return $array;
        }

        // if we have a subselector the $array[$container] must be an array
        if ($container !== '*' && ! array_key_exists($container, $array)) {
            $array[$container] = array();
        }
        // we got here through something like *[subitem]
        if ($container === '*') {
            foreach ($array as $key => $v) {
                $array[$key] = self::setBySelector($array[$key], $subselector, $value, $overwrite);
            }
        } else {
            $array[$container] = self::setBySelector($array[$container], $subselector, $value, $overwrite);
        }

        return $array;
    }
public static function setBySelector ( $ array , $ selector , $ value , $ overwrite = false ) { // make sure the array is an array in case we got here through a subsequent call // so arraySetElementBySelector(array(), 'item[subitem]', 'value'); // will call arraySetElementBySelector(null, 'subitem', 'value'); if ( ! is_array ( $ array ) ) { $ array = array ( ) ; } list ( $ container , $ subselector ) = self :: getSelectorParts ( $ selector ) ; if ( ! $ subselector ) { if ( $ container !== '*' ) { if ( $ overwrite === true || ! array_key_exists ( $ container , $ array ) ) { $ array [ $ container ] = $ value ; } } return $ array ; } // if we have a subselector the $array[$container] must be an array if ( $ container !== '*' && ! array_key_exists ( $ container , $ array ) ) { $ array [ $ container ] = array ( ) ; } // we got here through something like *[subitem] if ( $ container === '*' ) { foreach ( $ array as $ key => $ v ) { $ array [ $ key ] = self :: setBySelector ( $ array [ $ key ] , $ subselector , $ value , $ overwrite ) ; } } else { $ array [ $ container ] = self :: setBySelector ( $ array [ $ container ] , $ subselector , $ value , $ overwrite ) ; } return $ array ; }
Set values in the array by selector

@example
Arr::setBySelector($data, 'email', 'my@domain.com');
Arr::setBySelector($data, 'addresses[0][line]', null);
Arr::setBySelector($data, 'addresses[*][line]', null);

@param  array $array
@param  string $selector
@param  mixed $value
@param  bool $overwrite true if the $value should overwrite the existing value

@return array
Set values in the array by selector