1

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?

5
  • I think that the way you're reading the data is bad. Try this: instead of those two for statements use this to display the whole array: 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<n Commented Jun 11, 2016 at 15:19
  • Its a challenge type program from Hacker's Rank. You can find it "A Super Hero" by ma5termind Commented Jun 11, 2016 at 15:29
  • You're not adding to the enemyHlth element 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. Commented Jun 11, 2016 at 15:44
  • Yes I changed them to this fscanf($_fp,"%d\t%d\t%d\n") now it is at least reading the line but it is only reading last line. Commented Jun 11, 2016 at 15:52
  • I tried print_r at first it showed something which made no sense but now it is initializing the 2 index of level and bullets correctly. Commented Jun 11, 2016 at 15:54

1 Answer 1

1

I rewrote your code and I think it works well like this:

First, the input:

$actionF = array( "enemyHlth" => array(), "enemyBlts" => array() ); for($level=0;$level<$n;$level++){ $line = fgets($_fp); $processed = explode(" ", $line); if ($line != false) { $actionF["enemyHlth"][$level] = $processed; } } for($bullets = 0;$bullets<$m; $bullets++){ $line = fgets($_fp); $processed = explode(" ", $line); if ($line != false) { $actionF["enemyBlts"][$bullets] = $processed; } } 

And here's the output:

for($i=0;$i<$n;$i++){ for($j=0;$j<$m;$j++){ print $actionF["enemyHlth"][$i][$j]; } print "\n"; } 
Sign up to request clarification or add additional context in comments.

3 Comments

You have used fgets but it returns a string rather than a integer which is required.
Would like to know the purpose why you are checking the line since the input is already defined in proper format?
PHP will auto-cast to integers strings that look like integers. You can write this code: <?php $x = "12"; $x = $x + 5; echo $x; ?> and it will output 17. So it's ok if they are strings. If you want to be extra safe you can force cast them to integer. See this: stackoverflow.com/questions/8529656/… . Regarding your second question, I'm checking the line out of habit. In real-life programming you can't ever trust the input, you must check it. In your case you can remove the test because you can be sure the input is correct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.