druhoytza979667566 2012-10-22 10:37
浏览 31
已采纳

在php中基于数组元素填充

This is my code.

        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;

Upto this it works fine. But if the array contains more than one value say (1,3) I need to return both results of 1 and 3.

eg: if the array is like this

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

then

    $res=array('First Name','Insertion','Last Name','City','State','Country','Lead Country') 

But if there are 2 lead country only one should be displayed how to do this? Pls help me.

  • 写回答

3条回答 默认 最新

  • drxnfdx798517235 2012-10-22 10:52
    关注

    Here's an example that uses a function to add the elements, only if they don't already exist:

        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;
    

    Here's another way of doing it which is more OOP and is probably a bit more logical because it doesn't keep passing the whole array back and to, instead it uses an object which holds the array, and has a method to add to it, and get the final result:

        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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件