【问题标题】:convert string array to array php将字符串数组转换为数组php
【发布时间】:2018-01-25 23:31:34
【问题描述】:

我有这样的字符串

{"status":"2","holding":"","company":"Company","nik":"8981237","name":"Markonah","ownercard":"Istri"}

如何在 PHP 中将此字符串转换为关联数组

【问题讨论】:

    标签: php arrays string associative


    【解决方案1】:

    它的 JSON,要转换为数组使用 json_decode():

    $string = '{"status":"2","holding":"","company":"Company","nik":"8981237","name":"Markonah","ownercard":"Istri"}';
    $my_array = json_decode($string,true);
    echo $my_array["status"]; // output: 2
    

    【讨论】:

    • 执行 var_dump($my_array) 或 print_r($my_array) 以允许 OP 查看完整数组可能更好。 json_decode 还返回一个对象,因此您可以使用 $my_array->status。你错过了一个 ; $string 末尾的分号。
    • 要返回一个关联数组,您可以使用 json_decode($string, true);参考:php.net/manual/en/function.json-decode.php
    • @AhmadSopian 如果这解决了您的问题,请单击“复选标记”以接受此答案。
    猜你喜欢
    • 2010-10-15
    • 2014-05-08
    • 1970-01-01
    • 2018-01-29
    • 2013-08-12
    • 2021-12-27
    • 1970-01-01
    • 2021-08-26
    相关资源
    最近更新 更多