PHP函数填充数组

时间:2017-05-26 13:08:21

标签: php arrays

我在PHP中遇到了一些问题。

我的代码:

function fillArray($line, $col, $par0, $par1, $par2, $par3){
    $arr[$line][$col][0] = $par0;
    $arr[$line][$col][1] = $par1
    $arr[$line][$col][2] = $par2;
    $arr[$line][$col][3] = $par3;
    return $arr
}


function show() {

    for( $a = 0; $a < 5; $a++) {
        fillArray( $a, 0, "test", "test","test", "test");
    }

    for($b = 0; $b < 5; $b++) {
        $c = 1;
        fillArray( $b, $c, "test", "test","test", "test");

        $c = 2;
        fillArray( $b, $c, "test", "test","test", "test");
    }

    return $arr;
}

我想使用$arr,但$arr感觉好像只知道一个键:

var_dump(show());

或者我有这个:注意:未定义的变量:arr?

2 个答案:

答案 0 :(得分:0)

您需要像这样

捕获输出
function show(){

    for( $a = 0; $a < 5; $a++ ){
    $arr[] = fillArray( $a, 0, "test", "test","test", "test");
    }

    for( $b = 0; $b < 5; $b++){
    $c = 1;
    $arr[] = fillArray( $b, $c, "test", "test","test", "test");

    $c = 2;
    $arr[] = fillArray( $b, $c, "test", "test","test", "test");
    }

    return $arr;

}

还根据您的输出需求更新fillArray()调用。使用此代码后将删除通知。

答案 1 :(得分:0)

首先调用show function然后更新缺少结束';'

的代码
<?php
$result = show();
echo "<pre>";
print_r($result);

function fillArray($line, $col, $par0, $par1, $par2, $par3)
{
    $arr[$line][$col][0] = $par0;
    $arr[$line][$col][1] = $par1; // add ; 
    $arr[$line][$col][2] = $par2;
    $arr[$line][$col][3] = $par3;
    return $arr; // add ; for end

}

function show()
{

    for ($a = 0; $a < 5; $a++) {
        $arr[] = fillArray($a, 0, "test", "test", "test", "test");
    }

    for ($b = 0; $b < 5; $b++) {
        $c = 1;
        $arr[] = fillArray($b, $c, "test", "test", "test", "test");

        $c = 2;
        $arr[] = fillArray($b, $c, "test", "test", "test", "test");
    }

    return $arr;

}

然后你会看到你的输出:

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

                )

        )

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

                )

        )

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

                )

        )

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

                )

        )

    [4] => Array
        (
            [4] => Array
                (
                    [0] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [5] => Array
        (
            [0] => Array
                (
                    [1] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [6] => Array
        (
            [0] => Array
                (
                    [2] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [7] => Array
        (
            [1] => Array
                (
                    [1] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [8] => Array
        (
            [1] => Array
                (
                    [2] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [9] => Array
        (
            [2] => Array
                (
                    [1] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [10] => Array
        (
            [2] => Array
                (
                    [2] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [11] => Array
        (
            [3] => Array
                (
                    [1] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [12] => Array
        (
            [3] => Array
                (
                    [2] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [13] => Array
        (
            [4] => Array
                (
                    [1] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

    [14] => Array
        (
            [4] => Array
                (
                    [2] => Array
                        (
                            [0] => test
                            [1] => test
                            [2] => test
                            [3] => test
                        )

                )

        )

)