将字符串分解为数组

问题描述 投票:0回答:5

我想用不同类型的字符分解一个字符串。

我已经知道如何爆炸一根绳子,我也想我知道如何在不同的级别上做到这一点。

问题是如何使用其中一个值作为名称($var['name'])而不是数字($var[0])?

只知道如何在数组中手动执行此操作,但不知道如何用变量添加它。

总而言之,我想要这个字符串:

title:hello;desc:message|title:lorem;desc:ipsum;ids:1,2,3

成为这个数组:

Array
(
    [0] => Array
        (
            [title] => hello
            [desc] => message
        )

    [1] => Array
        (
            [title] => lorem
            [desc] => ipsum
            [ids] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )
        )

)

我随机制作了字符串作为示例,有什么数组天才可以帮助我吗? :)

谢谢!

编辑:

我想裂纹没明白。

我已经知道如何分解它,但是如何使用一些值作为名称,就像我上面向您展示的那样。

我尝试过爆炸数组,但不知道如何设置名称。 我一直在使用 foreach()。

<?php
    $string = "title:hello;desc:message|title:lorem;desc:ipsum;ids:1,2,3";
    $string = explode("|", $string);
    foreach($string as $split){
        $split = explode(";", $split);
        foreach($split as $split2){
            $split2 = explode(":", $split2);
            // more code..
        }
    }
?>

现在我需要 $split2[0] 作为名称,$split2[1] 作为值,就像前面的示例一样。

php arrays string explode
5个回答
2
投票

为什么不直接使用

json_encode() / json_decode()
serialize() / unserialize()

演示


0
投票
$input = "title:hello;desc:message|title:lorem;desc:ipsum;ids:1,2,3";
$items = preg_split("/\|/", $input);
$results = array();
$i = 0;
foreach($items as $item){
     $subItems = preg_split("/;/", $item);
     foreach($subItems as $subItem){
        $tmp = preg_split("/:/", $subItem);
        $results[$i][$tmp[0]] = $tmp[1];
     } 
     $i++;

}

返回:

数组(2) {
  [0]=>
  数组(2){
    [“标题”]=>
    字符串(5)“你好”
    [“描述”]=>
    字符串(7)“消息”
  }
  [1]=>
  数组(3){
    [“标题”]=>
    字符串(5)“lorem”
    [“描述”]=>
    字符串(5)“ipsum”
    [“id”]=>
    字符串(5)“1,2,3”
  }
}

然后处理你的“ids”索引


0
投票

您可以将字符串转换为 JSON,然后将 json 解码为数组。例如:

    $text = 'title:hello;desc:message|title:lorem;desc:ipsum;ids:1,2,3';

    $json = '[' . preg_replace(
        array("#(.*?):(.*?)(;|\||$)#i", "#\"((.?),(.*?))\"#i", "#(.*?)\|((.+)|$)#i", "#;#"),
        array('"$1":"$2"$3','[$1]', '[{$1}],[{$2}]', ','),
        $text
    ) . ']';

    $res = json_decode($json, true);

参见:http://codepad.viper-7.com/ImLULZ

但是@Glavić 的权利。


0
投票

我尝试在没有正则表达式的情况下使用

explode()
进行精确输出,但在您接受的答案中给出 id 作为
string

<?php


$str="title:hello;desc:message|title:lorem;desc:ipsum;ids:1,2,3";

$a=explode("|",$str)[0];
$b=explode("|",$str)[1];
$c=explode(";",$a)[0];
$d=explode(";",$a)[1];
$f=explode(";",$b)[0];
$g=explode(";",$b)[1];
$i=explode(";",$b)[2];
$e[][explode(":",$c)[0]]=explode(":",$c)[1];
$e[explode(":",$d)[0]]=explode(":",$d)[1];
$e[][explode(":",$f)[0]]=explode(":",$f)[1];
$e[1][explode(":",$g)[0]]=explode(":",$g)[1];
$e[1][explode(":",$i)[0]]=explode(",",explode(":",$i)[1]);

var_dump($e);

输出

array (size=3)
  0 => 
    array (size=1)
      'title' => string 'hello' (length=5)
  'desc' => string 'message' (length=7)
  1 => 
    array (size=3)
      'title' => string 'lorem' (length=5)
      'desc' => string 'ipsum' (length=5)
      'ids' => 
        array (size=3)
          0 => string '1' (length=1)
          1 => string '2' (length=1)
          2 => string '3' (length=1)

0
投票

当所有分隔字符都是静态/文字时,绝对没有理由使用正则表达式 - 只需使用

explode()
。在循环时爆炸,在最低级别上,如果值包含逗号,则在逗号上爆炸以填充深层子数组。

代码:(演示

$string = 'title:hello;desc:message|title:lorem;desc:ipsum;ids:1,2,3';

$result = [];
foreach (explode('|', $string) as $setString) {
    $set = [];
    foreach (explode(';', $setString) as $rowString) {
        [$key, $value] = explode(':', $rowString);
        $set[$key] = str_contains($value, ',') ? explode(',', $value) : $value;
    }
    $result[] = $set;
}
var_export($result);

输出:

array (
  0 => 
  array (
    'title' => 'hello',
    'desc' => 'message',
  ),
  1 => 
  array (
    'title' => 'lorem',
    'desc' => 'ipsum',
    'ids' => 
    array (
      0 => '1',
      1 => '2',
      2 => '3',
    ),
  ),
)
© www.soinside.com 2019 - 2024. All rights reserved.