PHP:将字符串转换为数组

时间:2017-05-29 19:26:41

标签: php arrays

如何将String转换为数组?

String bellow

$string = "12&Shoes&28&Jewelry&30&Watch&96&Beauty&98&Kids&Baby";

想要输出:

 array{
        [12] => 'Shoes',
        [28] => 'Jewelry',
        [30] => 'Watch',
        [96] => 'Beauty',
        [98] => 'Kids&Baby'
    }

任何人都可以建议我如何使用php函数转换它?比如preg_match,preg_match_all等。所以代码应该很短。

5 个答案:

答案 0 :(得分:3)

我知道你要求正则表达式,但如果这样做很简单,我就是那个喜欢用其他方式做的人。

sudo security add-trusted-cert -p ssl -d -r trustRoot -k ~/Library/Keychains/login.keychain selfsigned.crt

答案 1 :(得分:1)

尝试下一个:

$list = array();

// $tmp = explode('&', $string); - not suitable coz of 'Value&Value' existence 
preg_match_all('/\d+|[a-z&]+/i', $string, $tmp);
foreach ($tmp[0] as $i => $value) {
    // 0, 2, ... elements are the keys
    if ($i%2 === 0) {
        $key = $value;
    // 1, 3 ... values
    } else {
        $list[$key] = trim($value, '&');
    }
}

甚至更短的版本:

preg_match_all('/(\d+)\&([a-z&]+[^\&0-9])/i', $string, $tmp);
$list = array_combine($tmp[1], $tmp[2]);

结果:

var_dump($list);

array(5) {
  [12] => string(5) "Shoes"
  [28] => string(7) "Jewelry"
  [30] => string(5) "Watch"
  [96] => string(6) "Beauty"
  [98] => string(9) "Kids&Baby"
}

答案 2 :(得分:1)

请尝试使用preg_split PREG_SPLIT_DELIM_CAPTURE获取已拆分的项目,

$string = "12&Shoes&28&Jewelry&30&Watch&96&Beauty&98&Kids&Baby";

$array = preg_split("/(\d+)/", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
foreach($array AS $key => $val) {
   if($key%2) continue;
   $result[$val] = trim($array[$key+1], '&');
}

答案 3 :(得分:0)

我删除了之前的回答,因为我建议parse_str,但我没有正确地阅读这个问题。

一种解决方案可能是,使用preg_match_all。 唯一的问题是,我无法过滤掉最后一个&在每个值的末尾。试图找出regexp非匹配如何再次起作用:)

注意!如果您的值包含数字,则此功能无效!

php > preg_match_all('/(?P<key>[0-9]+)&(?P<values>[^0-9]+)/i', $string, $matches);
php > var_dump($matches);
array(5) {
  [0]=>
  array(5) {
    [0]=>
    string(9) "12&Shoes&"
    [1]=>
    string(11) "28&Jewelry&"
    [2]=>
    string(9) "30&Watch&"
    [3]=>
    string(10) "96&Beauty&"
    [4]=>
    string(12) "98&Kids&Baby"
  }
  ["key"]=>
  array(5) {
    [0]=>
    string(2) "12"
    [1]=>
    string(2) "28"
    [2]=>
    string(2) "30"
    [3]=>
    string(2) "96"
    [4]=>
    string(2) "98"
  }
  [1]=>
  array(5) {
    [0]=>
    string(2) "12"
    [1]=>
    string(2) "28"
    [2]=>
    string(2) "30"
    [3]=>
    string(2) "96"
    [4]=>
    string(2) "98"
  }
  ["values"]=>
  array(5) {
    [0]=>
    string(6) "Shoes&"
    [1]=>
    string(8) "Jewelry&"
    [2]=>
    string(6) "Watch&"
    [3]=>
    string(7) "Beauty&"
    [4]=>
    string(9) "Kids&Baby"
  }
  [2]=>
  array(5) {
    [0]=>
    string(6) "Shoes&"
    [1]=>
    string(8) "Jewelry&"
    [2]=>
    string(6) "Watch&"
    [3]=>
    string(7) "Beauty&"
    [4]=>
    string(9) "Kids&Baby"
  }
}

答案 4 :(得分:-1)

试试这个:

function trim_custom($item){
  return trim($item,'&');
}

$string = "12&Shoes&28&Jewelry&30&Watch&96&Beauty&98&Kids&Baby";
$values = array_filter( explode(",",preg_replace("/[^a-zA-Z-&]+/", ",", $string)) );
$values = array_map('trim_custom',$values);
$keys = array_filter ( explode(",",preg_replace("/[^0-9]+/", ",", $string)) );

$list = array_combine($keys,$values);

输出:

print_r($list);

Array
(
    [12] => Shoes
    [28] => Jewelry
    [30] => Watch
    [96] => Beauty
    [98] => Kids&Baby
)