PHP convert json data to php array using json_decode - php

I insert data using json_decode to MySQL database like this:
["kevin","rode","shure"]
Now I need to convert using json_decode to php array like this:
$selected = array( 'kevin', 'rode', 'shure' );
how do can I convert this?

You can try this
$encoded = '["kevin","rode","shure"]';
$decoded = json_decode($encoded);// or directly pass the value here
print_r($decoded);
And you should get back the encoded array

Convert de data in a string, or get from $_GET or $_POST, it depends of you
$data = '["kevin","rode","shure"]';
$data = json_decode($data);
foreach($data as $name){
echo $name . "<br />";
}
and beware of double quotes

use json_decode with true param to force array output :
$selected = json_decode('["kevin","rode","shure"]', true);
NB : don't forget quotes and see documentation http://php.net/manual/en/function.json-decode.php

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 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 take out int value from JSON object?

I'm working with Laravel 5 right now and I have the following problem. I've got response from DB query:
[{"id":1}]
and I want to take out 1 as int or string. Any ideas?
I've tried to solve this like follows:
$json = (DB query);
$data = json_decode($json);
$final = $data[0]->id;
and response is :
json_decode() expects parameter 1 to be string, array given
This is all you need.
$response = json_decode($response); // Decode the JSON
$string = $response[0]->id; // Save the value of id var
As you say, the string you have is in JSON format, so you need to use json_decode to access it in PHP.
The square brackets refer to an array, and the braces refer to an object within that array, so what you're looking for is the id value of the first element (i.e. element 0) in the array.
<?php
$json = '[{"id":1}]';
$data = json_decode($json);
echo $data[0]->id;
// 1
Try this
$json_array = '[{"id":1}]';
$data = json_decode($json_array);
print_r($data); // To display the result array
You just need to decode it, if I'm not misunderstanding your questions.
$json = '[{"id":1}]';
$decodedObject = json_decode($json);
Then you can loop through the aray and do something with your data:
foreach($decodedObject as $key => $value) {
$id = $value->id;
}
If you're using Laravel, though, why not use Eloquent models?

PHP json_Encode an array of json_encode-ed arrays

Im not sure what is happening, but if i do
json_encode()
On a single array, i get valid json, but if i do something like
$ar['key'] = "name";
$array[] = json_encode($ar);
$json = json_encode($array);
It will return invalid json like so:
["{"key":"name"}"]
The expected outcome is
[{"key":"name"}]
I have searched for hours trying to find what is going on.
Due to lack of desired outcome, I can only assume you are trying to get a multidimensional array.
The correct way to achieve this would be to build an array of arrays, and then json_encode the parent array.
$data = array();
$data['fruits'] = array('apple','banana','cherry');
$data['animals'] = array('dog', 'elephant');
$json = json_encode($data);
Following this code, $json will have the following value
{"fruits":["apple","banana","cherry"],"animals":["dog","elephant"]}
It could then be parsed properly by javascript using jQuery.parseJSON()
Just json_encode the entire array.
$ar['key'] = "name";
$json = json_encode($ar);
json_encode returns a string, and json encoding a string will return a string.
Also it's json_encode, not $json_encode

How to loop through this json decoded data in PHP?

I've have this list of products in JSON that needs to be decoded:
"[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]"
After I decode it in PHP with json_decode(), I have no idea what kind of structure the output is. I assumed that it would be an array, but after I ask for count() it says its "0". How can I loop through this data so that I get the attributes of each product on the list.
Thanks!
To convert json to an array use
json_decode($json, true);
You can use json_decode() It will convert your json into array.
e.g,
$json_array = json_decode($your_json_data); // convert to object array
$json_array = json_decode($your_json_data, true); // convert to array
Then you can loop array variable like,
foreach($json_array as $json){
echo $json['key']; // you can access your key value like this if result is array
echo $json->key; // you can access your key value like this if result is object
}
Try like following codes:
$json_string = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$array = json_decode($json_string);
foreach ($array as $value)
{
echo $value->productId; // epIJp9
echo $value->name; // Product A
}
Get Count
echo count($array); // 2
Did you check the manual ?
http://www.php.net/manual/en/function.json-decode.php
Or just find some duplicates ?
How to convert JSON string to array
Use GOOGLE.
json_decode($json, true);
Second parameter. If it is true, it will return array.
You can try the code at php fiddle online, works for me
$list = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$decoded_list = json_decode($list);
echo count($decoded_list);
print_r($decoded_list);

Categories