将逗号分隔的属性声明字符串转换为关联数组

发布于 2024-10-28 03:33:39 字数 247 浏览 5 评论 0原文

我有一个字符串 - 类似

$string = 'key1=value1, key2=value2, key3=value3';

如何从给定字符串中获取数组,如下所示?

$array = array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
);

I have a string - something like

$string = 'key1=value1, key2=value2, key3=value3';

How can I get an array from the given string like the following?

$array = array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
);

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

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

发布评论

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

评论(4

软甜啾 2024-11-04 03:33:39

如果值(= 符号右侧的文本)在 URL 的查询字符串中没有任何具有特殊含义的字符,并且值永远不会为 null,那么您将输入转换为有效的查询字符串格式后,使用 parse_str() 不会产生任何意外结果。

$string = 'key1=value1, key2=value2, key3=value3';
parse_str(
    str_replace(", ", "&", $string),
    $array
);
var_export($array);

演示

If the values (text on the right side of the = symbols) do not have any characters with special meaning in a URL's querystring AND a value will never be null, then you won't have any unexpected results using parse_str() after converting the input to valid querystring format.

$string = 'key1=value1, key2=value2, key3=value3';
parse_str(
    str_replace(", ", "&", $string),
    $array
);
var_export($array);

Demo

◇流星雨 2024-11-04 03:33:39

一个简单的解决方案可能是:

$string = 'key1=value1, key2=value2, key3=value3';

$array = array();
foreach (explode(', ', $string) as $couple) {
    list ($key, $value) = explode('=', $couple);
    $array[$key] = $value;
}

var_dump($array);

您会得到预期的数组作为结果:

array
  'key1' => string 'value1' (length=6)
  'key2' => string 'value2' (length=6)
  'key3' => string 'value3' (length=6)

A naive solution could be :

$string = 'key1=value1, key2=value2, key3=value3';

$array = array();
foreach (explode(', ', $string) as $couple) {
    list ($key, $value) = explode('=', $couple);
    $array[$key] = $value;
}

var_dump($array);

And you'd get the expected array as a result :

array
  'key1' => string 'value1' (length=6)
  'key2' => string 'value2' (length=6)
  'key3' => string 'value3' (length=6)
温柔少女心 2024-11-04 03:33:39

这正是 parse_str 所做的。在您的情况下,您需要将逗号替换为 &

$string = 'key1=value1, key2=value2, key3=value3';
$array = parse_str(str_replace(', ', '&', $string));

// Yields
array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
)

This is exactly what parse_str do. In your case, you need to replace your commas with a &:

$string = 'key1=value1, key2=value2, key3=value3';
$array = parse_str(str_replace(', ', '&', $string));

// Yields
array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
)
套路撩心 2024-11-04 03:33:39
$a = explode(', ', $string);
$array = array();
foreach($a as $v){
    $x = explode('=', $v);
    $array[$x[0]] = $x[1];
}
$a = explode(', ', $string);
$array = array();
foreach($a as $v){
    $x = explode('=', $v);
    $array[$x[0]] = $x[1];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文