How to modify subarray in place

时间:2017-12-18 07:41:20

标签: arrays ruby

I have:

email (ex: xxx@domain.com ), password and domain (ex: 'domain.com')

I want to modify this array to get:

a = [1,2,4,5]

This can be done with an a #=> [1,0,0,0] loop, but I'm trying not to use a loop here. Here's my code:

each

1 个答案:

答案 0 :(得分:7)

You can use fill:

echo "<table id='table'>";
while($row=pg_fetch_assoc($result)){echo "<tr>";
echo "<td align='left' width='200'>" . $row['message_by'] . "</td>";
echo "<td align='left' width='200'>" . $row['message_date'] . "</td>";
echo "<td align='left' width='200'>" . $row['message_text'] . "</td>";
echo "<td align='left' width='200'>" . $row['phone_number'] . "</td>";
echo "<td align='left' width='200'>" . $row['recipient_name'] . "</td>";
echo "</tr>";}
echo "</table>";

The above code sets the elements in a = [1, 2, 4, 5] #=> [1, 2, 4, 5] a.fill(0, 1) #=> [1, 0, 0, 0] a #=> [1, 0, 0, 0] to a, starting at index 0.