1

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.

4
  • 2
    the input is in single string or three different string? Commented Nov 8, 2017 at 9:10
  • 2
    This looks almost like JSON…!? json_decode it? Commented Nov 8, 2017 at 9:10
  • 1
    the input is the whol;e string ["0:12:23", 0.000000],["0:12:43", 0.000000],["0:13:03", 0.000000] Commented Nov 8, 2017 at 9:11
  • 2
    Then json_decode("[$data]") should do as a first step. Commented Nov 8, 2017 at 9:13

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

is print_r($matches[0]) returns the first time value?
@Psl no. it will return array of all time
You can do:- $final_time_array = $matches[0]; echo $final_time_array[0];
2

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]); 
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.