Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrays

Status
Not open for further replies.

Brian56

Programmer
May 29, 2003
66
CA
Hi,

Could someone please explain to a newbie how php handles arrays as i am getting lost.

Brian
 
Arrays in PHP fall into two categories-- indexed and associative. An indexed array is where each array element is denoted by a number (a subscript). They are a bit like arrays in C.

Ex:
[tt]<?php
$thisarray[0] = &quot;Howdy!&quot;
$thisarray[1] = &quot;This&quot;
$thisarray[2] = &quot;Is&quot;
$thisarray[3] = &quot;A&quot;
$thisarray[4] = &quot;Test.&quot;
$thisarray[5] = &quot;STOP!!!&quot;

for ($i=0; $thisarray[$i] != &quot;STOP!!!&quot;; $i++) {
echo '<td>' . $thisarray[$i] . '</td>';
}
?>
[/tt]

The other kind of array is an associative array. In these, each element is denoted by another string value. A bit like a hash in Perl. Example: (take note of the beginning of the script)

[tt]<?php
$dictionary['mouth'] = 'the opening through which an animal takes in food';
$dictionary['free'] = 'enjoying personal rights or liberty';
$dictionary['rush'] = 'to move, act, or progress with speed';
?>

<html><head><title>Dictionary</title></head>
<body>
<h1>A Little Dictionary</h1>
<h3>Look up a word:</h3>
<form method=&quot;GET&quot; action=&quot;dictionary.php&quot;>
<input type=text name=&quot;word&quot;>
<input type=submit>
</form>

<?php
if (isset($_GET('word')) {
if (isset($dictionary($_GET('word')) {
?>
<HR>
<H2><?php echo $_GET('word') ?></H2>
<P><?php echo $dictionary($_GET('word')) ?></P>
<?php
} else {
?>
<HR>
<P>Error: The word '<?php echo $_GET('word')?>' is not in our dictionary.</P>
<?php
} else {
?>
<P>Please type a word into the box, and click Submit.</P>
<?php
} ?>

<HR>
<SMALL><I>Powered by Tek-Tips Forums</I></SMALL>
</body>
</html>[/tt]

I REALLY hope that helps.
Will
 
WOW very impressive.

But a bit too much for me im afriad

Maybe i didnt ask the right question. I need to take the results of a mysql search store the returned values in an array then pass that array to another program.
 
$sql=&quot;select Column1 from table&quot;
$rs=mysql_query($sql); //This is the resultset..

for($i=0;$i<mysql_num_rows($rs);$i++)
{
$row=mysql_fetch_row($rs);
$arr[$i]=$row[0];
}


this will create a $arr with all the values in the Column1 field...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top