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]);
}
?>
Related
I have one parameter every time it comes from the request object in a standard order,I want to store that string into an array by splitting \for that i am using explode function it's throwing an error,please help me to explode with backslash
$obj = "App\Http\Data\User";
$array = explode("\",$obj);
You can use this:
$obj = "App\Http\Data\User";
$array = explode("\\",$obj);
print_r($array);
example here
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"}]
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 1 year ago.
I have values like
"34,37"
and I want to create array using this values, array like
array('34','37')
How to create such array if I have values.
hope this will help you :
you can use explode function if you have values as string;
$string = '34,37';
$data = explode(',',$string):
print_r($data); /*output array*/
for more : http://php.net/manual/en/function.explode.php
If you have a string like this:
$str = "1,2,3,4,5,6";
And you want to convert it into array, just use explode()
$myArray = explode(',', $str);
var_dump($myArray);
Have a look here for more information
As per my comment, you should use preg_split function. for more details about preg_split function please read PHP manual and also you can use explode function Explode function PHP manual
<?php
$string = '34,37';
$keywords = preg_split("/[\s,]+/", $string);
//OR $keywords = preg_split("/,/", $string); separated by comma only
print_r($keywords);
you can check your desired Output here
Try this,
$val = "34,37"
$val = explode(',', $val);
print_r($val);
output of above array:
Array
(
[0] => 34,
[1] => 37
)
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