[PHP] Multidimensional Arrays

[PHP] Multidimensional Arrays

Post by matijevic » Sat, 19 Jul 2003 22:48:18


<snip>
select id, name from customer
redim custarray(recordcount,2)
i = 0
while not eof
custarray(i,0) = id;
custarray(i,1) = name;
i = i+1;
movenext
wend
</snip>

Not sure what kind of db you are using but I put this together using
postgresql using the manual. So this is untested.

$conn = pg_connect("host=localhost dbname=whatever");
$result = pg_exec($conn, "select id, name from customer");
$i = 0;
while ($row = pg_fetch_array($result))
{
$custarray[$i][0] = $row["id"];
$custarray[$i][1] = $row["name"];
$i++;
}
 
 
 

[PHP] Multidimensional Arrays

Post by gbroughto » Sun, 20 Jul 2003 00:48:40

Hi

Many thanks for your time Matt. I tried that method amongst a few
others, and couldn't seem to get it right. What I have eventually come
across is the function 'array-push' (bizarre!) to add a new array line
for each record (after it's been initially defined). It seems to work
for me, and hopefully it is the correct, standard way to do such a task.

$strCusts = array(array('id','name'));
/* setting up empty array for cust id and name */
if (mysql_num_rows($resCusts) > 0) {
$intCusts = mysql_num_rows($resCusts);
/* store the number of records retrieved */
while ($oRsCusts = mysql_fetch_array($resCusts)) {

array_push($strCusts,array($oRsCusts["id"],$oRsCusts["name"])); /* add
new array row */
}
}

Many thanks once again.
Gary

 
 
 

[PHP] Multidimensional Arrays

Post by curt » Sun, 20 Jul 2003 08:40:38


thats to complicated just use:
$strCust[] = $osRsCusts;
 
 
 

[PHP] Multidimensional Arrays

Post by ralp » Sun, 20 Jul 2003 09:38:28

Here's a way of doing it:

----------------------------------------------

$query = "SELECT id, name FROM customer";
$result = mysql_query($query);

while ($oRsCusts = mysql_fetch_array($result)){
$customers_array[] = array(
'id' => $oRsCusts['id'],
'name' => $oRsCusts['name']
);
}

----------------------------------------------

You now have your db results store in array $customers_array


Hope this help.

-----Original Message-----
From: Gary Broughton [mailto: XXXX@XXXXX.COM ]
Sent: Friday, July 18, 2003 8:49 AM
To: XXXX@XXXXX.COM
Subject: Re: [PHP] Multidimensional arrays

Hi

Many thanks for your time Matt. I tried that method amongst a few
others, and couldn't seem to get it right. What I have eventually come
across is the function 'array-push' (bizarre!) to add a new array line
for each record (after it's been initially defined). It seems to work
for me, and hopefully it is the correct, standard way to do such a task.

$strCusts = array(array('id','name'));
/* setting up empty array for cust id and name */
if (mysql_num_rows($resCusts) > 0) {
$intCusts = mysql_num_rows($resCusts);
/* store the number of records retrieved */
while ($oRsCusts = mysql_fetch_array($resCusts)) {

array_push($strCusts,array($oRsCusts["id"],$oRsCusts["name"])); /* add
new array row */
}
}

Many thanks once again.
Gary





--
PHP General Mailing List ( http://www.yqcomputer.com/ )
To unsubscribe, visit: http://www.yqcomputer.com/
 
 
 

[PHP] Multidimensional Arrays

Post by cparke » Wed, 22 Oct 2003 03:59:33

Ed Curtis <mailto: XXXX@XXXXX.COM >
on Monday, October 20, 2003 11:39 AM said:


[snip]


Dis:

function returnArray($input)
{
$arrCnt = count($input);
$iCnt = 0;

while($iCnt < $arrCnt)
{
$listing[$iCnt] = explode(",", $input[$iCnt]);
}

return $listing;
}

Untested, but looks ok.


Chris.

Don't like reformatting your Outlook replies? Now there's relief!
http://www.yqcomputer.com/ ~jain/software/outlook-quotefix/