Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I currently have the following code which has an array, and outputs it into a JSON file called 'example.json'.

Below is the code that outputs to it:

PHP
$x = array(1, 2, 3); //Defining two basic arrays
$y = array(2, 4, 6);
$name = array("Joe", "John", "Johnny");
echo count($x);
$objOne = '["type": "FeatureCollection", "features": [';
file_put_contents("jsonfun.json", json_encode($objOne));
for($i = 0; $i < count($x); $i++)
{
    $objTwo = '{ "type": "Feature", "geometry": {"type": "Point", "coordinates": [' . $x[$i] . ', ' . $y[$i] . ']}, "properties": {"name": ' . $name[$i] . '} }]';
    file_put_contents("jsonfun.json", json_encode($objTwo), FILE_APPEND);
}
$objThree = '};';
file_put_contents("jsonfun.json", json_encode($objThree), FILE_APPEND);


Output:
"[\"type\": \"FeatureCollection\", \"features\": [""{ \"type\": \"Feature\", \"geometry\": {\"type\": \"Point\", \"coordinates\": [1, 2]}, \"properties\": {\"name\": Joe} }]""{ \"type\": \"Feature\", \"geometry\": {\"type\": \"Point\", \"coordinates\": [2, 4]}, \"properties\": {\"name\": John} }]""{ \"type\": \"Feature\", \"geometry\": {\"type\": \"Point\", \"coordinates\": [3, 6]}, \"properties\": {\"name\": Johnny} }]""};"


As you may have seen there are a lot of slashes which I don't know where they came from... and likewise there are also several quotations where they shouldn't be;

eg.
"[type...


Is there any chance I could remove those, or am I doing it wrong?

What I have tried:

I have researched into it but wasn't able to find anything...
Posted
Updated 30-Nov-16 23:47pm
v2
Comments
Peter_in_2780 30-Nov-16 23:39pm    
You need to understand what json_encode() does. See http://php.net/manual/en/function.json-encode.php for starters.

1 solution

Quote:
As you may have seen there are a lot of slashes which I don't know where they came from... and likewise there are also several quotations where they shouldn't be;
Your code is doing exactly what it is asked for.
Use the debugger to see your code execute.
Advice: store the results of json_encode in temporary variable and inspect the variables with debugger.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900