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

时间:2021-11-11 21:43:41

I am querying an API and it sends a JSON response which I am then decoding into an array. This part works fine, but the API sends the information in a rather unfriendly format.

我正在查询API并发送一个JSON响应,然后我将其解码为一个数组。这部分工作正常,但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!

2 个解决方案

#1


5  

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

#2


0  

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. Looks like a file system ^^

编辑:通用的东西可以递归完成。只要浏览元素is_array就调用相同的函数,并根据键和值构建新数组。看起来像文件系统^^

#1


5  

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

#2


0  

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. Looks like a file system ^^

编辑:通用的东西可以递归完成。只要浏览元素is_array就调用相同的函数,并根据键和值构建新数组。看起来像文件系统^^