-1

I have a little problem in PHP.

My code :

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;
}

I want to use $arr but $arr feel as if it knows just one key here:

var_dump(show());

or I have this: Notice: Undefined variable: arr?

dur
  • 15,689
  • 25
  • 79
  • 125

2 Answers2

0

You need to catch output in arr like this

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;

}

Also update fillArray() call according to your output needs.Notice will be remove after using this code.

Saurabh Parekh
  • 441
  • 1
  • 3
  • 11
0

first call show function then update code which missing end ';'

<?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;

}

then you will see your output :

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
                        )

                )

        )

)
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43