Use php variable index for retriving data from json - php

This is my json.
In php
$json = json_decode($finalAppData, true); // decode the JSON into an associative array
//suppose this is $link = ['appInfo']['items'][0]['screen']['items'][0]['screen']['items'][0];
This code is not working.
echo $json .$link."['screen']['menuHeader']";
produces output as
Array['appInfo']['items'][0]['screen']['items'][0]['screen']['items'][0]['screen']['menuHeader'].
but i want text value that can be seen if i use simply
echo $json['appInfo']['items'][0]['screen']['items'][0]['screen']['items'][0]['screen']['menuHeader'];
How can use index which is stored in variable to output data from json in php.

This should be similar to what you want:
$json = json_decode($finalAppData, true);
$link = "['appInfo']['items'][0]['screen']['items'][0]['screen']['items'][0]";
# Method #1
eval("echo \$json${link}['screen']['menuHeader'];");
# Method #2
$item = "\$json${link}";
eval("echo ${item}['screen']['menuHeader'];");
eval() takes a string of PHP code and interprets it. In this case, the nested keys are stored as a string in $link and then concatenated with a string that will be interpreted into the $json array resulting in a string of PHP code that will be sent to eval() to be interpreted.

Related

Get values from JSON array using PHP

I run the following PHP after I submit a form and get the output shown below:
$data = json_encode($_POST);
// output
{"First_Name":"Fred"}
How do I use PHP to just display the value 'Fred'?
I tried echo $data['First_Name']; but this is blank.
You no need to encode your incoming $_POST data.
Just say:
echo $_POST['First_Name'];
If you get a json data, decode it into an array:
$data = '{"First_Name":"Fred"}';
$decoded = json_decode($data, true);
echo $decoded['First_name'];
First of all, don't know why you use json_encode on PHP Array, and try to access it like it's an array - because after json_encode it's a string.
You have to use json_decode($data, true) and then you can access it like $data['First_Name'] or try to access it directly without json_encode() by $_POST['First_Name']
The json_decode() function is used to decode or convert a JSON object to a PHP object.And try to put the object to decode in another variable to avoid errors
<?php
$obj = '{"First_Name":"Fred"}';
$data = json_decode($obj);
echo ($data->First_Name);
?>

How to get data from json_encoded data from mysql in php?

I am storing data in json format in mysql database table column like
table A
column-user_details
"{\"name\":\"sadasfsf\",\"phone\":\"7896521747\",\"address_1\":\"dvgsdsd\",\"state_name\":\"g\",\"city_name\":\"sdgds\",\"zip_code\":\"ghdfh\"}"
I am fetching data like
json_decode($variable, true);
<?php echo $variable['name'];?>
However I am getting error like
Illegal string offset 'name'
the first parameter of json_decode is not a parameter passing by reference, so if you want to get the json code as php array, you mast adding the result of json_decode into a variable.
<?php
$variable = "{\"name\":\"sadasfsf\",\"phone\":\"7896521747\",\"address_1\":\"dvgsdsd\",\"state_name\":\"g\",\"city_name\":\"sdgds\",\"zip_code\":\"ghdfh\"}";
$array = json_decode($variable, true);
echo $array['name'];
NB: json_decode() is a php code, so you must use it after <?php tag, not outside of it

How to parser Json result from base64_encode() in PHP

I have written a php script to decode a value using base64_decode() function. I want to store the parse the result into different variable. How do I achieve this??
$str = 'eyJ0eXAiO0IjoiNTAwMCIsInZhbGlkaXR5IjoiMzAifQjdubnmGHANidodnd';
$msisdn_64 = base64_decode($msisdn);
print_r($msisdn_64);
NB: For privacy sake the $str variable contains dummy value And this Does not use a token to decode
The code above outputs:
{"typ":"JWT","alg":"HS256"}{"sub":"456564685455","service":"000","created":20010809,"account_name":"Acct","iss":"Acft","exp":false,"amount":"000","validity":"30"}}�.'���A˕X=·&|�L�0�"����
I tried something like this $msisdn[1]->sub , $msisdn[0]->sub , and $msisdn->sub to the value in the second object but its not working. Please help
You can use the json_decode function
https://www.php.net/manual/en/function.json-decode.php
Example:
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
$str = 'eyJ0eXAiO0IjoiNTAwMCIsInZhbGlkaXR5IjoiMzAifQjdubnmGHANidodnd';
$msisdn_64 = base64_decode($msisdn);
print_r($msisdn_64);
Here you need to decode the string again like below.
$str = 'eyJ0eXAiO0IjoiNTAwMCIsInZhbGlkaXR5IjoiMzAifQjdubnmGHANidodnd';
$msisdn_64 = json_decode(base64_decode($msisdn),true);
print_r($msisdn_64);
remember to put the parameter true in json_decode() method, because it returns the result as an associative array.

how to fetch json fom url in php

I am trying to make a php page tp print json data for this i m using one paraeter for which i needed to fetch json from another url.I used the code given in other stackoverflow ans but it always giving 0.I tried everything but it always giving 0.My php code is:
<?php
if(isset($_POST['add']))
{
require_once('loginConnect.php');
$bookname=$_POST['bookname'];
$url = "http://example/star_avg.php?bookName=$bookname";
$json = file_get_contents($url);
$json_data = json_decode($json,TRUE);
echo 'data' + $json_data->results[0]->{'num'};
?>
My json data from other url is:
{"result":[{"avg":"3.9","num":"3"}]}
You see 0 printed because you're performing an addition + between the string data and a non-existent property. In PHP, to concatenate strings, do not use +; instead, use the dot . operator
In addition, because you're using true as the 2nd parameter to json_decode, what you get back is an array of arrays. Use the array notation [] rather than the object notation -> to access members.
$json_data = json_decode($json,TRUE);
$num = $json_data['result'][0]['num']; //<- array notation
echo 'data: '.$num; //prints data: 3
Live demo

PARSE json string to json array (from GET)

i'm tring to parse url's json string to an json array.
ISSUE: json_decode = empty
QUESTION: Does anyone see what am i doing wrong?
MY STEPS:
tests from browser:
.....send.php?{"contactName":"name1"},{"contactName":"name2"}
my php code:
1. $url = $_SERVER['QUERY_STRING'];
2. $urlStringDecoded = urldecode($url);
echo urlStringDecoded result ok:
{"contactName":"name1"},{"contactName":"name2"}
3. $json = json_decode($urlStringDecoded, true);
RESULT EMPTY
echo("$json");
Seems like your JSON is invalid. Wrap your current string within [ ] Brackets.
Eg.
<?php
$url = $_SERVER['QUERY_STRING'];
//echo $url;
$urlStringDecoded = urldecode($url);
echo $urlStringDecoded;
$json = json_decode("[".$urlStringDecoded."]", true);
echo "<pre>";
print_r($json);
echo "</pre>";
?>
As you are trying to convert json string to json, you should provide right format. From you example it looks like you are passing , comma separated json objects whereas it should be an array. Add [] around your string and then try, in other words make it an array.

Categories