简体   繁体   English

PHP通过逗号分隔列表从数组中获取值

[英]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. 当人们提交表单时,它保存了一些关键字,但是以数字形式保存,以防止人们使用inspect元素更改输入。 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: 人们提交表单时,他们的输入将保存在mysql数据库中,如下所示:

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: 使用MySQL查询轻松完成...示例:

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. 然后,您可以使用像PDO这样的PHP预处理语句将数字传递给MySQL,然后将这些值取回数组中。

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. 我的意思是说我有一个用逗号分隔的列表,例如: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. 因此,如果我的列表包含数字1和2,则只需要数组中的值1和2。

Use array_key_exists() 使用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. $intersection变量将保持容纳只从那些值的数组$keyMap阵列具有在相应的键$fromDB之一。

http://php.net/array_intersect_key http://php.net/array_intersect_key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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