gpt4 book ai didi

php - 如何在php中将一个字符串变成一个数组

转载 作者:可可西里 更新时间:2023-11-01 00:08:25 25 4
gpt4 key购买 nike

我有一个像这样的 string..

food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'

我试过分解选项

但是我得到了一个像这样的数组

Array
(
[0] => 'food_item' => 'Butter Croissant'
[1] => 'food_id' => '1'
[2] => 'quantity' => '1'
[3] => 'price' => '140'
)

但是我需要像下面这样把它做成一个数组

Array
(
[food_item] => 'Butter Croissant'
[food_id] => '1'
[quantity] => '1'
[price] => '140'
)

我该怎么做,有人请帮助我..

提前谢谢你..

最佳答案

尝试:

$str = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'";
$mainArray = explode(",",$str);
$newArray = array();
foreach($mainArray as $main) {
$innerArray = explode("=>",$main);
$newArray[trim($innerArray[0])] = trim($innerArray[1]);
}

输出:

Array
(
[food_item] => 'Butter Croissant'
[food_id] => '1'
[quantity] => '1'
[price] => '140'
)

关于php - 如何在php中将一个字符串变成一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38503134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com