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"}]
Related
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]);
}
?>
I have a string that looks like this:
{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}
i only need the value for the country_name from that string.
so I tried this:
$country = '{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}';
if (preg_match('#^country_name: ([^\s]+)#m', $country, $match)) {
$result = $match[1];
}
echo $result;
but there is nothing being echoed in the $result
Could someone please advise on this issue?
$country = json_decode('{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}');
echo $country->country_name;
What you have there is a JSON string.
JSON stands for JavaScript Object Notation.
PHP can decode it into an Array or Object via json_decode($string, FALSE);
The 2nd parameter by default is FALSE, which means it will convert the string into an object, which you can then access as I showed you above.
If for some reason you don't want to use JSON you can give the following a try. Note that using JSON is the recommended way doing this task.
$country = '{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}';
$temp = explode('"country_name":', $country); //Explode initial string
$temp_country = explode(',', $temp[1]); //Get only country name
$country_name = str_replace('"', ' ', $temp_country[0]); //Remove double quotes
echo $country_name;
Result:
Ireland
Try this:
<?php
$country=json_decode('{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}')
$result=$country->country_name;
echo $result;
?>
This looks like a json string. Read more about JSON.
Use it like this:
$foo = '{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}';
$bar = json_decode($foo, true);
echo $bar['country_name'];
This can be used with any key from that string (eg ip, city)
More about json_decode.
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 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
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;