gpt4 book ai didi

php - 使用PHP计算字符串中的五个逗号后,将字符串转换为数组

转载 作者:行者123 更新时间:2023-12-02 06:26:50 25 4
gpt4 key购买 nike

我下面有一个字符串,

$string = "div-item-0,4,maintype:menu| heading: Quick Link|  isactive:1,0,0, div-item-1,4,maintype:text| heading:Image|  isactive:1,4,0, div-item-2,4,maintype:social| heading:Social|  isactive:1,8,0";"

现在,我想将此字符串转换为子字符串,使其成为数组元素,如下所示,
$array = [
"div-item-0,4,maintype:menu| heading: Quick, Link| isactive:1,0,0",
"div-item-1,4,maintype:text| heading:Image| isactive:1,4,0",
"div-item-2,4,maintype:social| heading:Social| isactive:1,8,0",
];

$string中计算了五个逗号后,子字符串将转换为数组元素。
如何使用PHP做到这一点?

最佳答案

您可以使用 array_chunk 方法解决此问题

<?php

$string = "
div-item-0,4,maintype:menu| heading: Quick Link| isactive:1,0,0,
div-item-1,4,maintype:text| heading:Image| isactive:1,4,0,
div-item-2,4,maintype:social| heading:Social| isactive:1,8,0";


$temp = explode(',', $string); // just create one big array
$temp = array_chunk($temp, 5); // group the array per 5 parts
foreach($temp as &$value) $value = trim(implode(',', $value)); // recombine to one string

var_dump($temp);

demo

关于php - 使用PHP计算字符串中的五个逗号后,将字符串转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57721252/

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