Page 1 of 1

Build an array

Posted: Thu May 08, 2003 10:41 am
by oQEDo
Hi guys,
I have a "For loop" routine. As it iterates through, I would like to put the results of each iteration into an array however i'm not really sure how to do this.
The code I am using is as follows:

Code: Select all

<?php
   for($i=1; $i<=$Usercount; $i++)
   {
      //I get a name from elsewhere at this point and put it into a variable called $User. I then want to put it into an array....
      $Users = array($User)
   }

?>
This however only puts the last value into the array: [0] => "Lastname"

What I would like is for the array to contain:
[0] => "first name"
[1] => "Second name"
[2] => "Third name"
and so on.

Please can anyone help me with the correct method of doing this.

Thanks

RM

Posted: Thu May 08, 2003 11:00 am
by Heavy
You could go:

Code: Select all

<?php
   for($i=1; $i<=$Usercount; $i++){
     $User = something();
     $Users[] = $User;
   }
?>
If you want to index the array with specific keys, you can do that too. PHP will automatically resize the array for you:

Code: Select all

<?php
   for($i=1; $i<=$Usercount; $i++){
     $User = something();
     $Users[$i  * 2] = $User;
   }
?>
You could simplify the "for" syntax using 0 as the first value of $i.
Arrays in wich all of the elements are added with [] always starts with key 0:

Code: Select all

<?php
   for($i=0; $i<$Usercount; $i++){
     $User = something();
     $Users[] = $User;
   }
?>
See also:
http://www.php.net/manual/en/language.types.array.php
and:
http://www.php.net/manual/en/control-st ... oreach.php

Posted: Thu May 08, 2003 1:01 pm
by oQEDo
Heavy,
Thanks very much for your reply, I'll have a play around with all the solutions but it's exactly what I was looking for.

(It's my first attempt at arrays in php since seeing the light and moving from asp!)

RM

Posted: Thu May 08, 2003 1:50 pm
by Heavy
You are welcome.

I too have some experience from arrays in ASP VBScript.
I'd say, when it comes to arrays, PHP is a fulfilled dream compared to VBScript.

Posted: Fri May 09, 2003 4:32 am
by []InTeR[]
PHP is the best language i know to handle arrays :!: :!: :!:

Using array_search

Posted: Fri May 09, 2003 5:11 am
by oQEDo
Guys,
Thanks for your replies. On the same topic, I am trying to use the array_search feature:
I have created a multidimentional array from a database read:

Code: Select all

<?php
$Query = "SELECT Reply_Recipient FROM mailreply WHERE Reply_MailID = ".$ID." AND Reply_Group = ".$_SESSION['GroupID'];
$Result = mysql_db_query($DBName,$Query,$Link);
while($Row = mysql_fetch_array($Result))$Replyto[] = $Row;

?>
I then search another table and would like to print out the ID's which match: if the corresponding ID is stored in the the array.

Code: Select all

<?php
......
while ($Row = mysql_fetch_array($Result))
{
     $Ispresent = array_search($Row['User_ID'],$Replyto);
     print $Ispresent." ID:".$Row['User_ID'];
}
?>
It was my understanding that my variable $Ispresent should then contain the key where the criteria is found in the array search however it's not working.

So to my question!
Can I use array_search with multidimentional arrays?
Am I doing this correctly?

Thanks again guys.

Posted: Fri May 09, 2003 5:32 am
by Wayne
Is it not possible to have one query that will return this value instead of putting the results of query 1 into an array and then running another query? seems like a very long way around, or do you need the results from the original query for something else?

Posted: Fri May 09, 2003 5:49 am
by Heavy
if you instead of the second piece of code did:

Code: Select all

<?php
......
while ($Row = mysql_fetch_array($Result))
{
     //Here:
     print_r($Row['User_ID']);
     print_r($Replyto);
     exit;
     /*
     $Ispresent = array_search($Row['User_ID'],$Replyto);
     print $Ispresent." ID:".$Row['User_ID'];
     */
}
?>
...you could view source in the browser and see why it is not working.

To get it to work, exchange this row in the first piece of code:

Code: Select all

<?php
while($Row = mysql_fetch_array($Result))$Replyto[] = $Row; 
?>
for:

Code: Select all

<?php
while($Row = mysql_fetch_array($Result))$Replyto[] = $Row['Reply_Recipient']; 
?>
Then, the array_search() recieves correct arguments.

Posted: Fri May 09, 2003 6:45 am
by oQEDo
Yet again Heavy you have provided the solution......:

Code: Select all

<?php
$Row['Reply_Recipient'];
?>
.....:makes it a 1 dimentional array and then array_search works perfectly.

I owe you a beer.