Formating the json data in php - php

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]

Related

Convert the following array with index to a string

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

using PHP convert the string values to array and get the values

my $data returns following values.the gettype() of the following values is a String.
["0:12:23", 0.000000],["0:12:43", 0.000000],["0:13:03", 0.000000]
I want to convert the string vlaues to an array.and have to out the time values "0:12:23" , "0:12:43" and "0:13:03"
How can i conver the string values to array using php and get the time vlaues only.
I hope that your input is in single string.So do like below:-
<?php
$string = '["0:12:23", 0.000000],["0:12:43", 0.000000],["0:13:03", 0.000000]'; // i assume it's a single string
preg_match_all('/\d:\d{2}:\d{2}/',$string,$matches);
print_r($matches);
Output:- https://eval.in/895248
Or as #deceze said use json_decode("[$string]") like below:-
<?php
$string = '["0:12:23", 0.000000],["0:12:43", 0.000000],["0:13:03", 0.000000]';
$string_array=json_decode("[$string]");
$time_array = array_column($string_array,0);
print_r($time_array);
?>
Output:- https://eval.in/895251
Reference:-
preg_match_all()
json_decode()
array_column()
You can use json_decode to convert it into array structure.and use foreach loop to filter out .try below,
<?php
$string = '["0:12:23", 0.000000],["0:12:43", 0.000000],["0:13:03", 0.000000]';
$string_to_array=json_decode("[$string]");
foreach($string_to_array as $values){
print_r($values[0]);
}
?>

How convert $GLOBALS to string?

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);

How to convert string variable to an array in php?

I have one string variable as
$p_list="1,2,3,4";
i want to convert it to an array such as
$a[0]='1';
$a[1]='2';
$a[2]='3';
$a[3]='4';
how to do this in php?
Use explode.
$p_list = "1,2,3,4";
$array = explode(',', $p_list);
See Codepad.
Try explode $a=explode(",","1,2,3,4");
Try this:
In PHP, explode function will convert string to array, If string has certain pattern.
<?php
$p_list = "1,2,3,4";
$noArr = explode(",", $p_list);
var_dump($noArr);
?>
You will get array with values stores in it.
Thanks

convert url string to array php

I pass with jquery ajax as data array the following structure
$_POST['data'][0] = 'results[]=stein&results[]=schere&results[]=stein&results[]=schere&results[]=stein'
$_POST['data'][1] = '9b2c1230757e4354b384c5c93e8e8f26'
How do I say to php to interpret $_POST['data'][0] as array. What I would like to get is array(1 =>'stein', 2=>'schere'...)
Use parse_str() — Parses the string into variables
$str = "results[]=stein&results[]=schere&results[]=stein&results[]=schere&results[]=stein";
parse_str($str, $output);
echo $output['results'][0]; // stein
Live CodePad
use parse_str()..
parse_str($_POST['data'][0]);
print_r($results);
echo $results[0]; //stein;
echo $results[1]; //schere;

Categories