解析具有父数据和缩进子数据的多行字符串并创建多维数组

发布于 2024-10-10 20:39:17 字数 905 浏览 7 评论 0原文

我正在尝试在 PHP 中解析字符串:

 -father_name "John" -father_weight 44.50 
    -kid >name "Marko" >age 12
    -kid >name "Sevda" >age 17
    -kid >name "Nathan" >age 19

有两种主要形式:

  1. 属性(例如 -father、-weight、-kid)
  2. 子属性(例如 >name、>age)

注意: 属性不是固定的,也不总是用单个空格分隔

,它们的值有两种类型:

  1. 字符串(如“Marko”)
  2. Int 或 Decimal(如 12.00)

输出将是:

[
    'father_name' => 'John',
    'father_weight' => '44.50',
    'kid' => [
        ['name' => "Marko", 'age' => 12],
        ['name' => "Sevda", 'age' => 17],
        ['name' => "Nathan", 'age' => 19]
    ]
]

它应该返回 >表单(属性和子属性)和分开。

如何巧妙地解析 PHP 中的这一行?

最后说明:我为此找到的解决方案:YAML。

I am trying to parse a string in PHP:

 -father_name "John" -father_weight 44.50 
    -kid >name "Marko" >age 12
    -kid >name "Sevda" >age 17
    -kid >name "Nathan" >age 19

There are two main FORMS:

  1. Attributes (such as -father, -weight, -kid)
  2. Sub-Attributes (such as >name, >age)

Note: Attributes are NOT FIXED and NOT ALWAYS SEPERATED BY single space

And their VALUES have two types:

  1. String (like "Marko")
  2. Int or Decimal (like 12.00)

OUTPUT would be:

[
    'father_name' => 'John',
    'father_weight' => '44.50',
    'kid' => [
        ['name' => "Marko", 'age' => 12],
        ['name' => "Sevda", 'age' => 17],
        ['name' => "Nathan", 'age' => 19]
    ]
]

It should return FORMS (attrs and sub-attrs) and VALUES SEPARATELY.

How can I parse this line in PHP cleverly?

Last Note: Solution I found for this: YAML.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山田美奈子 2024-10-17 20:39:17

尝试使用以下方法:

function parse_attributes($string, $separators = array('-','>'), $level = 0){
    $attributes = explode($separators[$level], $string);
    $attrs = array();
    $ret_arr = array();
    foreach($attributes as $attribute){
        if(!empty($attribute)){
            $ex_attr = explode(' ',$attribute);
            if(!empty($ex_attr[1])){
                if(count($separators) > $level && strpos($attribute, $separators[$level+1])){
                    $ret = parse_attributes($attribute, $separators, $level+1);
                    array_push($ret_arr, $ret);
                }
                if (empty($ret_arr))
                    $attrs[$ex_attr[0]] = str_replace('"', '', $ex_attr[1]);
                else
                    $attrs[$ex_attr[0]] = $ret_arr;
            }
        }
    }
    return $attrs;
}

使用:

$returned = parse_attributes('-father_name "John" -father_weight 44.50 -kid >name "Marko" >age 12 -kid >name "Sevda" >age 17 -kid >name "Nathan" >age 19');

print_r($returned);

返回:

Array
(
    [father_name] => John
    [father_weight] => 44.50
    [kid] => Array
        (
            [0] => Array
                (
                    [name] => Marko
                    [age] => 12
                )

            [1] => Array
                (
                    [name] => Sevda
                    [age] => 17
                )

            [2] => Array
                (
                    [name] => Nathan
                    [age] => 19
                )

        )

)

并使用:

echo($returned['kid'][0]['name']);

返回:

Marko

注意:您可以指定更多分隔符数组项,每个属性级别对应一个项。

希望这有帮助。

Try with this:

function parse_attributes($string, $separators = array('-','>'), $level = 0){
    $attributes = explode($separators[$level], $string);
    $attrs = array();
    $ret_arr = array();
    foreach($attributes as $attribute){
        if(!empty($attribute)){
            $ex_attr = explode(' ',$attribute);
            if(!empty($ex_attr[1])){
                if(count($separators) > $level && strpos($attribute, $separators[$level+1])){
                    $ret = parse_attributes($attribute, $separators, $level+1);
                    array_push($ret_arr, $ret);
                }
                if (empty($ret_arr))
                    $attrs[$ex_attr[0]] = str_replace('"', '', $ex_attr[1]);
                else
                    $attrs[$ex_attr[0]] = $ret_arr;
            }
        }
    }
    return $attrs;
}

Using:

$returned = parse_attributes('-father_name "John" -father_weight 44.50 -kid >name "Marko" >age 12 -kid >name "Sevda" >age 17 -kid >name "Nathan" >age 19');

print_r($returned);

Returns:

Array
(
    [father_name] => John
    [father_weight] => 44.50
    [kid] => Array
        (
            [0] => Array
                (
                    [name] => Marko
                    [age] => 12
                )

            [1] => Array
                (
                    [name] => Sevda
                    [age] => 17
                )

            [2] => Array
                (
                    [name] => Nathan
                    [age] => 19
                )

        )

)

And using:

echo($returned['kid'][0]['name']);

Returns:

Marko

NOTE: You can specify more separator array items, an item for each attribute level you have.

Hope this helps.

自由如风 2024-10-17 20:39:17
$output = array();
$input = explode('-', $input);
foreach ($input as $attribute) {
   $attribute = explode('>', $attribute);
   if (count($attribute) == 1) {
      $attribute = explode(' ', trim($sub_attribute), 2);
      $output[$attribute[0]] = eval($attribute[1]);
   } else {
      $attribute_name = trim($attribute[0]);
      if (!isset($output[$attribute_name]) {
         $output[$attribute_name] = array();
      }
      $sub_attribute_output = array();
      for ($i = 1; $i < count($attribute); $i++) {         
         $sub_attribute =  explode(' ', trim($attribute[$i]), 2);
         $sub_attribute_output[$sub_attribute[0]] = eval($sub_attribute[1]);
      }
      $output[$attribute_name][] = $sub_attribute_output;
   }
}
$output = array();
$input = explode('-', $input);
foreach ($input as $attribute) {
   $attribute = explode('>', $attribute);
   if (count($attribute) == 1) {
      $attribute = explode(' ', trim($sub_attribute), 2);
      $output[$attribute[0]] = eval($attribute[1]);
   } else {
      $attribute_name = trim($attribute[0]);
      if (!isset($output[$attribute_name]) {
         $output[$attribute_name] = array();
      }
      $sub_attribute_output = array();
      for ($i = 1; $i < count($attribute); $i++) {         
         $sub_attribute =  explode(' ', trim($attribute[$i]), 2);
         $sub_attribute_output[$sub_attribute[0]] = eval($sub_attribute[1]);
      }
      $output[$attribute_name][] = $sub_attribute_output;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文