Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 循环遍历数组并将字符串转换为嵌套数组_Php - Fatal编程技术网

Php 循环遍历数组并将字符串转换为嵌套数组

Php 循环遍历数组并将字符串转换为嵌套数组,php,Php,我有10个数组,格式如下: 这是$data Array ( [0] => stdClass Object ( [id] => 1 [name] => Product Name [category] => Product category [permName] => Product-Name

我有10个数组,格式如下:

这是$data

Array
(
    [0] => stdClass Object
        (
                [id] => 1
                [name] => Product Name
                [category] => Product category
                [permName] => Product-Name
                [picture] => http://randomdomain.com/1.jpg
                [idUser] => 1,2,3
                [rating] => 120,880,450
                [description] => Review 1, Review 2, Review 3
                [firstName] => Name 1, Name 2, Name 3
                [lastName] => Last 1, Last 2, Last 3
                [userName] => userName 1, Username 2, Username 3
        )
    [1] => stdClass Object
        (
                [id] => 2
                [name] => Product Name 2
                [category] => Product category
                [permName] => Product-Name
                [picture] => http://randomdomain.com/1.jpg
                [idUser] => 1,2,3
                [rating] => 120,880,450
                [description] => Review 1, Review 2, Review 3
                [firstName] => Name 1, Name 2, Name 3
                [lastName] => Last 1, Last 2, Last 3
                [userName] => userName 1, Username 2, Username 3
        )  

)
我想把每个数组转换成一个嵌套数组中的idUser、rating、description、firstName、lastName、userName。我想做这样的事情:

foreach ($data as $row) {                   
    $firstName = $row->firstName;           
    $firstNames = explode(',', $firstName); 
}
这会将其转换为数组,但如何将其作为嵌套数组插入到原始数组的原始位置?


<?php
foreach( $data as $row ) {
    $firstName = $row->firstName;           
    $firstNames = explode(',', $firstName);
    $nested = array();
    foreach( $firstNames as $name ) {
       $nested[] = $name;
    }
    $row->firstName = $nested;
}
?>

嗨,克莱蒙特,工作正常吗?可能有错误-我发布了一个希望更正确的版本:)。您可以不使用
$nested
变量来完成它,但我很忙!