Get a specific value from an array [duplicate] - php

This question already has answers here:
How to parse JSON and access results
(3 answers)
Closed 1 year ago.
The custom field of one of the plugins contains an array of data. I get this data, but I can't figure out how to make it a readable view for output on the page.
Such data is recorded in the field:
{"1 filename.doc":{"name":"filename.doc","url":"https://example.com/filename.doc","file":"/var/www/example.com/filename.doc","type":"application/msword","size":50688}}
From this I need a name and a link to do something like this:
filename.doc
I am just learning and I will be grateful for any hint!

Well you have a json string so the first step would be to convert it to a PHP array using json_decode:
$jsonArray = json_decode($jsonString, true);
The json strucutre looks like it can describe mutlitple files so we will run a foreach to iterate over the entries in the json. Within the foreach we will create an html link using the data from the json:
foreach($jsonArray as $file=>$fileAttributes) {
echo ''.$fileAttributes['name'].'';
}
Here's a working example: https://3v4l.org/5TAWd

Related

json_decode for PHP joomla [duplicate]

This question already has answers here:
How to display json values that are in 2nd level of a multi-dim array?
(3 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
Need help. I am having trouble solving this json thingy. here's what is on my database
{"img":["images/logo2.png","images/logo.png"]}
I need to decode this images and show it like this
images/logo2.png
images/logo.png
A quick google search will give you this link: json_decode.
$someJson = '{"img":["images/logo2.png","images/logo.png"]}';
$encodeJson = json_decode($someJson, true);
echo $encodeJson['img'][0]; //images/logo2.png
echo $encodeJson['img'][1]; //images/logo.png
Hint: Having a look at the PHP-Manual is not prohibited. ;)
Maybe select your entry from database and put in a variable called $dataBaseEntry then copy paste this code?
$decoded = json_decode($dataBaseEntry, true); //true for associated array
$logos = $decoded['img'];
print_r($logos)

PHP Traverse JSON data [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have a JSON output that looks like this:
{"data":[{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]}
I am trying to access just the data within the square brackets, inside the data array?
I have tried $jsonVar['data'] and $jsonVar->data and neither of them are working.
Is there a way I can just get access to [{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]?
Try this example:
<?php
$data = '{"data":[{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]}';
$test = json_decode($data, true);
echo json_encode($test['data']);
?>
Output:
[{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]

Extract the values from the array which is inside the object [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
if i solve this i will have my desired result for any expert this will take few minutes to guess and i am stuck in this from last 2 hours searching and googling but couldn't found the one i want. Okay so here is the thing ...
I am sending data through ajax to my php by doing JSON.stringify and receving that that to my php in the form of this
{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}
All i want to do is to get the values and insert them to the separate variables.
You need to use json_decode to convert the string to array. You can pass the second parameter to this method to convert this to array instead of object. Then you can retrieve the data like
$string = '{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}';
$array = json_decode($string, true);
print_r($array[0][0]);
Here's the example of it https://repl.it/HhI8/1

can't access object in array PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
Have tried every variation I can think of to access an object property in an array. I'm getting some data back form an API which i'm storing in a variable called $userTokenValid:
$userTokenValid = [{"authTokenValid":1}];
i'm then trying to access the authTokenValid property like so:
echo json_decode($userTokenValid[0]->authTokenValid);
I appreciate this might be quite basic but can't spot where I have gone wrong.
$userTokenValid isn't valid php. However [{"authTokenValid":1}] is a valid json string.
$userTokenValid = '[{"authTokenValid":1}]';
you can decode it with
$json = json_decode($userTokenValid);
finally
echo $json[0]->authTokenValid;

parse multilevel json in php [duplicate]

This question already has answers here:
Trying to parse JSON with PHP
(2 answers)
Accessing JSON array after json_decode/multidimensional array
(3 answers)
Closed 8 years ago.
I am receiving a multilevel json like this:
{"id":"3",
"field2":"ssss",
"field3":[
{"field31":"wwwww",
"field32":"qqqq"},
{"field31":"wwwww",
"field32":"qqqq"},
{"field31":"wwwww",
"field32":"qqqq"},
]}
I am reading json values (first level) like this in php:
$json = file_get_contents('php://input');
$json_post = json_decode(utf8_decode($json), true);
$id = $json_post['id'];
But I don't know how to get the json in the second level. What is the simplest way? I just need to get the json in other variable like $json_post, I will read it in a for bucle to get each row of the json file
It's going to give you a PHP object if you run json_decode(utf8_decode($json));
echo $json_post->field3->field31;
EDIT: I missed that you were using the conversion to array. This is what the array would look like
echo $json_post['field3']['field31'];

Categories