Get Key from Array Value
Get Key from Array Value
(OP)
What am I doing wrong? I have 4 salesman ID's and the cities they cover in an array called "cities". How do I obtain the Salesman ID based on the city entered in a form, Field-name "city"? I don't know how to search through an array inside of another array.
CODE --> php
$cities = array ( 0 => array("Aldenville", "Analomink"), 1 => array("Abington", "Ambler", "Ardmore", "Avondale"), 2 => array("Ackermanville", "Albrightsville", "Allens Mills", "Alpha"), 3 => array("Adamstown", "Alburtis", "Allentown")); if (($key = array_search($_POST['city'], $cities)) === false) { echo "Not found"; } else { echo $key; }
RE: Get Key from Array Value
IMHO you are using the wrong array structure. You should have an array with the city name as the key and the corresponding salesman as the value. This would let you search by salesman, cycling with array_search, and of course to search by city. This structure would also avoid having two salesmen for the same city, condition that I assume you don't want.
https://www.xcalcs.com : Online engineering calculations
https://www.megamag.it : Magnetic brakes for fun rides
https://www.levitans.com : Air bearing pads
RE: Get Key from Array Value
CODE --> php