如何在同一数组内合并数组

时间:2019-02-23 05:53:38

标签: php

我有此功能的寻呼机。

//Pager: Pagination
//Show next pages box if argument is greater than 20
//We fetching 20 records per page -- Adjust to your needs

    function pager(array $options = array()){
    $options = array_merge(array(
    'NumOfRecords'      => 0, //number of records from database 
    'maxPager'          => 5, //number pager to show eg. 1-5
    'maxPageRecords'    => 20, // since I need 20 records maximum per/page
    'PageFile'            => '' , // indicates the page we're calling pager on
    'method'                  => array_merge(array('global'=>'GET','name'=>'tab_id'),array_column($options, 'method'))          
    ),$options);

    $rows = (int) $options['NumOfRecords'];
    $maxPager = (int) $options['maxPager'];
    $maxPageRecords = (int) $options['maxPageRecords'];
    $pageFile = strtolower($options['PageFile']);
    $method   = $options['method']['global'];
    $method_name = $options['method']['name'];
    }

因为我将在项目的各个位置调用此分页器函数,所以有时我可能想更改方法数组属性值

所以我想拥有默认值... 现在,我如何合并默认值,加上以后调用时可能添加的默认值

我尝试了一下,但是我可以看到是否未定义该方法的默认属性之一,它给了我未定义的索引。

我该如何解决?

以这种方式呼叫寻呼机:

pager(array(
            'maxPageRecords'=>$pageSize,
            'NumOfRecords'=>$NumOfRecords, //records could be any number e.g. 400,200..make sure its coming from DB
            'maxPager'=>5,
            'PageFile' => "questions/?page=",
            'method'   => array('name'=>'pager_id')
            ));

1 个答案:

答案 0 :(得分:0)

function pager(array $new_options = array())
{
    $options = array_merge(array(
        'NumOfRecords'      => 0, //number of records from database 
        'maxPager'          => 5, //number pager to show eg. 1-5
        'maxPageRecords'    => 20, // since I need 20 records maximum per/page
        'PageFile'          => '', // indicates the page we're calling pager on
    ), $new_options);

    $options['method'] = array_merge(array('global'=>'GET','name'=>'tab_id'), $new_options['method'] ?? []);       

    $rows = (int) $options['NumOfRecords'];
    $maxPager = (int) $options['maxPager'];
    $maxPageRecords = (int) $options['maxPageRecords'];
    $pageFile = strtolower($options['PageFile']);
    $method   = $options['method']['global'];
    $method_name = $options['method']['name'];
}

3v4l在这里