json-php giving filed out put two times? - php

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>';
}
}

Related

Remove JSON Field In MySQL [PHP] [CodeIgniter]

I wrote a function to accept a 'product_id' and then it will cycle through all my Json fields to find all the json objects without the id 'product id' and then I need it encode it back and update database.
currently I substituted 'product_id' with '154' since that is a 'id' I am testing to remove.
This Foreach statement I wrote gets me all the ids
$user = $this->session->userdata('user_id');
$autoOrder = $this->db->get_where('auto_order', ['user_id' => $user])->row()->products;
$temp = json_decode($autoOrder);
foreach ($temp as $value) {
$last_value = (array)$value;
foreach ($last_value as $key=> $value) {
foreach($value as $key2=>$value2) {
if ($key2 == 'id' && $value2 != '154') {
echo "Product Id: " . $value2 . "<br>";
$new_ids[] = $value2;
}
}
}
}
How can I now cycle through all the autoOrder and retrieve only the ones with 'new_ids'?
Example How The JSON Looks:
[
{
"order": {
"id": "154",
"qty": "1",
"option": "{\"color\":{\"title\":\"Color\",\"value\":null}}",
"price": "2433.62",
"name": "Race Car",
"shipping": "0",
"tax": "26",
"image": "http://localhost/products/image/34",
"coupon": ""
}
}
]
This code you wrote it's a bit complicated to understand.
Anyway, you should save the new_ids into the DataBase, and then execute the query specifying the condition to retrieve only the object without new_ids on the WHERE.

Select individual column json in php

{
"responseData": {
"results": [
{
"title": "sobig",
"titleNoFormatting": "test",
},
{
"title": "test 2 ",
"titleNoFormatting": "test 2sd",
},
{
"title": "asdasdasda",
"titleNoFormatting": "asdasdasd",
},
{
"title": "A Warming",
"titleNoFormatting": "A Warming",
}
.
.
.
.
{
"title": "last thing",
"titleNoFormatting": "sada",
}
],
I have json files like this.
for($i=$veri1; $i <= $veri2; $i++) {
$uri = "http://test.com/json/".$i."/0";
$json = json_decode(file_get_contents($uri));
if($json->data->price >= $nakit && $json->data->odds >= $oran)
{
I'm getting some data with this code correctly from another json file.
i want get data from first json code, if "title" == "sobig" . How can I do that.
$json->responseData->results->title == sobig is not working. How can I get data if title is sobig
$json= json_decode($response, true);
foreach ($json['responseData']['results'] as $key => $value) {
if ($value == 'sobig') {
// found it
}
}
Try this example to see if this may fix your issue.
<?php
$json = '{ "responseData": {
"result" : [
{ "title": "sobig" , "titleNo":"test"},
{ "title": "loco" , "titleNo":"test"},
{ "title": "tom" , "titleNo":"test"}
]
}}';
$jsonDecoded = json_decode($json);
foreach ($jsonDecoded->responseData->result as $key => $value) {
var_dump($value); echo '<br>';
if($value->title == 'sobig'){
echo "we did it!!";
echo "<br>";
}
}
?>
I place a couple of var dumps so you can see the stucture of your object and why you need to use the foreach

how to create json from associate array

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

Accessing data in a multidimensional JSON object

I have the following JSON object returned from an API call in the $result variable, and my problem is that I need to access the "name" item in each list, but it doesn't work
{
"status": "success",
"data": {
"lists": [
{
"id": "0032",
"name": "Stan",
"status": "active",
},
{
"id": "0043",
"name": "David",
"status": "active",
},
{
"id": "2323",
"name": "Robert",
"status": "pending",
}
]
}
}
Code :
if (isset($result)) {
$json_object = json_decode($result, true);
echo $json_object['status'];
echo $result;
if ($json_object['status'] == 'success') {
$json_object2 = $json_object['data'];
foreach ($json_object['data'] as $key => $value){
foreach ($value as $key2 => $value2){
echo $key2 . " : " . $value2 . "</br>";
}
}
} else {
echo $json_object['data'];
}
}
Try something like this
<?php
$result = '{"status":"success","data":{"lists":[{"id":"0032","name":"Stan","status":"active"},{"id":"0043","name":"David","status":"active"},{"id":"2323","name":"Robert","status":"pending"}]}}';
$json_object = json_decode($result, true);
foreach($json_object['data']['lists'] as $item)
{
echo $item['name']."<br>";
}
?>

Parsing JSON array with PHP foreach

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";
}
}

Categories