0

guys lets play in multidimensional array little bit :p

lets said i have $filter which it contain multidimensional array :

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"
  }
}

from the array it should be return :

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"
}

actually i could get that result with this following code :

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");
    }
}

but i cant use that following code, because it using $filter['time'] and $filter['people'], how about if the time or people is a random string?

so can you show me guys, to looping it from $filter?

as like this :

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

}

thank you (:

1 Answer 1

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;
    }
}

This function will return you want.

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

I'll explain later.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.