PHP Deep Extend Array

后端 未结 7 1683
遇见更好的自我
遇见更好的自我 2020-12-31 01:53

How can I do a deep extension of a multi dimensional associative array (for use with decoded JSON objects). I need the php equivalent of jQuery\'s $.extend(true, array1, arr

7条回答
  •  攒了一身酷
    2020-12-31 02:15

    With a little googling I found this:

    /**
     * jquery style extend, merges arrays (without errors if the passed values are not arrays)
     *
     * @return array $extended
     **/
    function extend() {
        $args = func_get_args();
        $extended = array();
        if(is_array($args) && count($args)) {
            foreach($args as $array) {
                if(is_array($array)) {
                    $extended = array_merge($extended, $array);
                }
            }
        }
        return $extended;
    }
    
    extend($defaults, $new_options);
    

提交回复
热议问题