php中基于数组元素的填充

php中基于数组元素的填充,php,arrays,codeigniter,Php,Arrays,Codeigniter,这是我的密码 if(in_array("1", $mod)){ $res=array('First Name','Insertion','Last Name','Lead Country');} if(in_array("2", $mod)){ $res=array('Landline No:','Mobile No:','Lead Country');} if(in_array("3", $mod)){

这是我的密码

        if(in_array("1", $mod)){ 
        $res=array('First Name','Insertion','Last Name','Lead Country');}

       if(in_array("2", $mod)){ 
        $res=array('Landline No:','Mobile No:','Lead Country');}

        if(in_array("3", $mod)){ 
        $res=array('City','State','Country','Lead Country');}

        if(in_array("4", $mod)){ 
        $res=array('Email','Lead Country');}

        return $res;
到目前为止,它运行良好。但是如果数组包含多个值,比如(1,3) 我需要返回1和3的结果

如果数组是这样的

    array([0]=>1 [1]=>3)
然后


但是,如果有两个领先国家,只有一个应该显示如何做到这一点?
请帮助我。

使用
数组\u合并

$res = array();
if(in_array("1", $mod)){
    $res=array_merge($res, array('First Name','Insertion','Last Name','Lead Country'));
}

// and so on ...

return $res;

使用数组_merge生成结果

    $res = array();

    if(in_array("1", $mod)) { 
        $res = array_merge($res, array('First Name','Insertion','Last Name','Lead Country'));
    }

    // etc

下面是一个示例,仅当元素不存在时,才使用函数添加元素:

    function addItems($items, $arr)
    {
        foreach($items as $value)
        {
            if(!in_array($value, $arr))
            {
                $arr[] = $value;
            }
        }

        return $arr;
    }

    $res = array();

    if(in_array("1", $mod)){ 
    $res = addItems(array('First Name','Insertion','Last Name','Lead Country'), $res);}

    if(in_array("2", $mod)){ 
    $res = addItems(array('Landline No:','Mobile No:','Lead Country'), $res);}

    if(in_array("3", $mod)){ 
    $res = addItems(array('City','State','Country','Lead Country'), $res);}

    if(in_array("4", $mod)){ 
    $res = addItems(array('Email','Lead Country'), $res);}

    return $res;
这是另一种更具面向对象性的方法,可能更具逻辑性,因为它不会一直来回传递整个数组,而是使用一个保存数组的对象,并向其中添加一个方法,然后得到最终结果:

    class ItemsManager
    {
        protected $items = array();

        public function addItems($items)
        {
            foreach($items as $value)
            {
                if(!in_array($value, $this->items))
                {
                    $this->items[] = $value;
                }
            }
        }

        public function getItems()
        {
            return $this->items;
        }
    }

    $im = new ItemsManager();

    if(in_array("1", $mod)){ 
    $im->addItems(array('First Name','Insertion','Last Name','Lead Country'));}

    if(in_array("2", $mod)){ 
    $im->addItems(array('Landline No:','Mobile No:','Lead Country'));}

    if(in_array("3", $mod)){ 
    $im->addItems(array('City','State','Country','Lead Country'));}

    if(in_array("4", $mod)){ 
    $im->addItems(array('Email','Lead Country'));}

    return $im->getItems();

但是,如果有两个领先国家,那么只有一个国家应该显示如何做到这一点?@Avinash
$res=array\u unique($res)
return
语句之前。但是如果有两个lead country,则只应显示一个lead country。如何执行此操作?array\u merge将只添加尚未在数组中的元素。这就是使用它的意义所在。@rikh我想,你错了
array\u merge
在这两种情况下都会将元素添加到数组中(不在数组中/已经存在)。如果输入数组具有相同的字符串键(不是数组的值),则该键的后一个值将覆盖前一个值。但是,如果数组包含数字键,后面的值将不会覆盖原始值,而是将被追加。@Avinash没问题:)我编辑了答案,给出了另一种更好的方法。
    class ItemsManager
    {
        protected $items = array();

        public function addItems($items)
        {
            foreach($items as $value)
            {
                if(!in_array($value, $this->items))
                {
                    $this->items[] = $value;
                }
            }
        }

        public function getItems()
        {
            return $this->items;
        }
    }

    $im = new ItemsManager();

    if(in_array("1", $mod)){ 
    $im->addItems(array('First Name','Insertion','Last Name','Lead Country'));}

    if(in_array("2", $mod)){ 
    $im->addItems(array('Landline No:','Mobile No:','Lead Country'));}

    if(in_array("3", $mod)){ 
    $im->addItems(array('City','State','Country','Lead Country'));}

    if(in_array("4", $mod)){ 
    $im->addItems(array('Email','Lead Country'));}

    return $im->getItems();