Jump to content

Arrays


Mod-Jay

Recommended Posts

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
Share on other sites

I would use an associative array, why 3 arrays when you can have one?! :P

 

$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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.