Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Key from Array Value

Status
Not open for further replies.

Pudsters

Technical User
Mar 16, 2006
151
US
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.
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;
}
 
I can't see a way, other than cycling over all of the cities of the array.
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.

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
I think I will just break it up into 4 different arrays, one for North, South, East, and West cities, like this. The finished project will end up having hundreds of cities:

PHP:
$northCities = array("Aldenville", "Analomink", "Archbald");
$arrlength = count($northCities);
    
    for($i = 0; $i < $arrlength; $i++) {
	
	   if (($_POST['state'] == "PA" || $_POST['state'] == "NJ") && $northCities[$i] == $_POST['city']) {	
	   	  $myBcc = $salesNorth;	   
		  $salesman = $salesmanNorth;	
	   } else {	
	     $myBcc;	
	   }                
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top