How to print an entire array when one index is constant.
For understanding purposes I have made this array
$actionF = array( "enemyHlth" => array(array()), "enemyBlts" => array(array()) ); with input
3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 The array structure is supposed to store top three rows under a 3D array with 3rd dimension is just a constant "Enemy Health" while the bottom three in 3D array with same for 3rd dimension "Enemy Bullets". And both these two 3D arrays are stored in a single array called ActionF
Now when I try to print it
for($level=0;$level<$n;$level++){ $actionF["enemyHlth"] = array ( $level => fscanf($_fp,"%d\t%d\t%d\n") ); } for($bullets = 0;$bullets<$m; $bullets++){ $actionF["enemyBlts"] = array ( $bullets => fscanf($_fp,"%d\t%d\t%d\n") ); } print_r($actionF); Output
3 2 1 I think it is printing the last index of above I/P. How to make a 2D array when third dimension is just a constant or is there something else am I missing?
print_r($actionF);I think you'll see the array does not contain what you expect, because you haven't read the file correctly. I could try helping you write the correct code but I'm not sure what you want to achieve. What should *$actionF["enemyHlth"]** and **$actionF["enemyBlts"]** contain at the end, assuming everything works well? Also, what are **$n** and **$m**? By the way, you're also missing a *$ in front of n here: $bullets<nenemyHlthelement each time through the first loop, you're simply overwriting it. If you want to add to an array, it should be either$actionF["enemyHlth"][] = new element;or$actionF["enemyHlth"][$key] = new element;depending on whether you're creating an indexed or associative array. Your question doesn't say what the array is supposed to look like.fscanf($_fp,"%d\t%d\t%d\n")now it is at least reading the line but it is only reading last line.