Хочу сделать декартово произведение одного массива, имеющего вложенные массивы php

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
        )
    )

Мой массив такой, и я хочу вывод, как это

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

3 ответа

Использовать этот

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

Просто вызовите pumping(), вы получите это.

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

Попробуй это:

<?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 элементов.

Рабочий пример: НАЖМИТЕ!!!

Другие вопросы по тегам