0

I want to assign values from an array to a simple variable, my code is as follows :

$codeval = $_POST['code']; //can be Apple or Banana or Cat or Dog
$systemrefcode = array("a" => "Apple", "b" => "Banana", "C" => "Cat", "D" => "Dog");

foreach($systemrefcode as $code => $value) {
     if($codeval == $value){ //if Apple exists in array then assign code and use it further
        $codes = $code;//Assign code to codes to use in next step

     }
$selection = 'Your Selection is -'.$codes.'and its good.';
echo $selection;

When I check in console it shows no response. What am I doing wrong?

4
  • 1
    Not sure if it fixes it, but you are missing a double quote in "D => "Dog". Try "D" => "Dog" and there is a closing } missing. Commented Aug 30, 2018 at 18:21
  • those are common mistakes, i did them in a hurry, but still after fixing that didn;t get what i wanted Commented Aug 30, 2018 at 18:25
  • What happens when you var_dump $codeval? If it doesn't match anything in your array, you won't get any output. Commented Aug 30, 2018 at 19:20
  • as far i can see they;re matching the array but i got no response. Commented Aug 30, 2018 at 19:25

3 Answers 3

1

You can get the key of the wanted value with array_search():

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;

So, for your code works, you can use like this:

$codeval = $_POST['code'];
$systemrefcode = array("a" => "Apple", "b" => "Banana", "C" => "Cat", "D" => "Dog");

$code = array_search($codeval, $systemrefcode);

$selection = 'Your Selection is - '.$code.' and its good.';
echo $selection;

OBS.:

  1. array_search() will return false if value is not found;
  2. array_search() is case sensitive, so if you have 'Apple' in the array and search for 'apple', it'll return false.
Sign up to request clarification or add additional context in comments.

5 Comments

but when the requirement is dynamic, like have the same array as you mentioned, and will do like this $color = $_POST['color']; $key = array_search($color, $array); $choose = 'You chose'.$key.'color code'; echo $choose; Will i get the code of the respective color from array?
@BRAHMA array_search() returns the key of the searched value, so if the key is a color code, yes, you'll get the code of the respective color. But have in mind that array_serach() will return false if searched value is not in the array.
Did you run my code and checked? See nothing is coming.
@BRAHMA I edited the post to show you how you can use it in your code.
@BRAHMA what do you mean as "not working"? it's echoing nothing? are you having en error message? and also the code in this link is not the one I wrote.
0

You can flip the $systemrefcode array so that the values become keys and vice versa.

$coderefsystem = array_flip($systemrefcode);
$codes = $coderefsystem($codeval);
$selection = 'Your Selection is -'.$codes.'and its good.';
echo $selection;

Comments

0

You could break out of the foreach when there is a match and echo the string afterwards.

$post = "1-Apple";
$codeval = explode('-', $post)[1];
$systemrefcode = array("a" => "Apple", "b" => "Banana", "C" => "Cat", "D" => "Dog");
$codes = "";

foreach ($systemrefcode as $code => $value) {
    if ($codeval === $value) { //if Apple exists in array then assign code and use it further
        $codes = $code;//Assign code to codes to use in next step
        break;
    }
}

if ($codes !== "") {
    $selection = 'Your Selection is -' . $codes . ' and its good.';
    echo $selection; // Your Selection is -a and its good.
} else {
    echo "codes is empty";
}

4 Comments

This is working since you assigned the value directly to $codeval, but in my case the value is coming to this php file via AJAX and using form serialize(), so do i need to do something special and again the value assigned to the $codeval is like (1-Apple, and i'm exploding it then assigning the 1st index to $codeval and then doing the rest), is there any modification to code, as per your suggestion?
@BRAHMA So you are doing something like 3v4l.org/GK09a What exactly is missing?
In my case again its not working, will you please check this one 3v4l.org/rhlil
@BRAHMA You are forgetting explode. Check 3v4l.org/sYDar That will return an array from which you will get the second entry [1] which will be "Prepaid"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.