PHP问题 - 如何用字符串创建数组?

问题描述:

In my database, some field settings are serialized and stored. When I do this:

print_r(unserialized($r['settings']));

I'll get this:

Array ( 
[prefix] => 
[suffix] => 
[min] => 
[max] => 
[allowed_values] => 1|Common 2|Rare 3|Almost Extinct 
)

I'm trying to create an array based on the values for allowed_values like this:

Array (
[1] => Common
[2] => Rare
[3] => Almost Extinct
)

The problem is, when I use explode("|", $r['allowed_values']), I get:

Array(
[0] => 1
[1] => Common 2
[2] => Rare 3
[3] => Almost Extinct
)

Which, makes sense, but obviously not what I was hoping for... So, I'm just wondering if there's a simple way to do what I'm trying here? I thought about using explode multiple times, once for spaces, and once for pipes, but that won't work either because of the space in "Almost Extinct"...

在我的数据库中,某些字段设置被序列化并存储。 当我这样做时: p>

  print_r(unserialized($ r ['settings'])); 
  code>  pre> 
 
 

我会得到这个: p>

  Array(
 [prefix] => 
 [suffix] => 
 [min] => 
 [max  ] => 
 [allowed_values] => 1 |普通2 |罕见3 |几乎绝迹
)
  code>  pre> 
 
 

我正在尝试创建一个数组 基于allowed_values的值,如下所示: p>

  Array(
 [1] => Common 
 [2] => Rare 
 [3] =>  ;几乎绝迹
)
  code>  pre> 
 
 

问题是,当我使用explode(“|”,$ r ['allowed_values'])时,我得到: p>

 数组(
 [0] => 1 
 [1] => Common 2 
 [2] =>稀有3 
 [3] =&gt  ;几乎绝迹
)
  code>  pre> 
 
 

这是有道理的,但显然不是我所希望的...所以,我只是想知道是否有一个简单的 做我正在尝试的方式? 我想过多次使用爆炸,一次用于空间,一次用于管道,但由于“几乎灭绝”中的空间而无法使用...... p> div>

try this:

 $tab=array();
 preg_match_all("/\s*(\d+)\|([^\d]+)/",$r['allowed_values'],$tab);
 print_r(array_combine($tab[1],$tab[2]));

this should make the trick :)

I think this should work. This splits the first number and the "|" char

<?php
$string = "1|Faruk 2|Test";
$array = preg_split('/\d/', $string, -1, PREG_SPLIT_NO_EMPTY); 
$content = explode("|", implode($array));

var_dump($content);
?>

And here is the Result

array(3) {
  [0]=>
  string(0) ""
  [1]=>
  string(6) "Faruk "
  [2]=>
  string(4) "Test"
}

If the original indexes are not sequential from 1, and you need to keep them as they are, see dweeves answer, as that keeps the indexes and values bound together correctly.

$string = unserialized($r['settings']['allowed_values']);
//$string = '1|Common 2|Rare 3|Almost Extinct';

print_r (preg_split("/\d+\|/", $string, -1, PREG_SPLIT_NO_EMPTY));

Outputs:

Array
(
    [0] => Common 
    [1] => Rare 
    [2] => Almost Extinct
)

I would use a regex to match the pattern of each entry, but capture only the text of interest. Using preg_match_all(...), the $match object is populated with sub-arrays of the capture groups.

$input = '1|Common 2|Rare 3|Almost Extinct';
preg_match_all('/\d+\|([^\d]+)/',$input, $match);
print_r($match[1]);

Produces:

Array
(
    [0] => Common 
    [1] => Rare 
    [2] => Almost Extinct    
)

Note that if you're labels will use numbers, you'll need to tweak the regex.

This method also allows you to capture the number and create your own ordered array, if the values stored in your tables might be out of order.