PHP将关联数组转换为索引数组

时间:2014-11-19 01:53:07

标签: php arrays multidimensional-array associative-array

这么长的故事简短,我的数组$ total看起来像这样:

Array ( 
    [0] =>  Array
            (
            [0] => 1
            [1] => 3
            ) 
    [1] => Array 
            (
            [0] => 6
            [1] => 7 
            [2] => 8
            ) 
    [2] => Array 
            ( 
            [0] => 9 
            [1] => 10 
            ) 
    )

但是我需要将它传递给一个看起来像这样的函数:

$count = cartesian(
Array("GH20"),
Array(1,3),
Array(6,7,8), 
Array(9,10)
);

我问了一个类似的问题,但我怀疑他们给出的答案是正确的,我不认为以正确的方式提出问题。 cartesain函数似乎不接受:

$count = cartesian(
Array($total[0]),
Array($total[1]),
Array($total[2])
);

经过一番挖掘之后,我相信这是因为数组有没有引起错误的键但是没有返回任何值,它只返回"数组"为了一切。

我的问题,分两部分,是如何从数组中删除键,以便根据需要进行格式化。如果可能的话,可以动态完成吗?

我已经尝试了大家在google上提出的使用array_values()函数的建议,但是这没有帮助

非常感谢任何帮助,谢谢,尼克

根据要求,案例功能:

function cartesian() {
$_ = func_get_args();
if(count($_) == 0)
    return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
    foreach($c as $p)
        $r[] = array_merge(array($v), $p);
return $r;
}

这是我想要的输出(每个排列的($ total):

Array (
[0] => Array (
    [0] => GH20
    [1] => 1
    [2] => 6
    [3] => 9
)
    [1] => Array (
    [0] => GH20
    [1] => 1
    [2] => 6
    [3] => 10
)
    [2] => Array (
    [0] => GH20
    [1] => 1
    [2] => 7
    [3] => 9
)
    [3] => Array (
    [0] => GH20
    [1] => 1
    [2] => 7
    [3] => 10
)
    [4] => Array (
    [0] => GH20
    [1] => 1
    [2] => 8
    [3] => 9
)
    [5] => Array (
    [0] => GH20
    [1] => 1
    [2] => 8
    [3] => 10
)
    [6] => Array (
    [0] => GH20
    [1] => 3
    [2] => 6
    [3] => 9
)
    [7] => Array (
    [0] => GH20
    [1] => 3
    [2] => 6
    [3] => 10
)
    [8] => Array (
    [0] => GH20
    [1] => 3
    [2] => 7
    [3] => 9
)
    [9] => Array (
    [0] => GH20
    [1] => 3
    [2] => 7
    [3] => 10
)
    [10] => Array (
    [0] => GH20
    [1] => 3
    [2] => 8
    [3] => 9
)
    [11] => Array (
    [0] => GH20
    [1] => 3
    [2] => 8
    [3] => 10
)
) 

0 个答案:

没有答案