2016-05-16 60 views
1
Array 
(
    [Order] => Array 
     (
      [0] => Alan Donald 
      [1] => AB Divilliars 
      [2] => Craig Macmillan 
      [3] => Faf Duplesis 
      [4] => Hashim Amla 
      [5] => Imran Tahir 
      [6] => Herschelle Gibbs 
      [7] => Lance Clusenor 
      [8] => Morne Morkel 
      [9] => Pat Symxox 
      [10] => Peter Van zil 
     ) 

    [Hows_Out] => Array 
     (
      [0] => b Ian Botham 
      [1] => run out(Aliastar Cook) 
      [2] => lbw by Luke Ronchi 
      [3] => lbw by Ian Botham 
      [4] => 
      [5] => lbw by Luke Ronchi 
      [6] => b Luke Ronchi 
      [7] => lbw by Luke Ronchi 
      [8] => lbw by Luke Ronchi 
      [9] => hit wicket(Luke Ronchi) 
      [10] => lbw by Luke Ronchi 
     ) 

    [bowler] => Array 
     (
      [0] => Ian Botham 
      [1] => Ian Botham 
      [2] => Ian Botham 
      [3] => Luke Ronchi 
      [4] => Luke Ronchi 
      [5] => Luke Ronchi 
      [6] => Luke Ronchi 
      [7] => Luke Ronchi 
      [8] => Luke Ronchi 
      [9] => Luke Ronchi 
     ) 
    ) 

我的陣列是這樣的,我想這樣的想讓其子陣列PHP一個陣列的笛卡爾積

Array 
(
    [1] => Array 
     (
      [Order] => Alan Donald 
      [Hows_out] => b Ian Botham 
      [bowler] => Ian Botham 
     ) 
     . 
     . 
     . 
) 
+0

使用foreach循環並存儲在新數組中 –

回答

0

輸出試試這個:

<?php 

$data = Array 
(
    'Order' => Array 
     (
      '0' => 'Alan Donald', 
      '1' => 'AB Divilliars', 
      '2' => 'Craig Macmillan', 
      '3' => 'Faf Duplesis', 
      '4' => 'Hashim Amla', 
      '5' => 'Imran Tahir', 
      '6' => 'Herschelle Gibbs', 
      '7' => 'Lance Clusenor', 
      '8' => 'Morne Morkel', 
      '9' => 'Pat Symxox', 
      '10' => 'Peter Van zil', 
     ), 

    'Hows_Out' => Array 
     (
      '0' => 'b Ian Botham', 
      '1' => 'run out(Aliastar Cook)', 
      '2' => 'lbw by Luke Ronchi', 
      '3' => 'lbw by Ian Botham', 
      '4' => '', 
      '5' => 'lbw by Luke Ronchi', 
      '6' => 'b Luke Ronchi', 
      '7' => 'lbw by Luke Ronchi', 
      '8' => 'lbw by Luke Ronchi', 
      '9' => 'hit wicket(Luke Ronchi)', 
      '10' => 'lbw by Luke Ronchi', 
     ), 

    'bowler' => Array 
     (
      '0' => 'Ian Botham', 
      '1' => 'Ian Botham', 
      '2' => 'Ian Botham', 
      '3' => 'Luke Ronchi', 
      '4' => 'Luke Ronchi', 
      '5' => 'Luke Ronchi', 
      '6' => 'Luke Ronchi', 
      '7' => 'Luke Ronchi', 
      '8' => 'Luke Ronchi', 
      '9' => 'Luke Ronchi', 
     ), 
    ); 


//get all lengths of subarrays 
$lengths = array(); 
foreach($data as $key => $subarray) 
    $lengths[$key] = count($subarray); 

//get max length to define loops count 
$maxLength = array_keys($lengths, max($lengths)); 

//creating results 
$result = array(); 
for($i = 0; $i < $lengths[$maxLength[0]]; $i++) 
{ 
    $singleResult = array(); 

    $singleResult['Order'] = isset($data['Order'][$i]) ? $data['Order'][$i] : null; 
    $singleResult['Hows_Out'] = isset($data['Hows_Out'][$i]) ? $data['Hows_Out'][$i] : null; 
    $singleResult['bowler'] = isset($data['bowler'][$i]) ? $data['bowler'][$i] : null; 

    $result[] = $singleResult; 
} 

print_r($result); 

我添加檢查子數組的長度,因爲在數據bowler索引數組中有10個元素,而在其他數組中有11個元素。

工作例如:CLICK!!!

0

使用此

function fetchArr($your_array) 
{ 
    $result_array = array(); 
    foreach($your_array as $key=>$value) 
    { 
     foreach($value as $k=>$v) 
     { 
      $result_array[$k][$key] = $v; 
     }   
    } 
    return $result_array; 
} 
print "<pre>";print_r(fetchArr($your_array)); 
+0

Woh !!短添加有效的代碼@Brijal,非常感謝您的幫助 –

+0

如果答案對你有幫助,那麼請隨時接受和紫外線這個答案@PuneetKumar –

0

只需調用抽水(),你得到它。

$raw_arr = [ 
     'order'=>['Alan Donald','AB Divilliars','foo'], 
     'Hows_Out'=>['b Ian Botham','run out(Aliastar Cook)','bar'], 
     'bowler'=>['Ian Botham','Ian Botham','baz'] 
      ]; 


$arr=pumping($raw_arr); 
echo '<pre>',var_dump($arr),'</pre>'; 


function pumping($raw_arr){ 
    $arr = []; 
    reset($raw_arr['order']); 
    reset($raw_arr['Hows_Out']); 
    reset($raw_arr['bowler']); 

    do { 
     $a=[]; 

     $a['order']=current($raw_arr['order']); 
     $a['Hows_Out']=current($raw_arr['Hows_Out']); 
     $a['bowler']=current($raw_arr['bowler']); 

     $i=next($raw_arr['order']); 
     next($raw_arr['Hows_Out']); 
     next($raw_arr['bowler']); 

     $arr[]=$a; 

    } while ($i !== false); 
    return $arr; 
} 
+0

謝謝Jabob!它工作正常 –