我有一个正在转换为json.I的电子表格,我可以使用php code.But进行转换,我想将数组命名为.How,我可以做这个..Please help..Php,并提到了输出和所需的输出。
Required output
["Name"{"Timestamp":"7\/24\/2015 12:42:41","Name":"ADADSADS","Type":"ASDSD","Place":"ASDSADD","Date":"ASDSD","Time":"ASDSD","Free":"ASDSD","Organizer":"ASDSD","Contact":"ASDSD","Description":"ASDSD","id":0}]Output from the below code
[{"Timestamp":"7\/24\/2015 12:42:41","Name":"ADADSADS","Type":"ASDSD","Place":"ASDSADD","Date":"ASDSD","Time":"ASDSD","Free":"ASDSD","Organizer":"ASDSD","Contact":"ASDSD","Description":"ASDSD","id":0}]<?php
/*
* Converts CSV to JSON
* Example uses Google Spreadsheet CSV feed
* csvToArray function I think I found on php.net
*/
header('Content-type: application/json');
// Set your CSV feed
$feed = 'google doc url';
// Arrays we'll use later
$keys = array();
$newArray = array();
// Function to convert CSV into associative array
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
fclose($handle);
}
return $arr;
}
// Do it
$data = csvToArray($feed, ',');
// Set number of elements (minus 1 because we shift off the first row)
$count = count($data) - 1;
//Use first row for names
$labels = array_shift($data);
foreach ($labels as $label) {
$keys[] = $label;
}
// Add Ids, just in case we want them later
$keys[] = 'id';
for ($i = 0; $i < $count; $i++) {
$data[$i][] = $i;
}
// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$newArray[$j] = $d;
}
// Print it out as JSON
echo json_encode($newArray);
?>
发布于 2015-07-25 08:29:47
在这行之前echo json_encode($newArray);
将数据分配给数组键。像这样
$newArray2['name']=$newArray;
echo json_encode($newArray2);发布于 2015-07-25 08:31:08
那是无效的,json。这样做有什么意义?
substr_replace()用替换中给出的字符串替换由开始参数和(可选)长度参数分隔的字符串副本。 混合substr_replace (混合$string,混合$replacement,混合$start,混合$length )
echo substr_replace(json_encode($newArray), '"Name"', 1, 0);但如果你指的是{ "Name": <JSON> },那么你可以:
echo json_encode(array("Name" => $newArray));https://stackoverflow.com/questions/31624486
复制相似问题