I want to convert a string like this code below to array, How can I do this plz?
$icon = '{``font``:``feather``,``icon``:``feather-facebook``}';
Is there any way by using json_decode or something like this?
Thanks.
The input string is not proper json format
When you correct json syntax then you can just json_decode it
$icon = '{"font":"feather","icon":"feather-facebook"}';
$array = json_decode($icon);
var_dump($array);
Related
Currently i am facing issue of replace string , I used postman & passing string in postman like [{"id":"115","flag":"1","qty":"3","size":"10"}] as a parameters but when i print string i am getting output like [{\"id\":\"115\",\"flag\":\"1\",\"qty\":\"3\",\"size\":\"10\"}] , So i want to only remove '\' from string i have tried following code but not work.
$fliesid_in_store = $_REQUEST['fliesid_in_store'];
echo $res = preg_replace("/[^a-zA-Z]/", "", $fliesid_in_store);
Have you tried stripslashes.
$fliesid_in_store = $_REQUEST['fliesid_in_store'];
echo stripslashes($fliesid_in_store);
The string you mentioned is in json format
$json = [{"id":"115","flag":"1","qty":"3","size":"10"}] //this is json
Assign that to variable and decode it.
$string = json_decode($json,TRUE) this give result in array format
In your case
$string = json_decode($_REQUEST['fliesid_in_store'],TRUE);
need your help on this one...
I'm trying to create a code that will get a .txt file and convert all text content to json.
here's my sample code:
<?php
// make your required checks
$fp = 'SampleMessage01.txt';
// get the contents of file in array
$conents_arr = file($fp, FILE_IGNORE_NEW_LINES);
foreach($conents_arr as $key=>$value)
{
$conents_arr[$key] = rtrim($value, "\r");
}
$json_contents = json_encode($conents_arr, JSON_UNESCAPED_SLASHES);
echo $json_contents;
?>
I already got the result when i tried to echo the $json_contents
["Sample Material 1","tRAINING|ENDING","01/25/2018 9:37:00 AM","639176882315,639176882859","Y,Y","~"]
but when I tried to echo using like this method $json_contents[0]
I only got per character result.
Code
Result
hope you can help me on this one..
thank you
As PHP.net says
"Returns a string containing the JSON representation of the supplied value."
As you are using $json_contents[0] this will return the first char of the json string.
You can do this
$conents_arr[0]
Or convert your json string to PHP array using
$json_array = json_decode($json_contents, true);
echo $json_array[0];
It is happening because $json_contents is a string. It might be json string but it's string so string properties will apply here and hence when you echo $json_contents[0] it gives you first character of the string. You can either decode the encoded json string to object like below:
$json = json_decode($json_contents);
echo $json[0];
or echo it before the json_encode:
echo $conents_arr[0];
$json_contents = json_encode($conents_arr, JSON_UNESCAPED_SLASHES);
json_encode() function takes an array as input and convert it to json string.
echo $json_contents; just print out the string.
if you want to access it you have to decode the JSON string to array.
//this convert array to json string
$json_contents = json_encode($conents_arr, JSON_UNESCAPED_SLASHES);
//this convert json string to an array.
$json_contents = json_decode($json_contents, true);
//now you can access it
echo $json_contents[0];
I have a string like this:
$str = '[{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABB0pg6HTwdv7EqUBAAEC","file_size":1347,"file_path":"photos\/file_2.jpg","width":90,"height":75},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABIMbRhad2WVdE6UBAAEC","file_size":17588,"width":320,"height":265},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABHSo-WKlRRfBEaUBAAEC","file_size":18480,"width":330,"height":273}]';
How can I access items in it?
I can use regex to select them, something like /"file_id":"(.*?)"/. But that's not clean at all. Is there any approach to make a array (or an object) of string above?
It's a json string.
You need to decode it with json_decode.
The second argument (true) is to make it an array.
$str = '[{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABB0pg6HTwdv7EqUBAAEC","file_size":1347,"file_path":"photos\/file_2.jpg","width":90,"height":75},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABIMbRhad2WVdE6UBAAEC","file_size":17588,"width":320,"height":265},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABHSo-WKlRRfBEaUBAAEC","file_size":18480,"width":330,"height":273}]';
$arr = json_decode($str, true);
Var_dump($arr);
https://3v4l.org/9BFIC
Explode(“,{”, $str); will work for the above.
You will get array value for each file.
My problem is that after exploding a string I get this: %C4%84 value
but I should get Ą. I can convert it using preg_replace, but is there simple way to convert ?
$band_song = explode('/',$url,6);
echo $band5=$band_song[3].' '.$band_song[4]);
You can simply urlencode your variables.
$var = urlencode($original_var);
EDIT
You'd want decode, not encode sorry
$var = urldecode($original_var);
Problem:
Converting a PHP string to a JSON array.
I have a string in PHP that looks like this:
intelligence skin weight volume
Desired output:
Is there a way in PHP where I can convert it so it looks like this instead:
["skin", "intelligence", "weight", "volume"]
I looked at json_encode() but that only put double quotes around the keywords.
If you want to create JSON array, you have to first explode your input string into an array.
Try with:
$input = 'intelligence skin weight volume';
$output = json_encode(explode(' ', $input));
first explode the string based on space. then u get an array containing individual words.then json_encode the array
$string="intelligence skin weight volume";
$array=explode(' ',$string);
$json=json_encode($array);
Check json_encode
This function would expect array and will convert array into json. Then use json_decode() to revert json to an array
$str="intelligence skin weight volume";
$arr=explode(' ',$str);
$json=json_encode($arr);
explode() used to split a string by a delimiter(in this senarion it is " ") Now you can encode the returend array as json.
Use json_encode
$jsonVal = json_encode(explode(' ', "intelligence skin weight volume"));