I want to create this JSON string with PHP:
[{name:'20140722.1304',data:[[0, 0.224],[0, 0.228]] }, {name:'20140729.1149',data:[[1, 0.224],[1,0.228]] }]
My current attempt:
$jsonArray = array(
'name' => '20140722.1304'
,'data' => array('0' => '0.024', '1'=> '0.028')
);
$jsonValue = json_encode($jsonArray);
echo $jsonValue;
But this code's output looks like:
{"name":"20140722.1304","data":["0.024","0.028"]}
Where did I went wrong? What do I have to change in my code to get to my expected output?
I finally got you to tell us what you want in the comments; please be up-front with this in the future. The expected output you gave differed from your actual output in so many ways that it was impossible to tell what you actually thought the problem was, but:
I want to get the output as {name:'20140722.1304',data:[[0, 0.224],[0, 0.228]]}
At this point, the only difference I can see is that your data is a nested array in your expected output, but not your actual output.
That has nothing to do with JSON. You're just not building your input array correctly.
Try json-encoding this:
$jsonArray = array(
'name' => '20140722.1304'
,'data' => array(array(0, 0.024), array(0, 0.028))
);
<?php
for($i=0;$i<2;$i++)
{
$jsonArray[] = array(
'name' => '20140722.1304'
,'data' => array('0' => '0.024', '1'=> '0.028')
);
}
//$jsonValue = json_encode($jsonArray);
$jsonValue = json_encode($jsonArray,true);
echo$jsonValue;
?>
try this
$jsonArray = array(
'name' => '20140722.1304'
,'data' => array('0' => '0.024', '1'=> '0.028')
);
$out = array_values($jsonArray);
echo json_encode($out);
output:
["20140722.1304",["0.024","0.028"]]
EDit:
$array = array(
2 => array("name" => '20140722.1304'),
4 => array("'data" => array('0' => '0.024', '1'=> '0.028')));
$out = array_values($array);
echo json_encode($out);
output:
[{"name":"20140722.1304"},{"'data":["0.024","0.028"]}]
Related
I have an array inside a foreach to generate data in json, but I should add a comma to validate the code. But I can not ... how can I do it?
$obj = array(
'name' => 'value',
'img' => 'value',
'url' => 'value',
);
echo json_encode($obj);
I have this code
{"name":"value","img":"value","url":"value"}
{"name":"value","img":"value","url":"value"}
{"name":"value","img":"value","url":"value"}
but I would like this code
[
{"name":"value","img":"value","url":"value"},
{"name":"value","img":"value","url":"value"},
{"name":"value","img":"value","url":"value"}
]
Don't echo the JSON in the loop. Put all the objects in another array, and convert that to JSON.
Start with an empty array:
$array = [];
In the loop push onto that array:
$array[] = array(
'name' => 'value',
'img' => 'value',
'url' => 'value',
);
After the loop is done, do:
echo json_encode($array);
I want to build an array of arrays which in the next step will be used as argument to json_encode().
Each element in the array looks like this:
$element = array(
'ITEM_ID' => $itemID,
'STATUS' => $status
)
An example of a desired result with two elements is:
array( array('ITEM_ID' => 1,'STATUS' => "ok"), array('ITEM_ID' => 2,'STATUS' => "not ok") )
I have tried:
array_push($elementArray, $element1);
array_push($elementArray, $element2);
But is does not give the desired result. What should I do?
push_array is not a php functionyou can try with array_push() or more simple
Try with
$element = array(
'ITEM_ID' => $itemID,
'STATUS' => $status
)
$element2 = array(
'ITEM_ID' => $itemID,
'STATUS' => $status
)
$finalArray[] = $element;
$finalArray[] = $element2;
echo "<pre>";
print_r($finalArray);
my php code generate a non-Valid json output error
my php code :
$questions = array();
while($question = mysql_fetch_array($result, MYSQL_ASSOC)) {
$questions[] = array('question'=> $question);
}
print_r ($questions);
$newQuestions = array('questions' => array());
foreach($questions as $key => $question){
$newQuestion = array(
'question' => $question['question']['question'],
'correct' => $question['question']['correct'],
'answers' => array(
$question['question']['answer1'],
$question['question']['answer2'],
$question['question']['answer3'],
$question['question']['answer4']
)
);
$newQuestions['questions'][] = $newQuestion;
}
$output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);
echo '<br/><br/>';
echo $output;
table fields :
Question :
correct :
answer 1 :
answer 2 :
answer 3 :
answer 4 :
Example :
Question : is php a good language ?
correct : 1
answer 1 : yes
answer 2 : no
answer 3 : maybe
answer 4 : good
the output is OK, and formated as I want.
output sample : http://pastebin.com/eefS7KYW
I am sure my php code is correct but I don't know where is exactly the issue !!
==============
Fixed : it was just two echo $output !
Seems like you are doing a lot of variable passing which gets very confusing, very quickly. Especially when all the variables are iterations of 'question'. It appears that you are creating an array from information pulled from a database in the format [questions[question,correct,answers[1,2,3,4]]] This code format may work better?
$newQuestions = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$newQuestions['questions'][] = array(
'question' => $row['question'],
'correct' => $row['correct'],
'answers' => array(
$row['answer1'],
$row['answer2'],
$row['answer3'],
$row['answer4']
)
);
}
$output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);
echo '<br/><br/>';
echo $output;
Had a semicolon in the wrong place.Fixed the code above and test the code with the following:
<?php
$array = array(
array('question'=>'Question1',
'correct'=>3,
'answer1' => 'Q1Answer1',
'answer2' => 'Q1Answer2',
'answer3' => 'Q1Answer3',
'answer4' => 'Q1Answer4'
),
array('question'=>'Question2',
'correct'=>3,
'answer1' => 'Q2Answer1',
'answer2' => 'Q2Answer2',
'answer3' => 'Q2Answer3',
'answer4' => 'Q1Answer4'
),
array('question'=>'Question3',
'correct'=>3,
'answer1' => 'Q3Answer1',
'answer2' => 'Q3Answer2',
'answer3' => 'Q3Answer3',
'answer4' => 'Q1Answer4'
)
);
$newQuestions = array();
//while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach($array as $row){
$newQuestions['questions'][] = array(
'question' => $row['question'],
'correct' => $row['correct'],
'answers' => array(
$row['answer1'],
$row['answer2'],
$row['answer3'],
$row['answer4']
)
);
}
print_r($newQuestions);
$output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);
echo '<br/><br/>';
echo $output;
?>
I want output in json like this
response:{url="www.google.com/raj.png", size=12.344KB},{url="www.google.com/raj2.png", size=12.344KB},{url="www.google.com/raj4.png", size=12.344KB}
But currently i am getting
"url=> www.google.com/img1.png size => 12.344 KB,url=> www.google.com/img2.png size => 12.344 KB"
//Using some loop here
{
$response[] = array('url' => 'url_value','size' => 'file_size');
}
//without loop hardcoded values:
$response = array ( array('url' => "www.google.com/raj.png",'size' => "12.344KB"),
array('url' => "www.google.com/img2.png",'size' => "10.344KB") );
return json_encode($response);
$out= Array(
Array('url'=>'www.goog', 'size'=>'12KB'),
Array('url'=>'moogle', 'size'=>'13KB')
);
echo Json_encode($out);
Not sure how you are formatting your array, but if you define it like this it should do the trick:
$response_array = array(
'response' => array(
array(
'url' => 'www.google.com/raj.png',
'size' => '12.344KB',
),
array(
'url' => 'www.google.com/raj.png',
'size' => '12.344KB',
),
array(
'url' => 'www.google.com/raj.png',
'size' => '12.344KB',
),
),
);
$respose_json = json_encode($response_array);
echo($response_json);
For looping through some results, try something like this:
$response_array = array('response' => array());
foreach($result_set as $result_item) {
$response_item = array();
$response_item['url'] = $result_item['url'];
$response_item['size'] = $result_item['size'];
$response_array['response'][] = $response_item;
}
$respose_json = json_encode($response_array);
echo($response_json);
With the $result_set above being whatever data you've pulled from a DB, server, etc.
I am trying to create an JSON object out of a PHP array. The array looks like this:
$post_data = array('item_type_id' => $item_type,
'string_key' => $string_key,
'string_value' => $string_value,
'string_extra' => $string_extra,
'is_public' => $public,
'is_public_for_contacts' => $public_contacts);
The code to encode the JSON look like this:
$post_data = json_encode($post_data);
The JSON file is supposed to look like this in the end:
{
"item": {
"is_public_for_contacts": false,
"string_extra": "100000583627394",
"string_value": "value",
"string_key": "key",
"is_public": true,
"item_type_id": 4,
"numeric_extra": 0
}
}
How can I encapsulate the created JSON code in the "item": { JSON CODE HERE }.
Usually, you would do something like this:
$post_data = json_encode(array('item' => $post_data));
But, as it seems you want the output to be with "{}", you better make sure to force json_encode() to encode as object, by passing the JSON_FORCE_OBJECT constant.
$post_data = json_encode(array('item' => $post_data), JSON_FORCE_OBJECT);
"{}" brackets specify an object and "[]" are used for arrays according to JSON specification.
Although the other answers posted here work, I find the following approach more natural:
$obj = (object) [
'aString' => 'some string',
'anArray' => [ 1, 2, 3 ]
];
echo json_encode($obj);
You just need another layer in your php array:
$post_data = array(
'item' => array(
'item_type_id' => $item_type,
'string_key' => $string_key,
'string_value' => $string_value,
'string_extra' => $string_extra,
'is_public' => $public,
'is_public_for_contacts' => $public_contacts
)
);
echo json_encode($post_data);
You could json encode a generic object.
$post_data = new stdClass();
$post_data->item = new stdClass();
$post_data->item->item_type_id = $item_type;
$post_data->item->string_key = $string_key;
$post_data->item->string_value = $string_value;
$post_data->item->string_extra = $string_extra;
$post_data->item->is_public = $public;
$post_data->item->is_public_for_contacts = $public_contacts;
echo json_encode($post_data);
$post_data = [
"item" => [
'item_type_id' => $item_type,
'string_key' => $string_key,
'string_value' => $string_value,
'string_extra' => $string_extra,
'is_public' => $public,
'is_public_for_contacts' => $public_contacts
]
];
$post_data = json_encode(post_data);
$post_data = json_decode(post_data);
return $post_data;