extract an mumber from array [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
In mooodle I create a course via web service the output of array that I print via command print_r is:
[{"id":29,"shortname":"math1"}]
I wanna to extract just 29 of this text via PHP
how can id do it?

to do this you have to decode the array:
<?php
$json_array = ' [{"id":29,"shortname":"math1"}]';
$php_array = json_decode($json_array);
echo $php_array[0]->id;
And you can do a foreach loop if there is more data
Sandbox link

Related

How to extract decode JSON from REDCap Array with PHP [duplicate]

This question already has answers here:
Why does this PHP code just echo "Array"?
(6 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have an JSON Export from a REDCap and it started as a nested ARRAY. I use json_decode, but when I print the result I get an ERROR.
Here is the Array:
[{"record_id":"13","lastname":"Mustermann","first_name":"Json","dob":"1948-10-12","case_number":"1366613"}]
My Code is:
$deco = json_decode($json);
print $deco;
The line "echo" gives an error. What am I doing wrong?!
Thank you

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)

How to convert a response to an appropriate form? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I use Binance API.
The PHP function gets file contents of url:
https://api.binance.com/api/v1/klines?symbol=BTCUSDT&interval=4h&limit=1
I receive such a response:
[[1528545600000,"7635.01000000","7650.00000000","7566.00000000","7621.15000000","3246.04484200",1528559999999,"24674635.12120312",57351,"1932.28202000","14685353.70584041","0"]]
How to get the second value? (7635.01000000)
If you want to get the second value use this:
$receivedData = '[[1528545600000,"7635.01000000","7650.00000000","7566.00000000","7621.15000000","3246.04484200",1528559999999,"24674635.12120312",57351,"1932.28202000","14685353.70584041","0"]]';
$jsonDecodeData = json_decode($receivedData);
$secondValue = jsonDecodeData[0][1];
echo $secondValue; //7635.01000000

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

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