在循环语句codeigniter上的1个多维数组中创建新数组

时间:2016-12-16 09:05:48

标签: php arrays codeigniter multidimensional-array

让我们玩多维数组:p

让我说$filter它包含多维数组:

array(2) {
  ["time"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
  }
  ["people"]=>
  array(5) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
    [3]=>
    string(1) "4"
    [4]=>
    string(1) "5"
  }
}
从数组

应该返回:

array(3) {
  ["time"]=>
  string(1) "1"
  ["people"]=>
  string(1) "1"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "1"
  ["people"]=>
  string(1) "2"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "1"
  ["people"]=>
  string(1) "3"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "1"
  ["people"]=>
  string(1) "4"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "1"
  ["people"]=>
  string(1) "5"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "2"
  ["people"]=>
  string(1) "1"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "2"
  ["people"]=>
  string(1) "2"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "2"
  ["people"]=>
  string(1) "3"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "2"
  ["people"]=>
  string(1) "4"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "2"
  ["people"]=>
  string(1) "5"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "3"
  ["people"]=>
  string(1) "1"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "3"
  ["people"]=>
  string(1) "2"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "3"
  ["people"]=>
  string(1) "3"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "3"
  ["people"]=>
  string(1) "4"
  ["promo_id"]=>
  string(1) "1"
}

array(3) {
  ["time"]=>
  string(1) "3"
  ["people"]=>
  string(1) "5"
  ["promo_id"]=>
  string(1) "1"
}

实际上,我可以使用以下代码获得该结果:

foreach ($filter['time'] as $row){
    foreach ($filter['people'] as $item){
        $new_array['time'] = $row;
        $new_array['people'] = $item;
        $new_array['promo_id'] = $promo_id;
        vd($new_array,"new");
    }
}

但我无法使用以下代码,因为它使用$filter['time']$filter['people'],如果timepeople是一个随机字符串怎么样?

所以你能告诉我们,从$filter循环它吗?

就像这样:

foreach ($filter as $key => $row){

}
谢谢(

1 个答案:

答案 0 :(得分:1)

function combination($arr,$res,$completed_key){
    $array_key = array_keys($arr);
    $last_array_key = array_pop($array_key);

    $start = false;
    $temp = $res;

    $res=array();
    $i=0;
    foreach($arr as $key=>$row){
        if(!in_array($key,$completed_key)&& $start==false){
            array_push($completed_key,$key);
            $start=true;
        }
        if($start){
            if(count($temp)==0){
                foreach($row as $key2 => $row2){
                    $res[][$key]=$row2;
                }
            }else{
                foreach($temp as $temp_key=>$temp_row){
                    foreach($row as $key2 => $row2){
                        $res[$i]=$temp_row;
                        $res[$i][$key]=$row2;
                        $i++;
                    }
                }
            }
        }
        if($start){
            break;
        }

    }

    if(!in_array($last_array_key,$completed_key)){
        return $this->combination($arr,$res,$completed_key,$debugger);
    }else{
        return $res;
    }
}

此功能将返回您想要的。

    $filter["time"]=array(1,2,3);
    $filter["people"]=array(1,2);
    $filter["size"]=array("a","b");
    $res=$this->combination($filter,array(),array());

我稍后会解释。