Reputation: 91
Create a program with the following output:
a 00000000
b 00000000
c 00000X00
d 00000000
e 00000000
f 000X0000
g 00000000
h 00000X00
12345678
The user enters two-dimensional array at the beginning of the program. Based on this sequence (coordinates) are drawn X's on the table.
Conditions: Must be used arrays and loops.
I`m 18 and I started to learn php 2 weeks ago. Im having trouble with this task. Can anyone help me ? THANKS!
SORRY!
this is what I`ve done:
<?php
$input = array(2 => array(5),5 => array(3),7 => array(6));
$range = array('a','b','c','d','e','f','g','h');
$length = 8;
$output = '';
foreach($range as $index => $letter)
{
$output .= "$letter ";
for($i = 0; $i < $length; ++$i)
{
$output .= (array_key_exists($index, $input) && in_array($i, $input[$index])) ? 'X' : '0';
}
$output .= "\n";
}
echo $output;
?>
i got this output:
a 00000000
b 00000000
c 00000X00
d 00000000
e 00000000
f 000X0000
g 00000000
h 000000X0
instead of this:
a 00000000
b 00000000
c 00000X00
d 00000000
e 00000000
f 000X0000
g 00000000
h 00000X00
12345678
Where I am making a mistake?
Upvotes: 1
Views: 131
Reputation: 2995
<?php
$input = array(2 => array(5),5 => array(3),7 => array(5));
$range = array('a','b','c','d','e','f','g','h');
$length = 8;
$output = '';
$x='';
foreach($range as $index => $letter)
{
$output .= "$letter ";
for($i = 0; $i < $length; ++$i)
{
$output .= (array_key_exists($index, $input) && in_array($i, $input[$index])) ? 'X' : '0';
}
$output .= "\n";
$x.= 1+$index;
}
$output .= $x;
echo $output;
?>
this will output
a 00000000
b 00000000
c 00000X00
d 00000000
e 00000000
f 000X0000
g 00000000
h 00000X00
12345678
Upvotes: 0
Reputation: 5957
7 => array(6)
should be 7 => array(5)
http://sandbox.onlinephpfunctions.com/code/423262c6a4bcdb8693c179dc620966d779b65a51
Upvotes: 1