从索引数组中选择数组值

时间:2013-05-07 06:10:27

标签: php arrays

得到这个数组:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [correct] => 
                    [answer] => false answer1
                )

            [1] => Array
                (
                    [correct] => 
                    [answer] => false answer2
                )

            [2] => Array
                (
                    [correct] => 
                    [answer] => false answer3
                )

            [3] => Array
                (
                    [correct] => 1
                    [answer] => correct answer
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [correct] => 
                    [answer] => false answer1
                )

            [1] => Array
                (
                    [correct] => 
                    [answer] => false answer2
                )

            [2] => Array
                (
                    [correct] => 
                    [answer] => false answer3
                )

            [3] => Array
                (
                    [correct] => 1
                    [answer] => correct answer
                )

        )
)

并获得了一系列索引:

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

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

我想获得的输出:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [correct] => 
                    [answer] => false answer1
                )

            [1] => Array
                (
                    [correct] => 
                    [answer] => false answer2
                )

            [2] => Array
                (
                    [correct] => 
                    [answer] => false answer3
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [correct] => 
                    [answer] => false answer1
                )

            [1] => Array
                (
                    [correct] => 
                    [answer] => false answer2
                )

            [2] => Array
                (
                    [correct] => 
                    [answer] => false answer3
                )

        )
)

如您所见,我想从第一个数组中选择所有值,具体取决于第二个数组的索引。我该怎么做?

编辑:

这是我现在试图解决的问题:

for($i = 0; $i < count($array1); $i++){
    $output_array[] = $array1[$i][$index_array[$i][0]];
    $output_array[] = $array1[$i][$index_array[$i][1]];
    $output_array[] = $array1[$i][$index_array[$i][2]];
}

输出不是我想要的......

EDIT2:

尝试过这样的事情:

$output_array[$i] = array_push($array1[$i][$index_array[$i][1]]);
$output_array[$i] = array_push($array1[$i][$index_array[$i][2]]);

但没有工作..

1 个答案:

答案 0 :(得分:0)

解决了它。

for($i = 0; $i < count($array1); $i++){
    $output_array[$i][] = $array1[$i][$index_array[$i][0]];
    $output_array[$i][] = array_merge($array1[$i][$index_array[$i][1]]);
    $output_array[$i][] = array_merge($array1[$i][$index_array[$i][2]]);
}

不确定它是否正确,但是有效..