Hello I want to decode json. My code below:
<?php
$json = '{"response":{"count":1,"items":[{"id":165983743,"owner_id":170785079,"title":"Ke$ha - Blow","duration":253,"description":"","date":1379017507,"views":1,"comments":0,"photo_130":"http:\/\/cs518121.vk.me\/u170785079\/video\/s_5e5f6f2c.jpg","photo_320":"http:\/\/cs518121.vk.me\/u170785079\/video\/l_dd4ec237.jpg","files":{"mp4_240":"http:\/\/cs518121v4.vk.me\/u170785079\/videos\/500770e51c.240.mp4","mp4_360":"http:\/\/cs518121v4.vk.me\/u170785079\/videos\/500770e51c.360.mp4","mp4_480":"http:\/\/cs1-46v4.vk.me\/p13\/483502b20c4f.480.mp4","mp4_720":"http:\/\/cs518121v4.vk.me\/u170785079\/videos\/500770e51c.720.mp4"},"player":"http:\/\/vk.com\/video_ext.php?oid=170785079&id=165983743&hash=1e417a266e9a3f00"}]}}';
$obj = json_decode($json);
print_r ($obj);
print $obj->{'response'}->{'items'}->{'files'}->{'mp4_240'};
But I get a blank page
print_r should actually print something - your json is correct.
You should do it like this:
print $obj->response->items[0]->files->mp4_240;
Here's a code working on ideone: http://ideone.com/4xXfOl
EDIT: Please whoever downvoted these answers, explain why you do so in comments...
at first u must enable display errors at yours php interpreter
ini_set('display_errors',1);
error_reporting(E_ALL);
and then u have to read more intently the structure of json which u want to travers, the
items as an array, actually the object keys says it to you: the plural form of item
so the solve is:
print $obj->{'response'}->{'items'}[0]->{'files'}->{'mp4_240'};
of course I dislike such syntax, it would be better using
print $obj->response->items[0]->files->mp4_240;
use $obj->{'prop_name'} form when the programm selects accessing attributes dynamicly
I think you need this
echo $obj->response->items[0]->files->mp4_240;
instead of print $obj->{'response'}->{'items'}->{'files'}->{'mp4_240'};
Related
I just want to ask how to display the data from an array like this? Here's the link
This is my first time seeing an array like this long and I am confused. I tried getting the data from the cart_message and I get an array as a result. I'm sure this is the wrong format to get it. I am a newbie when it comes to this.
$messages['cart_message'];
Update:
Here's what I am trying to do.
I am trying to display the string on the cart_message
I want to display the text on the cart_message on the side cart template. So, to do that, I thought it will display if I use this filter
$yith_message = apply_filters( 'yith_ywpar_panel_messages_options', $messages);
echo '<pre>'; var_dump($yith_message); echo '</pre>';
The result is Null
Try:
var_export($messages);
More info: var_export
Hello! The data in table is like that, and I cannot echo these entries individually. I want to show this data in HTML table like for example, Subject: Math, Correct: 34, Wrong: 6.
I am new in php, please help me with this case.
The data in the table seems to be in JSON format. You can convert it to a PHP object using json_decode() then print that object using print_r().
When put all together, you get
echo print_r(json_decode($data), true);
Note, I have to edit this response back to what it is now because #fenil-shah had changed it some something else that was not suitable. Please send your own response instead of making others say what they didn't intend to.
#fenil-shah, my answer was written the way it was on purpose. Here are the explanations:
In general, if you want to stay in control of the result of the print_r() function, you need to return its result as a string. That's why I have passed true as second parameter for the print_r() function. You wouldn't using print_r() without returning its result as a string in a middle of another script because it will output some text in an uncontrolled manner that can break an add. Once you have the return string, you can send it to a log file or do whatever you want with it.
json_decode works beautifully without passing it a true as second parameter. The OP's example is showing a object. You edit was purposely changing the decoded object to an array with associative array. Why?
You have removed the space after the comma which you had moved in from the print_r() to the json_decode() to make it look like json_decode($data,true). You need to know that the space was there for two reasons: Readability and Compliance with the PSR-2 standard (read more about that PHP coding standard at https://www.php-fig.org/psr/psr-2/#46-method-and-function-calls).
There are echo "",print_r() and print() function available in php to print data.If query is returning array then use print_r() function but if query is not returning anything or invalid fetch method in php then data is not getting display by any of these functions.
I think this is what you want as a output.
You can decode the JSON received from database using json_decode() function and get your result transformed in associative array.
Later on passing the array in foreach() loop will do your work accordingly
<?php
$result = '{
"30": {
"subject_id":343,
"correct_answers":34,
"wrong _answers":61,
"not_answered":0,
"time_spent":3801,
"time_to_spend":3680,
"time_spent_correct_ answers":3286,
"time_spent_wrong_answers":515
},
"52": {
"subject_id":52,
"correct_answers":7,
"wrong_answers":3,
"not_answered":0,
"time_spent":883 ,
"time_to_spend":94343,
"time_spent_correct_ans wers":352,
"time_spent wrong_answers":441
},
"53": {
"subject_id":53,
"correct_answers":3,
"wrong_answers":7,
"not_answered":43,
"time_spent":584 ,
"time_to_spend":900,
"time_spent_correct_ans wers":154,
"time_spent wrong_answers":430
}
}';
$json_decoded_data = json_decode($result,true);
?>
<table>
<tbody>
<tr>
<th>subject_id</th>
<th>correct_answers</th>
<th>wrong_answers</th>
</tr>
<?php
foreach($json_decoded_data as $row){
echo "<tr>";
echo "<td>".$row['subject_id']."</td>";
echo "<td>".$row['correct_answers']."</td>";
echo "<td>".$row['correct_answers']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
I'm trying to retrieve a URL string from some json code.
Here is the json code
{"files":["www.example1.com"],"previews":["www.example1preview.com"],"meta":{},"userId":"guest","product":{"id":"2335","name":"standard"},"type":"u"}
Looking at what I've seen in the PHP manual I'm trying to retrieve previews like this.
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->{'previews'};
?>
This doesn't seem to output the value however.
By experimenting with php -a command on terminal, I've put your json into json_decode and managed to get your link by just doing:
print $meta_value_json->previews[0];
The only reason to use print $meta_value_json->{'previews'}; at least according to php documentation is if you want an object as output and the key trying to retrieve is numerical or of a type that is not supported by php.
By experimenting a bit further, the reason that print $meta_value_json->{'previews'}; fails is because print expects a string, in our case here previews is an array. Therefore if you do print $meta_value_json->{'previews'}[0]; it will also work as expected.
You need to get the value from your decoded json like this: $class->parameter.
Knowing that previews is an array, you will also need to choose a specific element from it to print ( I got the first one ):
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->previews[0]; /// get the specific value
?>
I need to get brand: "hello world"
When I send using post data with json and get it with
$input = Input::all();
then with
die(print_r($input));
this is written
Array ([[], {brand: "hello world"}, [], 1427154586016])1
And
I tryed using json_encode and get with
die(print_r($encode));
this
{data: "{"brand":"Hello world"}", dc: "142715"}1
now if I do
$brand = $encode['data'] or $brand = $encode['brand']
I get an error.
How do I get Hello world to var $brand
It looks like you're using json_encode() when you should be using json_decode(). But like kamlesh pointed out, it appears that your original JSON data is not valid JSON to begin with, so this would not help you. This wiki article has an example of valid syntax.
Also, if you're using laravel, you can use the helper function dd(), which will die() and var_dump() automatically for you, saving you a bit of time.
Hopefully this solves your problem, but if not, read the docs for the Input. There should be something there to get you on the right track.
Hopefully this will be quite an easy one for you here.
Anyway, I have been having alot of trouble with fread() so have decided to use an api link that I have been able to locate.
The script is simply to convert extracted game server data into a readable format.
The link for the "Serialized" data is : http://api.typefish.co.uk/monitor/sa-mp/80.86.81.14:6969/?info&masterlist&rules&players
Simply put I have very little in the way of ideas on what I can do with this data.
So far this is what I have although I am probably miles off:
$content = file_get_contents("http://api.typefish.co.uk/monitor/sa-mp/80.86.81.14:6969/?info&masterlist&rules&players");
trim($content, "{");
trim($content, "}");
foreach(exlpode(",", $content) as $pieces){
explode(":", $pieces);
}
And now I am stuck. Can someone either tell me a way thats miles better or help me finish off the script with some ideas?
Thanks
This is the code I have now after 2 answers:
$newarray = json_decode($content);
var_dump($newarray);
echo "<br><br>";
echo $newarray->{"mapname"};
The problem is that I am unable to display the variable in this format, how can I rebuild that so I can view the variables.
I have changed this:
echo $newarray->{"mapname"};
to this:
echo $newarray->data->rules->mapname;
This data is in json format. All you need to use to convert it into a php Array is json_decode($content)
Reference: http://us3.php.net/json_decode
And read more about json here: http://www.json.org/
You ar doing it wrong, it's not PHP serialized string, its json output you can use json_decode to get string into PHP array.
Like:
var_dump(json_decode($content, true));