简体   繁体   中英

PHP get values from array by comma separated list

I'm trying to get a small piece of code to work, but i'm kind of stuck.

When people submit a form, it saves a few keywords, but in numbers, to prevent people from changing the input with inspect element. Every keyword has it's own number.

For example :

1 = test
2 = test2
etc.

When people submit the form, their input gets saved in a mysql database like this:

1, 2

What I'm trying to do from this point is I want to show keywords instead of numbers. I've already made an array for the keywords:

$array = array(1 => 'test1', 2 => 'test2', 3 => 'test3', 4 => 'test4');

Is there a way to only show the values from the array that are the same as the numbers from the database?

Easily done with MySQL query... example:

select KeywordName from MyTable where KeywordId in (1, 2)

You could then use a PHP prepared statement like PDO to pass the numbers to MySQL, and to fetch back the values into your array.

I'm sorry I misunderstood your question at first, but you can use this:

$list = "1, 2";
$keyArray = explode(',', $list);

$newArray = array(); //this will be your new array containing all the right values
forEach($keyArray as $key){
    $key = trim($key);
    if(isset($array[$key]))
        $newArray[$key] = $array[$key];
}

You could do as follows

$fromDB = array(1,2);

$keyMap = array(1 => 'test1', 2 => 'test2', 3 => 'test3', 4 => 'test4');

$keywords = array();    

foreach ($fromDB as $value)
{
    $keywords[] = $keyMap[$value];
}

print_r($keywords);

what i meant was that i have a comma separated list, for example: 1,2. I also have an array, which is located in my question. What i'm trying to do is i only want the values from the array, that are the same as the ones in the comma separted list. So if my list contains the numbers 1 and 2, I only want the values 1 and 2 from the array.

Use array_key_exists()

Assume we have this;

  $s = 2; //2 was fetched from the database
  $array = array(1 => 'test1', 2 => 'test2', 3 => 'test3', 4 => 'test4');
  if( array_key_exists($s, $array) ) {
      echo $array[$s];
  }

You mentioned a comma seperated list.

  $csv = "2,1,4,500"; //Was fetched from the database
  $array = array(1 => 'test1', 2 => 'test2', 3 => 'test3', 4 => 'test4');
  foreach( explode(",", $csv) ) {
    if( array_key_exists($s, $array) ) {
        echo $array[$s] . PHP_EOL; //Final Output: test2 \r\n test1 \r\n test4
    }
  }

First of all, you shouldn't store serialized data in your DB. Normalize your data appropriately instead.

That said:

$fromDB = array_flip(array_map('trim',explode(',',"1, 2")));
$keyMap = array(1 => 'test1', 2 => 'test2', 3 => 'test3', 4 => 'test4');
$intersection= array_intersect_key($keyMap, $fromDB);

The $intersection variable will hold an array containing only those values from the $keyMap array which have corresponding keys in the $fromDB one.

http://php.net/array_intersect_key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM