Page 1 of 1

associative arrays

Posted: Thu Jun 23, 2005 3:25 am
by matscott
Hi all.

I have been bashing my head against a brick wall with this one.
What I am attempting to do is read a table from mysql database.
From the table I wish to extract the id value and use it as my associative array key and pull the quantity in as the value in the array.
Here is the code I am using:

Code: Select all

<?
//$stock_array = array();
$stock_control = mysql_query ("SELECT * FROM stock_tab WHERE location='mail_order'");
while ($stock_row = mysql_fetch_array($stock_control))
{
$stock_id33 = $stock_row['order_id'];
$qu33 = $stock_row['quantity'];
array_push($stock_array[$stock_id33], $qu33);
}
foreach ($stock_array as $key => $value)
echo $key.'=>'.$value.'<br />';
?>
The error I recieved is
Warning: First argument to array_push() needs to be an array in /data/html/test_site/get_stock_array.php on line 17
so it would appear to be a error in my construction of array_push.
I have used several other methods all of which do not appear to work.

Does anyone know how to correctly code this so that I can push key values and values into my associative array.

Thanks in advance.

Posted: Thu Jun 23, 2005 3:30 am
by big_c147
i could be very very wrong here after just posting for help on arrays myself i dont confess to be a expert (far from it) :D ...........anyway on your first line should

Code: Select all

//$stock_array = array();
be commented out?

code error

Posted: Thu Jun 23, 2005 4:02 am
by matscott
sorry, typo
please consider that it isn't commented out.
I am going to repost the code as I failed to notice the posting code notice first time around.

Code: Select all

<?
$stock_array = array();
$stock_control = mysql_query ("SELECT * FROM stock_tab WHERE location='mail_order'");
while ($stock_row = mysql_fetch_array($stock_control))
{
$stock_id33 = $stock_row['order_id'];
$qu33 = $stock_row['quantity'];
array_push($stock_array[$stock_id33], $qu33);
}
foreach ($stock_array as $key => $value)
echo $key.'=>'.$value.'<br />';
?>
thanks for the pointer though.

solved

Posted: Thu Jun 23, 2005 4:38 am
by matscott
I have solved it.

Code: Select all

<?

include '../frenchsole/include/fs_local_db.php';
$stock_array = array();
$stock_control = mysql_query ("SELECT * FROM stock_tab WHERE location='mail_order'");
while ($stock_row = mysql_fetch_array($stock_control))
{
$stock_id33 = $stock_row['stock_id'];
$qu33 = $stock_row['quantity'];
//array_push($stock_array[$stock_id33], $qu33);
$stock_array[$stock_id33] = $qu33;
}
foreach ($stock_array as $key => $value)
echo $key.'=>'.$value.'<br />';

?>
I made some typos which I found and then used the original code I had last night and it works as expected.
Fantastic.