PHP数组合并问题,多级数组合并为单级

时间:2017-05-19 05:49:49

标签: php arrays codeigniter merge

这是我的输出:我使用更多的foreach循环,将不同的条件和值放入相同的数组名$returnVal[]=

EX:

foreach($val1 as $vals)
{
      $returnVal[]=$vals;
}




foreach($val2 as $vals)
  {
      $returnVal[]=$vals;
  }



 foreach($val3 as $vals)
  {
      $returnVal[]=$vals;
   }



 foreach($val4 as $vals)
   {
      $returnVal[]=$vals;
   } `

我得到输出:

 Array
    (
        [0] => Array
            (
                [0] => 39705--COLUMBUS--LOWNDES--MS
                [1] =>  39702--COLUMBUS--LOWNDES--MS
                [2] =>  39710--COLUMBUS--LOWNDES--MS
                [3] =>  39701--COLUMBUS--LOWNDES--MS
            )

        [1] => Array
            (
                [0] => Array
                    (
                        [0] => 39705--COLUMBUS--LOWNDES--MS
                        [1] =>  39702--COLUMBUS--LOWNDES--MS
                        [2] =>  39710--COLUMBUS--LOWNDES--MS
                        [3] =>  39701--COLUMBUS--LOWNDES--MS
                    )

            )

        [2] => Array
            (
                [0] => Array
                    (
                        [0] => 39705--COLUMBUS--LOWNDES--MS
                        [1] =>  39702--COLUMBUS--LOWNDES--MS
                        [2] =>  39710--COLUMBUS--LOWNDES--MS
                        [3] =>  39701--COLUMBUS--LOWNDES--MS
                    )

                [1] => Array
                    (
                        [0] => Array
                            (
                                [0] => 39705--COLUMBUS--LOWNDES--MS
                                [1] =>  39702--COLUMBUS--LOWNDES--MS
                                [2] =>  39710--COLUMBUS--LOWNDES--MS
                                [3] =>  39701--COLUMBUS--LOWNDES--MS
                            )

                    )

                [2] => 57038--JEFFERSON--UNION--SD
            )

        [3] => 57049--NORTH SIOUX CITY--UNION--SD
    )

我的问题:删除重复的值并合并到一个级别

1 个答案:

答案 0 :(得分:1)

如果结果数组中不存在推送元素,这将避免重复的条目。请尝试下面的代码,

$returnVal = [];
foreach($val1 as $vals)
{
    if (!in_array($vals, $returnVal)) {
        $returnVal[]=$vals;
    }

} ..etc and so on