Author Topic: Need help from REAL CODE GURU for array output  (Read 2238 times)

globonetics

  • Guest
Need help from REAL CODE GURU for array output
« Reply #1 on: April 07, 2012, 02:39:53 AM »

i have the array like (array is generated dynamically with any no of elements)

 
Code: [Select]
Array(
    [0] => Array
        (
            [0] => 149
            [1] => 140
            [2] => 141
        )

    [1] => Array
        (
            [0] => 91
            [1] => 90
        )

    [2] => Array
        (
            [0] => 118
            [1] => 117
        )

)

 

I need the output like

149,91,118
149,91,117

149,90,118
149,90,117

140,91,118
140,91,117

140,90,118
140,90,117

141,91,118
141,91,117

141,90,118
141,90,117

i write the code like

Code: [Select]

array_traverse($array_1,'0','');
function array_traverse($array_1,$element_no, $gstr) { 
  $count =  count($array_1);
  $diff = $count - $element_no;
  for($s  = $element_no; $s < count($array_1) ; $s++) {
    $str = '';
    foreach($array_1[$s] as $el) {     
       $str .= $gstr.$el.','; 
     
       if(($element_no+1) < count($array_1)) {         
        array_traverse($array_1,$element_no+1,$str);
       }
       $a1 = explode(',',substr($str,0,-1));
       if(count($a1) == $count) {
         echo $str.'[br /]';         
       }       
       $str = '';
    }
  } 
  echo '[br /]';
}


But this will gives me not-needed string too -  and i need to avoid that.

Can anybody help me please.... i really need it.

Thanks