I have a simple array of stuff:
$array = array(“apples”,”oranges”,”strawberries”);
I am trying to find the order of the stuff inside the array. (sometimes the order changes, and so do the items)
I’m expecting to get something like this:
“apples” => 0,
“oranges => 1,
“strawberries => 2
The end result has something to do with database sorting.
Something like this, inside a foreach loop:
UPDATE tbl SET sortorder = $neworder WHERE fruit = ‘$fruitname’
The $neworder variable would be populated with the new order, inside the array. While the $fruit variable comes from the item inside the array.
There may be a better way to do this, but this works:
$array = array("apples","oranges","strawberries"); $order = array(); foreach ($array as $index => $fruit) { $order[$fruit] = $index; }