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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题