PHP Deep Extend Array

后端 未结 7 1701
遇见更好的自我
遇见更好的自我 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条回答
  •  萌比男神i
    2020-12-31 02:05

    I use this in the same way I use angular.extend(dst, src) and jQuery.extend().

    function extend($base = array(), $replacements = array()) {
        $base = ! is_array($base) ? array() : $base;
        $replacements = ! is_array($replacements) ? array() : $replacements;
    
        return array_replace_recursive($base, $replacements);
    }
    

    Example:

    si() is a utility sanitize function that grabs $_POST or $_GET and returns an array.

            $s = extend(array(
                'page' => 1,
                'take' => 100,
                'completed' => 1,
                'incomplete' => 1,
            ), si());
    

提交回复
热议问题