Extracting information from an array in php - php

Can any one tell me how to extract the 'honda' value out of the array below using php?
{
"version": "1.0",
"encoding": "UTF-8",
"entry": {
"name": "bob",
"car": {
"model": "honda"
}
}
}

This looks like a json encoded object. What you could do is:
$info = json_decode($data, true); //where $data has your stuff from the question
$carModel = $obj['entry']['car']['model'];

If you have all that in a variable called "obj", then
$obj = '{ "version": "1.0", "encoding": "UTF-8", "entry": { "name": "bob", "car": { "model": "honda" } } }';
$arr = json_decode($obj, true);
echo $arr['entry']['car']['model'];
should be 'honda'
EDITED: Per Omar below, you do need the true as the second param in json_decode. He should get selected as the correct answer.

Use json_decode as: http://php.net/manual/fr/function.json-decode.php
<?php
$json = '{"version": "1.0","encoding": "UTF-8","entry": {"name": "bob","car": {"model": "honda"} } }';
$tab = json_decode($json, true);
$honda = $tab['entry']['car']['model'];
var_dump($honda);
// or with object:
$obj = json_decode($json);
$honda = $obj->entry->car->model;
var_dump($honda);

Related

how to get this json output into php array

I want this json output into php array:
{
"data": [
{
"id": "1",
"name": "Roger",
"country": "Switzerland",
"city": "Basel"
},
{
"id": "2",
"name": "Rafael",
"country": "Spain",
"city": "Madrid"
},
]
}
I am trying this:
$arrResult = array();
$arrResult['data'] = array();`
while($row = mysqli_fetch_assoc($result)){`
$id = $row['id'];
$name = $row['name'];
$country = $row['country'];
$arrResult['data'][] = array(
'id'=> $id,
'name'=> $name,
'country'=> $country,
);
}
echo json_encode($arrResult, JSON_FORCE_OBJECT);
I want the same output as given into JSON Format from a php array.
I have found your problem. Remove the comma in the json (explained in the code).
<?php
$json = '{
"data": [
{
"id": "1",
"name": "Roger",
"country": "Switzerland",
"city": "Basel"
},
{
"id": "2",
"name": "Rafael",
"country": "Spain",
"city": "Madrid"
}, <--- THIS IS A PROBLEM
]
}';
/* in order for this function to work you need to delete that comma.
Because there isn't another list in json so comma should not be there */
$arrResult = json_decode($json, true);
print_r($arrResult);
You can use json_decode() if you are using PHP >= 5.2.0
Here is a sample on how to use json_decode()
<?php
// JSON string
$someJSON = '[{"name":"Shriram","gender":"male"},{"name":"Lexa","gender":"female"},{"name":"John Doe","gender":"male"}]';
// Convert JSON string to Array
$someArray = json_decode($someJSON, true);
print_r($someArray); // Dump all data of the Array
echo $someArray[0]["name"]; // Access Array data
// Convert JSON string to Object
$someObject = json_decode($someJSON);
print_r($someObject); // Dump all data of the Object
echo $someObject[0]->name; // Access Object data
?>
you can use the json_decode function.
example;
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
echo $json;
$array = json_decode($json, true);
print_r($array);
?>
What I've understood is that you want to encode your array into the json format shown.
Use json_encode($arrResult) instead of json_encode($arrResult, JSON_FORCE_OBJECT).
JSON_FORCE_OBJECT outputs an object rather than an array.

PHP JSON decode without foreach

how can I get the "avatarmedium" from i json without a foreach Loop?
Thats my current Code:
$json = '{
"response": {
"players": [
{
"steamid": "76561198019025166",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d3/d3ca76e0f1e8d071e826bc3baeebd0e7215e50e2_medium.jpg"
}
]
}
}';
$result = json_decode ($json);
echo $json->response[0]->players->profileurl;
Thank you
with best regards
<?php
$json = '{
"response": {
"players": [
{
"steamid": "76561198019025166",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d3/d3ca76e0f1e8d071e826bc3baeebd0e7215e50e2_medium.jpg"
}
]
} }';
$result = json_decode ($json);
echo $result->response->players[0]->avatarmedium;

PHP Json Parsing 6

How do I pull the 76561198216468627 from
{ "response": { "steamid": "76561198216468627", "success": 1 } }
I've tried this but it pulls 1 instead of the id
foreach ($parsed_json->{'reponse'} as $item) {
$steamid = $item;
}
You need to decode the JSON string into either a PHP object or array. Here's how to do it each way:
ARRAY METHOD
$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }';
$json = json_decode($jsonString, true);
foreach($json as $item)
{
$steamid = $item['steamid'];
}
OBJECT METHOD
$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }';
$json = json_decode($jsonString);
foreach($json as $item)
{
$steamid = $item->steamid;
}
Try this:
$json = '{
"response": [{ "steamid": "76561198216468627", "success": 1 }]
}';
$decode = json_decode($json);
echo $decode->response[0]->steamid;
Then you can get 76561198216468627

How to decode this json with foreach

This is the JSON
{
"circuit_list": [
{
"_id": "58c0f378a986f808cdaf94cf",
"aggregation": {
"dev_name": "ME2-D2-BOO",
"port": {
"desc": "AKSES_SITE_SITE-TSEL_ME2-D2-BOO#1/2/5_200M_BOO082#CIPAKUBOO534",
"name": "1/2/5"
}
},
"area": "AREA 2",
"site_id": "N/A",
"site_name": "N/A"
},
{
"_id": "58c0f378a986f808cdaf94d0",
"aggregation": {
"dev_name": "ME2-D2-BOO",
"port": {
"desc": "AKSES_SITE_SITE-TSEL_ME2-D2-BOO#1/2/5_200M_BOO082#CIPAKUBOO534",
"name": "1/2/5"
}
},
"area": "AREA 2",
"site_id": "N/A",
"site_name": "N/A"
}
}
I already try with this code
$json = json_decode($url, true);
foreach($json as $value)
{
$_id = $value->_id;
}
it didn't work. Please help, I need to get the value to show them on the view. Did I do this wrong? this json is difficult because i didn't understand the structure.
I usually decode json with format
[{"id":"1","name":"faisal"}]
like this and with my foreach it's working.
If the second parameter of json_decode is true, the function will return an array instead of an object. Also, you would need to loop over the circuit_list property of the object.
$json = json_decode($url); // <- remove the parameter
foreach($json->circuit_list as $value) // <- loop over circuit_list
{
$_id = $value->_id;
}
<?php
$json = json_decode($url,true);
foreach($json['circuit_list'] as $value)
{
$id = $value['_id'];
}
?>

Php Json Get inside array

This is My json How can get "download" string
{
"total": 1,
"items": [
{
"id": "1186e1472bc1c",
"title": "title,
"desc": "",
"download": "www.google.com",
}
]
}
This is My code:
$response = file_get_contents($nagu);
$obj = json_decode($response, true);
$download = $obj ['items'];
echo $download;
I want output "www.google.com".
$download = $obj ['items'][0]['download'];
echo $download;

Categories