Search code examples
phparraysfor-loopforeacharray-push

how can i get the data from this array in php?


I have the following code..

$array_test = array();

for($i = 0;$i<5;$i++) {
    array_push($array_test,array("result".$i=>"exist_data".$i));
}

for($j = 0; $j<count($array_test);$j++) {
    echo $array_test[$j];
}

var_dump($array_test); // the data exist

but the loop for only show me ArrayArrayArrayArray

thanks for help.


Solution

  • echo $array_test[$j]; would not work because you have a 2D array and can't access the variables of 2D array in that way. This would throw Array to String Conversion error.

    Change your code to :-

    for($j = 0; $j<count($array_test);$j++) {
        echo $array_test[$j]["result".$j] ."<br>";
    }
    

    Output

    exist_data0
    exist_data1
    exist_data2
    exist_data3
    exist_data4