想知道我是否能得到一些帮助。
我有一个如下所示的数组:
Array
(
[0] => Array
(
[0] => Model X, Model S, Model 3
[1] => Tesla
[2] => Elon Musk
)
)
我想创建另一个数组(用逗号分隔键[0]),就像这样
Array
(
[0] => Array
(
[0] => Model X
[1] => Tesla
[2] => Elon Musk
)
[1] => Array
(
[0] => Model S
[1] => Tesla
[2] => Elon Musk
)
[2] => Array
(
[0] => Model 3
[1] => Tesla
[2] => Elon Musk
)
)
希望这是有道理的,希望得到一些帮助。
感谢。
答案 0 :(得分:1)
这是您的解决方案。
$Array=array(array('Model X, Model S, Model 3','Tesla','Elon Musk'));
$Unfold1=explode(',',$Array[0][0]);
foreach($Unfold1 AS $Data){
$Response[]=array($Data,$Array[0][1],$Array[0][2]);
}
print_r($Response);
答案 1 :(得分:0)
使用explode
,array_slice
和array_merge
函数的解决方案:
$arr = [0 => ['Model X, Model S, Model 3', 1 => 'Tesla', 2 => 'Elon Musk']];
$model_info = array_slice($arr[0], 1); // model's additional info
$result = [];
foreach (explode(",", $arr[0][0]) as $model) {
$result[] = array_merge([$model], $model_info);
}
// now $result contains the expected array