Link to home
Start Free TrialLog in
Avatar of KCTechNet
KCTechNetFlag for United States of America

asked on

newbie question - how to create an associative array

I have seen things like:
      $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

But I have not been able to find examples on how to create an associative array dynamically, In other words, I need to add items to a new array while looping through a recordset.

    while($row = mysql_fetch_assoc($res))
    {
       //  add the name and age to array
    }
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

You normally add like this:
$array[$key] = $value

In your example it makes no sense adding to the $row array.
It's only read from a mysql query, and adding to it won't have any effect on the query.

HTH,
Dan
Firstly

It appears you are using the MySQL library to access your database. This has been deprecated and will no longer be supported in future versions of PHP. Consider learning MySQLi or PDO instead

Secondly,

If you are using fetch_row_assoc the returned array is already an associative array - why do you need to create a second associative array if you have it already

To create an associative array is simply defining the key

$arr = array();
$arr['firstname'] = 'Bob';

Open in new window


You can find more info at PHP.net and W3 Schools
In PHP, all arrays are 'associative' with a 'key' and a 'value'.  If you don't create a key value, PHP will create one that is an integer that doesn't already exist in the array.  The man page for the 'array type' has some info you should probably read because it isn't all obvious.

http://www.php.net/manual/en/language.types.array.php
Avatar of KCTechNet

ASKER

where I was hoping to go with the $row was something like

while($row = mysql_fetch_assoc($res))
    {
       $fieldArray[] = array('fieldName' => $row["fieldName"], 'fieldWidth' => $row["columnWidth"]);
    }

The reason I want to put it in an array is because I will later close the connection but want to access the values in the array.  So I would later loop through $fieldArray and want to get:
$fieldArray[0] fieldName is 'field1'
$fieldArray[0] fieldWidth is 50
$fieldArray[1] fieldName is 'field2'
$fieldArray[1] fieldWidth is 150

etc..

I have been playing with the 'while' loop code in this post and I think it is loading the array, but now I don't know how to get the values back out
perhaps a good example what what I want to do is:
http://www.w3schools.com/php/showphp.asp?filename=demo_func_array4

When I try that it loops 14 times, which is correct, but  I get:
Key=0, Value=Array
Key=1, Value=Array
Key=2, Value=Array
Key=3, Value=Array
Key=4, Value=Array
Key=5, Value=Array
Key=6, Value=Array
Key=7, Value=Array
Key=8, Value=Array
Key=9, Value=Array
Key=10, Value=Array
Key=11, Value=Array
Key=12, Value=Array
Key=13, Value=Array
In "$row = mysql_fetch_assoc($res)", $row IS an array.  What you are showing above is correct.  To access the elements, you

echo $fieldArray[0]["fieldName"]." ".$fieldArray[0]["columnWidth"];
echo $fieldArray[1]["fieldName"]." ".$fieldArray[0]["columnWidth"];
echo $fieldArray[2]["fieldName"]." ".$fieldArray[0]["columnWidth"];
.
.
.
echo $fieldArray[13]["fieldName"]." ".$fieldArray[0]["columnWidth"];

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have several recordsets I loop through and I re-use $row.  I didn't even think that I don't need to create another array, just change it to a name that is not going to get destroyed.

so I changed it to:
$fieldArray=mysql_fetch_assoc($res);

then later, I am trying to access it like this:
                     foreach ($fieldArray as $k) {
                    echo $fieldArray[$k]["fieldName"].": ".$fieldArray[$k]["columnWidth"]."<br/>";
                }

but I get the error:  Warning: Invalid argument supplied for foreach() in /home/uniqueda/public_html/xtime/gridDisplay.php on line 58
oops..didnt see your last post...let me review....
that was it... perfect.

thanks
You're welcome, glad to help.