[thelist] php: multidimensional arrays; and use array_unique or db index?

Michael Barrett mbarrett at abouthalf.com
Sat Mar 29 11:25:00 CST 2003


On Saturday, March 29, 2003, at 01:59  AM, Dunstan Orchard wrote:

> In the end I saw the light and came up with this code, but I'd be 
> interested
> to know what the 'proper' way to extract data from a multi-d array is.

In answer to your first question: foreach is a good tool for inserting 
and pulling data from an array.

foreach($array["index1"]["index2"] as $key => $value) {

//key is the value of each index2
//value is the value assigned to the key

$sql = insert into table $key = $value;

$result = mysql_query($sql);

}

foreach works with a copy of the array. So you don't have to reset or 
resort the array after calling it. You should apply sorting to the 
array prior to calling foreach.

If you don't need the keys from the array, you can use this syntax:
foreach($array as $value)
which only returns each value from the index.



More information about the thelist mailing list