想要制作一个具有子数组php的数组的笛卡尔积

时间:2016-05-16 07:21:15

标签: php arrays associative

[
     {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL",
            "mnemonic": "R",
            "id": "SublimeREPL",
            "children":
            [
                {"command": "repl_open", 
                 "caption": "PSQL",
                 "id": "repl_sql",
                 "mnemonic": "Q",
                 "args": {
                    "type": "subprocess",
                    "encoding": {"windows": "$win_cmd_encoding",
                                 "linux": "utf-8",
                                 "osx": "utf-8"},
                    "cmd": {"windows": ["psql.exe","-U","tahnoon"],
                            "linux": ["psql","-U","tahnoon"],
                            "osx": ["psql","-U","tahnoon"]},
                    "cwd": "$file_path",
                    "cmd_postfix": "\n", 
                    "cwd": "$file_path",
                    "external_id": "sql",
                    "suppress_echo": false, 
                    "syntax": "Packages/SQL/SQL.tmLanguage"
                    }
                }
            ]   
        }]
    }
]

我的数组是这样的,我想要像这样输出

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

3 个答案:

答案 0 :(得分: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!!!

答案 1 :(得分: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));

答案 2 :(得分: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;
}