Wondering why my PHP code will not display all "Value" of "Values" in the JSON data:
$user = json_decode(file_get_contents($analytics));
foreach($user->data as $mydata)
{
echo $mydata->name . "\n";
}
foreach($user->data->values as $values)
{
echo $values->value . "\n";
}
The first foreach works fine, but the second throws an error.
{
"data": [
{
"id": "MY_ID/insights/page_views_login_unique/day",
"name": "page_views_login_unique",
"period": "day",
"values": [
{
"value": 1,
"end_time": "2012-05-01T07:00:00+0000"
},
{
"value": 6,
"end_time": "2012-05-02T07:00:00+0000"
},
{
"value": 5,
"end_time": "2012-05-03T07:00:00+0000"
}, ...
You maybe wanted to do the following:
foreach($user->data as $mydata)
{
echo $mydata->name . "\n";
foreach($mydata->values as $values)
{
echo $values->value . "\n";
}
}
You need to tell it which index in data to use, or double loop through all.
E.g., to get the values in the 4th index in the outside array.:
foreach($user->data[3]->values as $values)
{
echo $values->value . "\n";
}
To go through all:
foreach($user->data as $mydata)
{
foreach($mydata->values as $values) {
echo $values->value . "\n";
}
}
$user->data is an array of objects. Each element in the array has a name and value property (as well as others).
Try putting the 2nd foreach inside the 1st.
foreach($user->data as $mydata)
{
echo $mydata->name . "\n";
foreach($mydata->values as $values)
{
echo $values->value . "\n";
}
}
Related
I have the following JSON on a page
{
"users": [
{
"name": "John",
"age": "30",
},
{
"name": "Dave",
"age": "20",
},
...
]
}
I'm decoding it using $json = json_decode($data);. If I want a loop that print every name and age for each user how can I do that?
I'm tring to do something like the following code but it's not working
foreach($json->users->name as $key => $result){
$name = $result;
$age = $result->age;
echo $name;
echo $age;
echo "<br>";
}
What am I doing wrong? How could I achieve that?
You can do it like this:
$string = "{\"users\": [{\"name\": \"BA8842_530\",\"age\": \"0.0\"},{\"name\": \"BA8842_540\",\"age\": \"20\"}]}";
$json = json_decode($string, true);
foreach ($json['users'] as $key => $value) {
echo "Name: " . $value['name'] . "<br>";
echo "Age: " . $value['age'] . "<br>";
}
The output would be:
// Output
Name: BA8842_530
Age: 0.0
Name: BA8842_540
Age: 20
...
Hope this helps!
foreach($json->users as $row){
echo $row->name;
echo $row->age;
echo "<br>";
}
I had searched the answer but there's no solution for my problem.
How make output like this?
Thanks in advance
I want out this below
Title: First Title
Tags: tag-a-1, tag-a-2,tag-a-3
Title: Second Title
Tags: tag-b-1, tag-b-2, tag-b-3
Title: Third Title
Tags: tag-c-1, tag-c-2, tag-c-3
file.json
{
"videos": [
{
"title": "First Title",
"tags": [
{
"tag_name": "tag-a-1"
},
{
"tag_name": "tag-a-2"
},
{
"tag_name": "tag-a-3"
}
],
"publish_date": "2016-09-12 16:40:14"
},
{
"title": "Second Title",
"tags": [
{
"tag_name": "tag-b-1"
},
{
"tag_name": "tag-b-2"
},
{
"tag_name": "tag-b-3"
}
],
"publish_date": "2016-09-12 16:40:14"
},
{
"title": "Third Title",
"tags": [
{
"tag_name": "tag-c-1"
},
{
"tag_name": "tag-c-2"
},
{
"tag_name": "tag-c-3"
}
],
"publish_date": "2016-09-12 16:40:14"
}
]
}
output.php
<?php
ini_set('display_errors', 1);
$html = "file.json";
$html = file_get_contents($html);
$videos = json_decode($html, true);
foreach ($videos['videos'] as $video) {
$title = $video['title'];
foreach ($video['tags'] as $tags) {
$tags = $tags['tag_name'];
echo 'Title: ' . $title . '<br />';
echo 'Tags: ' . $tags . ', <br /><br />';
}
}
Your second iteration seems to be wrong. You should print title/tags in your first loop instead.
foreach ($videos['videos'] as $video) {
$title = $video['title'];
$tags = array(); // reset it every new item to avoid race-condition on empty one.
foreach ($video['tags'] as $tags) {
$tags[] = $tags['tag_name'];
// ^ also add new elements here
}
echo 'Title: ' . $title . '<br />';
echo 'Tags: ' . implode(',', $tags) . '<br /><br />';
// ^ also join your tags
}
Try using the json_decode function. That seems the way to go.
http://php.net/manual/en/function.json-decode.php
I've linked the docs in for you. Best of luck.
I am new to PHP. So bear with me. I have to fetch songs from db. I don't know how to initialize associate array in a for loop using keyValuePair. and also add status attribute to it.
What i want is
{
"status" : "true" ,// It tells whether day available or not
"data": [
{
"name": "Joe Bloggs",
"id": "203403465"
},
{
"name": "Fred Bloggs",
"id": "254706567"
},
{
"name": "Barny Rubble",
"id": "453363843"
},
{
"name": "Homer Simpson",
"id": "263508546"
}
]
}
My Code
$html = file_get_html('http://1stfold.com/taskbox/Farrukh/Zare/');
$output = array();// how to initialze it in for loop with keyValue pair
// Find all "A" tags and print their HREFs
foreach($html->find('.branded-page-v2-body a') as $e)
{
if (0 === strpos($e->href, '/watch?v'))
{
$output[] = $e->href . '<br>';
echo $e->href . '<br>';
}
}
echo json_encode($output);
Thanks in Advance.
You can add an array to the $output array, by simply changing this :
$output[] = $e->href . '<br>';
To this :
$output['data'][] = array('name' => $name_value, 'id' => $id_value);
This will push arrays to the the $output['data'] array.
You should add the "status" keyValuePair before the loop
Objective: To parse following json string and get mentioned values separately, later those separated values are going to be inserted to mysql database. I have checked my json string on JsonLint
user_name,
selected_date,
selected_project,
tasks , 4.1.task_name, 4.2 work_hours
{
"user_name": "USER",
"selected_date": "2015-06-08",
"selected_project": "Project1",
"tasks": [
{
"task_name": "task-1",
"work_hours": [
{
"Monday": " 3"
},
{
"Tuesday": " 0"
},
{
"Wednesday": " 2.5"
},
{
"Thursday": " 2"
},
{
"Friday": " 0"
},
{
"Saturday": " 0"
},
{
"Sunday": " 0"
}
]
}
]
}
PHP code is:
$str_json = file_get_contents('php://input'); //($_POST doesn't work here)
$response = json_decode($str_json, true); // decoding received JSON to array
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($response, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val)
{
if(is_array($val))
{
echo "$key:\n";
}
else
{
echo "$key => $val\n";
echo "$value";
}
}
As i am new to JSON and PHP, i could not solve this, help would be appreciated.
<?
$json = '{
"user_name": "USER",
"selected_date": "2015-06-08",
"selected_project": "Project1",
"tasks": [
{
"task_name": "task-1",
"work_hours": [
{
"Monday": " 3"
},
{
"Tuesday": " 0"
},
{
"Wednesday": " 2.5"
},
{
"Thursday": " 2"
},
{
"Friday": " 0"
},
{
"Saturday": " 0"
},
{
"Sunday": " 0"
}
]
}
]
}';
// decode your json into associative arrays
$decoded = json_decode($json, true);
// use array
echo "Username: ". $decoded['user_name'] . "<br>";
echo "Date: ". $decoded['selected_date'] . "<br>";
echo "project: ". $decoded['selected_project'] . "<br>";
echo "Task: ". $decoded['tasks'][0]['task_name'] . "<br>";
foreach($decoded['tasks'][0]['work_hours'] as $key => $value) {
foreach($value as $key2 => $value2){
echo $key2 . ": ". $value2 . "<br>";
}
}
?>
There is records about the login_ids in json array. I want to fetch required results in json-php decode function. It is giving the login_id two times on top and bottom for each row. I there any way not to print the login_id in the value filed? The code details are given below.
<?php
$json = '{"records" :
[
{
"Name": "Jhon",
"Age": "45",
"Place": "Kettle",
"Phone": "9834453",
"log_id": "216"
},
{
"Name": "Bill",
"Age": "41",
"Place": "Ottava",
"Phone": "4513453",
"log_id": "215"
},
{
"Name": "James",
"Age": "39",
"Place": "Mexico",
"Phone": "3456734",
"log_id": "217"
},
{
"Name": "larry",
"Age": "51",
"Place": "New city",
"Phone": "34890453",
"log_id": "213"
}
]
}';
$myjson = json_decode($json, true); // decode the json string to an array
foreach ( $myjson['records'] as $row ) { // loop the records
echo 'Details of login id: '. $row['log_id']. '<br/>';
foreach ( $row as $field => $value ) { // loop the fields
echo $field . ': '. $value . '<br>';
}
echo '<br/>';
}
?>
Also is there any simple method to print the required login id (i.e where the login_id="217") so that the address of one person is given out put.
Following code my be better but not tested.
foreach ( $myjson['records'] as $row ) {
if ($row['log_id'] =='216') { // login id to be checked
echo 'Details of login id: '. $row['log_id']. '<br/>';
foreach ( $row as $field => $value ) { // loop the fields
if ($field != "log_id") { // hide the login id t bottom
echo $field . ': ' . $value . '<br>';
}
}
echo '<br/>';
}
}
Test the field name before printing it.
foreach ($row as $field => $value {
if ($field != "log_id") {
echo $field . ': ' . $value . '<br>';
}
}