Mod-Jay Posted March 21, 2011 Share Posted March 21, 2011 Hello! i want to store data in a array (as seen in the code). Im trying to make it so that a php code search through the arrays and find the correct one. once it does it tells me. I dont know if im close, How ever this is what i have. $carrier = $_POST['carrier']; $carriers = array( array("verizon","tmobile","sprint","att","virgin","textnow","metro","unknown"), array("@vtext.com","@tomomail.net","@messaging.sprintpcs.com","@txt.att.net","@vmobl.com","@textnow.me","@mymetropcs.com","@teleflip.com") ); If ($carrier = Link to comment https://forums.phpfreaks.com/topic/231320-arrays/ Share on other sites More sharing options...
Zurev Posted March 21, 2011 Share Posted March 21, 2011 I would use an associative array, why 3 arrays when you can have one?! $carrier = $_POST["carrier"]; $carriers = array( "verizon" => "@vtext.com", "sprint" => "@messaging.springpcs.com"); // etc, rest of carriers in this format... if (array_key_exists($carrier, $carriers)) { // if it exists, then we'll set it to correctCarrier $correctCarrier = $carriers[$carrier]; // this is how you would access it: $carriers[$carrier]; } Link to comment https://forums.phpfreaks.com/topic/231320-arrays/#findComment-1190534 Share on other sites More sharing options...
kenrbnsn Posted March 21, 2011 Share Posted March 21, 2011 What do you want to do once the carrier matches? If you just want to get the correct matching address, I would do it this way: <?php $carriers = array('verizon'=>'@vtext.com','tmobile'=>'@tomomail.com','sprint'=>'@messaging.sprintpcs.com','att'=>'@txt.att.net','virgin'=>'@vmobl.com','textnow'=>'@textnow.me','metro'=>'@mymetropcs.com','unknown'=>'@teleflip.com'); $somevar = (array_key_exists($_POST['carrier'],$carriers))?$carriers[$_POST['carrier']]:''; ?> Ken Link to comment https://forums.phpfreaks.com/topic/231320-arrays/#findComment-1190541 Share on other sites More sharing options...
Mod-Jay Posted March 21, 2011 Author Share Posted March 21, 2011 Okay uhm Let me explain it better, Atm im using 8 different if statements to get all the correct carriers the line "else if ($carrier == "carrier") {" is being used. I want to make it so it only uses 1 If statement. Link to comment https://forums.phpfreaks.com/topic/231320-arrays/#findComment-1190545 Share on other sites More sharing options...
Zurev Posted March 21, 2011 Share Posted March 21, 2011 Quote Okay uhm Let me explain it better, Atm im using 8 different if statements to get all the correct carriers the line "else if ($carrier == "carrier") {" is being used. I want to make it so it only uses 1 If statement. Use a switch statement, instead of 8 if/elseifs... Extending on my code: switch($correctCarrier) { case "verizon": // code if verizon break; case "tmobile": // code if tmobile break; case "sprint": // you get it by now break; } Link to comment https://forums.phpfreaks.com/topic/231320-arrays/#findComment-1190595 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.