加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

在PHP中将字符串如id = 1&type = 2转换为数组的最快方法?

发布时间:2020-12-13 22:07:00 所属栏目:PHP教程 来源:网络整理
导读:我需要将其更改为: $arr['id']=1;$arr['type']=2; 解决方法 使用: parse_str(). void parse_str(string $str [,array $arr]) Parses str as if it were the query string passed via a URL and sets variables in the current scope. 例: ?php $str = "fi
我需要将其更改为:

$arr['id']=1;

$arr['type']=2;

解决方法

使用: parse_str().

void parse_str(string $str [,array &$arr])

Parses str as if it were the query string passed via a URL and sets variables in the current scope.

例:

<?php
    $str = "first=value&arr[]=foo+bar&arr[]=baz";
    parse_str($str);
    echo $first;  // value
    echo $arr[0]; // foo bar
    echo $arr[1]; // baz

    parse_str($str,$output);
    echo $output['first'];  // value
    echo $output['arr'][0]; // foo bar
    echo $output['arr'][1]; // baz

?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读