0

我有以下数组:

Array
(
    [0] => Array
        (
            [id] => 23
            [title] => jkl
        )

    [1] => Array
        (
            [id] => 478
            [title] => lmn
        )

    [2] => Array
        (
            [id] => 22
            [title] => pqr
        )

    [3] => Array
        (
            [id] => 21
            [title] => abc
        )
)

同样是第二个数组:

Array
(
    [0] => Array
        (
            [id] => 103
            [title] => Activities
        )

    [1] => Array
        (
            [id] => 76
            [title] => Top 10 Ideas
        )

    [2] => Array
        (
            [id] => 9
            [title] => Best Shopping Areas
        )    
)

如果第一个数组的计数小于 3,我想将第一个数组附加到第二个数组中,因此我使用了数组推送功能并这样做:

if (count($secondArr) < 3) {
            echo "<pre>";
            echo array_push($secondArr, $firstArr);
            print_r($result);
            exit;
        }

现在在 array_push 之后显示这样的数组(多维)

Array
(
    [0] => Array
        (
            [id] => 76
            [title] => Activities
        )

    [1] => Array
        (
            [id] => 103
            [title] => Top 10 Ideas
        )

    [2] => Array
        (
            [id] => 9
            [title] => Best Shopping Areas
        )

    [3] => Array
        (
            [0] => Array
                (
                    [id] => 23
                    [title] => jkl
                )

            [1] => Array
                (
                    [id] => 478
                    [title] => lmn
                )

            [2] => Array
                (
                    [id] => 22
                    [title] => pqr
                )

            [3] => Array
                (
                    [id] => 21
                    [title] => abc
                )   

        )

)

我不想这样(多维)。我希望它附加键 4,5 等等。如果是的话,那有可能吗?

谢谢。

4

7 回答 7

2

你应该array_merge改用。

$result = array_merge($secondArr, $firstArr);
于 2012-10-04T06:21:57.383 回答
1

array_push向数组中插入一个新元素。你想合并两个数组:

$firstArray = array_merge($firstArray, $secondArray);
于 2012-10-04T06:21:46.750 回答
1

根据array_push 的 PHP.net 规范:

array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:

所以它不会工作。您可能希望以完全相同的方式使用array_merge而不是,。array_push

于 2012-10-04T06:23:29.653 回答
1

使用 array_merge

http://in2.php.net/manual/en/function.array-merge.php

array_push — 将一个或多个元素压入数组末尾

http://au.php.net/manual/en/function.array-push.php

于 2012-10-04T06:23:30.860 回答
0

你可以做:

$array1 = array(array(1,2,3),array(1,2,3));
$array2 = array(array(4,5,6),array(4,5,6));

if(count($array2) > 3){
    foreach($array2 as $val){
            $array1[] = $val;
    }
}
print_r($array1);

或者您可以简单地合并:

$new_array = array_merge($array1 , $array2);
于 2012-10-04T06:32:29.513 回答
0

如果你仍然坚持使用数组推送,试试这个:

if (count($secondArr) < 3) {
    echo "<pre>";
    foreach($firstArr as $arr){
        array_push($secondArr,$arr);
    }
    print_r($secondArr);
    exit;
}

但我同意使用array_merge。

于 2012-10-04T06:42:45.753 回答
0

您可以使用 array_merge 尝试以下代码

if (count($secondArr) < 3) {
            array_merge($secondArr, $firstArr);
        }
于 2012-10-04T07:24:35.540 回答