Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 将JSON字符串转换为JSON数组_Php_Arrays_Json - Fatal编程技术网

Php 将JSON字符串转换为JSON数组

Php 将JSON字符串转换为JSON数组,php,arrays,json,Php,Arrays,Json,我有一个像这样的CSV文件 first_name,last_name,phone Joseph,Dean,2025550194 Elbert,Valdez,2025550148 [{ "first_name": "Joseph", "last_name": "Dean", "phone": "2025550194", "id": 0 }, { "first_name": "Elbert", "last_name": "Valdez", "

我有一个像这样的CSV文件

first_name,last_name,phone
Joseph,Dean,2025550194
Elbert,Valdez,2025550148
[{
    "first_name": "Joseph",
    "last_name": "Dean",
    "phone": "2025550194",
    "id": 0
}, {
    "first_name": "Elbert",
    "last_name": "Valdez",
    "phone": "2025550148",
    "id": 1
}]
使用来自GitHub的这个,我得到如下输出

first_name,last_name,phone
Joseph,Dean,2025550194
Elbert,Valdez,2025550148
[{
    "first_name": "Joseph",
    "last_name": "Dean",
    "phone": "2025550194",
    "id": 0
}, {
    "first_name": "Elbert",
    "last_name": "Valdez",
    "phone": "2025550148",
    "id": 1
}]
这几乎是我想要的——然而不是

"phone": "2025550194"
我需要

"phone": [{
    "type": "phone",
    "number": "2025550194"
}]
如何更正此问题?

您可以使用以下内容更改代码,并获得所需的输出:-

备选案文1:-

// Bring it all together
for ($j = 0; $j < $count; $j++) {
  $d = array_combine($keys, $data[$j]);
    if ($j == 'phone') {
        $newArray[$j] = array('type' => $j, 'number' => $d);
    } else {
        $newArray[$j] = $d;
    }

}
它可能会对您有所帮助。

您可以使用以下命令更改代码,并获得所需的输出:-

备选案文1:-

// Bring it all together
for ($j = 0; $j < $count; $j++) {
  $d = array_combine($keys, $data[$j]);
    if ($j == 'phone') {
        $newArray[$j] = array('type' => $j, 'number' => $d);
    } else {
        $newArray[$j] = $d;
    }

}

它可能会对您有所帮助。

如果您获得JSON字符串,那么您当然会首先将其转换为一个数组,其中包含:

$arr = json_decode($json, true);
但可能您已经拥有了阵列。然后对其应用此循环:

foreach($arr as &$row) {
    if (isset($row['phone'])) { // only when there is a phone number:
        $row['phone'] = [[ "type" => "phone", "number" => $row['phone'] ]];
    }
}

查看它是否在上运行。

如果您获得JSON字符串,那么您当然会首先将其转换为具有以下内容的数组:

$arr = json_decode($json, true);
但可能您已经拥有了阵列。然后对其应用此循环:

foreach($arr as &$row) {
    if (isset($row['phone'])) { // only when there is a phone number:
        $row['phone'] = [[ "type" => "phone", "number" => $row['phone'] ]];
    }
}

查看是否继续运行。

将csv转换为json时需要此输出,或者在获得json输出后需要此输出?@HarshSanghani这并不重要-只要最终的
echo json\u encode($newArray)语句输出正确的json我很好:)您在将csv转换为json时需要此输出,或者在获得json输出后需要此输出?@HarshSanghani其实并不重要-只要最终的
echo json_encode($newArray)语句输出正确的json我很好:)@Daniel您可以将csv中使用的最终foreach循环添加到json吗?@Daniel或者您可以尝试第二个选项。您的第二个解决方案也可以工作(+1)。我只想添加一个条件以确保“phone”键存在。@Daniel您可以将csv中使用的最终foreach循环添加到json吗?@Daniel或者您可以尝试第二个选项。您的第二个解决方案也可以工作(+1)。我只想添加一个条件以确保“phone”键存在。