I need save $GLOBALS into a field in MySQL, but...
$GLOBALS is an ARRAY
When I try with some function as
function array_to_string($array){
$string = '';
code...
$string .= code...
code...
return $string;
}
$string = array_to_string($GLOBALS);
this "$string" grow and grow... (is infinite)
Any idea please?
you can try implode() function....
The implode() function returns a string from the elements of an array.
For example..
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
the output
Hello World! Beautiful Day!
Conversion to JSON is adviced.
$string=json_encode($array);
json_encode Returns a JSON encoded string on success or FALSE on failure.
$array_back=json_decode($string);
json_decode Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
Try to use utf8_encode and json_encode
$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);
Related
I have an array output with index as:
[{"0":"10:00PM","2":"12:00PM"}]
I want to convert this to a string something like:
[{"10:00PM","12:00PM"}]
How can I do this in PHP?
You can use preg_replace to replace string for following output.
$json = '[{"0":"10:00PM","2":"12:00PM"}]';
$result = preg_replace("/\"\d\"\:/","",$json);
echo $result;
Output
[{"10:00PM","12:00PM"}]
Live demo
Know more about preg_replace
You can just push your value into an array.
$data = array();
array_push($data,"10:00PM");
array_push($data,"12:00PM");
echo json_encode($data);
Output:
[{"10:00PM","12:00PM"}]
I want to decode a JSON string. However, the value that I got is something like this string(100) "{"data":{"type":"campaign-folders","id":"d208f3171a","attributes":{"name":"My Folder"}}}".
Could anyone tell me what should I do so that I can treat this variable as a usual string ?
Use json_decode http://php.net/manual/en/function.json-decode.php
$obj = json_decode($json);
var_dump($obj);
Use json_decode ,you can solve it.
var_dump(json_decode($json, true));//check this.
here to convert JSON string to Array
$someArray = json_decode($someJSON, true);
print_r($someArray);
echo $someArray[0]["name"];
and here to convert JSON to Object
$someObject = json_decode($someJSON);
print_r($someObject);
echo $someObject[0]->name;
I have this Json Object Below, I want to extract this data and output it in PHP
{"seat_booked":"A5","0":"A5","1":"A3"}
then get them into this format
$seat_booked = "'A5', 'A5', 'A3'";
How can I do this?
I hope you are looking for this, its very simple example by using json_decode():
$string = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = json_decode($string,true);
$resuiredString = '"'."'".implode("','", $decoded)."'".'"';
echo $resuiredString;
Result:
"'A5','A5','A3'"
Side Note:
I suggest you to learn about variable concatenation.
PHP Concatenation
Another solution:
$json = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = array_map(
function($val) {
return "'". $val."'";
},
array_values(json_decode($json, true))
);
To get an object from a json in php you can use json_decode has explained here.
But you have another problem, your json is wrong!
If you want to represent a single dimensional array you should at least do this
["A5","A5","A3"]
Finally, using json_decode:
$obj = json_decode('["A5","A5","A3"]');
var_dump($obj);
Also, you could do something like:
{"0":"A5","1":"A5","2":"A3"}
$obj = json_decode('{"0":"A5","1":"A3", "2": "A5"}', true);
var_dump($obj);
Edit:
It's not very clear from your question if you are trying to get back an object from a json or if you just want to get a string from it.
If what you need is an string then you don't even need json, you could do this by string manipulation and/or using regex.
But just for completeness, if a quoted comma separated string is what you need you can do this:
$array = json_decode('["A5","A5","A3"]');
$str = implode("','",$array);
$str = "'" . $str . "'";
var_dump($str);
I have the json data in following format
["0","0","0","0","0","0","0","2","5","4","3","0"]
I want to convert the above data in to following format using php
[0,0,0,0,0,0,0,2,5,4,3,0]
How can i do this using php
Thanks
You can use array_map to typecast all items in the array to an integer with the callback intval.
For integers in an array:
$array = array_map('intval', json_decode('["0","0","0","0","0","0","0","2","5","4","3","0"]'));
Retrieve JSON from array:
echo json_encode($array);
$string = '["0","0","0","0","0","0","0","2","5","4","3","0"]';
echo str_replace('"','', $string);
Output is:
[0,0,0,0,0,0,0,2,5,4,3,0]
I receive string "{success: false, errors: { reason: 'text text text' }}" by CURL, how to convert this string to array or object?
String '{"success": "false"....}' may be converted to object by json_decode, but I have string without qoutes.
Use this regex first (it adds quotes)
$json = preg_replace ('/(?<!")(?<!\w)(\w+)(?!")(?!\w)/u', '"$1"', $string);
After that, you can simply use json_decode()
$array = json_decode ($json);
Update
I found this script somewhere:
function json_fix_quotes ($string){
$string = str_replace("{",'{"',$string);
$string = str_replace(":'",'":"',$string);
$string = str_replace("',",'","',$string);
$string = str_replace("'}",'"}',$string);
return $string;
}
Try that instead of the regex