简体   繁体   English

PHP,将带有方括号的字符串转换为数组

[英]PHP, Convert a string with brackets into an array

I am querying an API and it sends a JSON response which I am then decoding into an array. 我正在查询一个API,它发送一个JSON响应,然后将其解码为数组。 This part works fine, but the API sends the information in a rather unfriendly format. 这部分工作正常,但是API以相当不友好的格式发送信息。

I will paste the part I am having issues with. 我将粘贴遇到问题的部分。 Essentially I am trying to change each like keys into their own array. 本质上,我试图将每个类似的键更改为它们自己的数组。

Array
(
    [name] => Name
    [address] => 123 Street Rd
    [products[0][product_id]] => 1
    [products[0][price]] => 12.00
    [products[0][name]] => Product Name
    [products[0][product_qty]] => 1
    [products[1][product_id]] => 2
    [products[1][price]] => 3.00
    [products[1][name]] => Product Name
    [products[1][product_qty]] => 1
    [systemNotes[0]] => Note 1
    [systemNotes[1]] => Note 2
)

Now what I would like to do is to make it like this: 现在我想做的就是这样:

Array
(
    [name] => Name
    [address] => 123 Street Rd
    [product] => Array
    (
        [0] => Array
        (
            [product_id] => 1
            [price] => 12.00
            [name] => Product Name
            [product_qty] => 1
        )
        [1] => Array
        (
            [product_id] => 2
            [price] => 3.00
            [name] => Product Name
            [product_qty] => 1
        )
    [systemNotes] => Array
    (
        [0] => Note 1
        [1] => Note 2
    )
)

Is there any practical way of doing this? 有实际的方法吗?

Thanks! 谢谢!

References are your friend here: 参考是您的朋友在这里:

$result = array();

foreach ($inputArray as $key => $val) {
    $keyParts = preg_split('/[\[\]]+/', $key, -1, PREG_SPLIT_NO_EMPTY);

    $ref = &$result;

    while ($keyParts) {
        $part = array_shift($keyParts);

        if (!isset($ref[$part])) {
            $ref[$part] = array();
        }

        $ref = &$ref[$part];
    }

    $ref = $val;
}

Demo 演示版


However, there is another, much simpler way, although it is a little less efficient in terms of functional complexity: 但是,还有另一种简单的方法,尽管在功能复杂性方面效率较低:

parse_str(http_build_query($inputArray), $result);

Demo 演示版

With $array your source array : 使用$array您的源数组:

$new_array = array("name" => $array["name"], "address" => $array["address"]);
foreach($array["products"] AS $product)
{
    $new_array["product"][] = array(
        "product_id" => $product["produit_id"],
        "price" => $product["price"],
        "name" => $product["name"],
        "product_qty" => $product["product_qty"]);
}

foreach($array["systemNotes"] AS $note)
{
   $new_array["systemNotes"][] = $note;
}

It is just browsing and creating a new structure. 它只是浏览并创建一个新结构。 ^^ ^^

Edit : Something generic could be done recursively. 编辑:通用的东西可以递归地完成。 Calling the same function as long as the browsed element is_array , and building a new array according to the keys and values. 只要已浏览的元素is_array调用相同的函数,然后根据键和值构建一个新的数组。 Looks like a file system ^^ 看起来像文件系统^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM