php关联数组爆炸问题

发布于 2024-11-01 17:36:39 字数 388 浏览 5 评论 0原文

我有如下的 php 脚本;

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';

$array = explode(",", $ages2);

echo $array["Peter"];

echo $ages["Peter"];

在这种情况下,echo $ages["Peter"]; 工作正常,但 echo $array["Peter"]; 不起作用。任何人都可以解决这个问题吗?

提前致谢。

巴塞拉弗雷德

I have php script as below;

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';

$array = explode(",", $ages2);

echo $array["Peter"];

echo $ages["Peter"];

In this case, echo $ages["Peter"]; is working well, but echo $array["Peter"]; is not working. Can anybody solve this please..

Thanks in advance.

blasteralfred

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

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

发布评论

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

评论(5

不奢求什么 2024-11-08 17:36:39

您必须分两步进行:

  • 首先,使用 ', ' 作为分隔符进行爆炸;获取诸如 "Peter"=>32 之类的数据
  • ,然后,对于每个值,使用 '=>' 作为分隔符进行爆炸,以拆分姓名和年龄
    • 当然要删除名称两边的双引号。

例如,您可以使用如下内容:

$result = array();

$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';
foreach (explode(', ', $ages2) as $couple) {
    list ($name, $age) = explode('=>', $couple);
    $name = trim($name, '"');
    $result[$name] = $age;
}

var_dump($result);

并且,转储数组,您将得到以下输出:

array
  'Peter' => string '32' (length=2)
  'Quagmire' => string '30' (length=2)
  'Joe' => string '34' (length=2)

这意味着使用这个:

echo $result['Peter'];

会得到:

32

You'll have to go in two steps :

  • First, explode using ', ', as a separator ; to get pieces of data such as "Peter"=>32
  • And, then, for each value, explode using '=>' as a separator, to split the name and the age
    • Removing the double-quotes arround the name, of course.

For example, you could use something like this :

$result = array();

$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';
foreach (explode(', ', $ages2) as $couple) {
    list ($name, $age) = explode('=>', $couple);
    $name = trim($name, '"');
    $result[$name] = $age;
}

var_dump($result);

And, dumping the array, you'd get the following output :

array
  'Peter' => string '32' (length=2)
  'Quagmire' => string '30' (length=2)
  'Joe' => string '34' (length=2)

Which means that using this :

echo $result['Peter'];

Would get you :

32
千寻… 2024-11-08 17:36:39

当然是行不通的。 explode 只是按给定的分隔符进行分割,但不会创建关联数组。

Of course it doesn't work. explode just splits by the given delimiter but doesn't create an associative array.

灯角 2024-11-08 17:36:39

如果您确实有这样的字符串,您唯一的希望就是手动解析它。要么使用 preg_match_all,要么我想你可以这样做:

$array = eval('return array('.$ages2.');');

但当然不建议这样做,因为它可能会在很多方面出错。

无论如何,我非常确定您可以重构此代码,或者如果您需要更多帮助,可以为我们提供更多背景信息。

Your only hope if you really have such a string is to parse it manually. Either using preg_match_all, or I suppose you could do:

$array = eval('return array('.$ages2.');');

But of course this isn't recommended since it could go wrong in many many ways.

In any case I'm pretty sure you can refactor this code or give us more context if you need more help.

平定天下 2024-11-08 17:36:39

您需要通过提取姓名和年龄来自己构建数组:

<?php
$array = array();
$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';
foreach (explode(",", $ages2) as $element) {
    $parts = explode("=>", $element);

    if (count($parts) == 2) {
        $name = str_replace(array('"', ' '), '', $parts[0]);
        $age = (int) $parts[1];

        $array[$name] = $age;
    }   
}

print_r($array);

You'll need to build the array yourself by extracting the name and age:

<?php
$array = array();
$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';
foreach (explode(",", $ages2) as $element) {
    $parts = explode("=>", $element);

    if (count($parts) == 2) {
        $name = str_replace(array('"', ' '), '', $parts[0]);
        $age = (int) $parts[1];

        $array[$name] = $age;
    }   
}

print_r($array);
你的呼吸 2024-11-08 17:36:39

$ages2 不是一个数组,所以你在这里尝试的不会直接起作用,但是你可以将具有该结构的字符串转换为这样的数组:

$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';

$items = explode(",", $ages2);
foreach ($items as $item) {
    list($key,$value) = explode('=>',$item);
    $key = str_replace('"','',trim($key)); // Remove quotes and trim whitespace.
    $array[$key] = (int)$value;
}

如果你 var_dump($array),你将拥有:

array(3) {
  ["Peter"]=>
  int(32)
  ["Quagmire"]=>
  int(30)
  ["Joe"]=>
  int(34)
}

所以你可以按预期执行此操作并返回 32:

echo $array['Peter']

$ages2 is not an array, so what you're trying here won't work directly, but you can transform a string with that structure into an array like this:

$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';

$items = explode(",", $ages2);
foreach ($items as $item) {
    list($key,$value) = explode('=>',$item);
    $key = str_replace('"','',trim($key)); // Remove quotes and trim whitespace.
    $array[$key] = (int)$value;
}

If you var_dump($array), you'll have:

array(3) {
  ["Peter"]=>
  int(32)
  ["Quagmire"]=>
  int(30)
  ["Joe"]=>
  int(34)
}

So you can do this as expected and get 32 back out:

echo $array['Peter']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文