问题描述:

I have an array of subtitles which have start_time, end_time and text i want to make changes in it.

This is my array

Array

(

[0] => stdClass Object

(

[start_time] => 00:00:01,280

[end_time] => 00:00:03,109

[text] => Previously on "Pretty Little Liars..."

)

[1] => stdClass Object

(

[start_time] => 00:00:03,110

[end_time] => 00:00:04,780

[text] => Lesli? What are you doing in Rosewood?

)

[2] => stdClass Object

(

[start_time] => 00:00:04,790

[end_time] => 00:00:06,949

[text] => You mention my name to anyone ever again

)

[3] => stdClass Object

(

[start_time] => 00:00:06,950

[end_time] => 00:00:08,219

[text] => and you'll live to regret it.

)

[4] => stdClass Object

(

[start_time] => 00:00:08,220

[end_time] => 00:00:09,749

[text] => (Aria): We went to a junkyard to take some pictures

)

);

I want to change

start_time in startTime

end_time in endTime

text in data

and you can see this format (00:00:01,280) time in start_time and

end_time i want to change this time in milliseconds i have already

create a function which convert this time into milliseconds

function milliSeconds($time){

$string = "00:38:42,689";

$time = explode(":", $string);

$hour = $time[0] * 60 * 60 * 1000;

$minute = $time[1] * 60 * 1000;

$second = explode(",", $time[2]);

$sec = $second[0] * 1000;

$milisec= $second[1];

$result = $hour + $minute + $sec + $milisec;

return $result;

}

I hope you understand what i want to change in my array after changes i want my array like this

Result

Array

(

[0] => stdClass Object

(

[startTime] => 324324324

[end_time] => 234234234

[data] => Previously on "Pretty Little Liars..."

)

[1] => stdClass Object

(

[startTime] => 234234334

[endTime] => 34234234

[data] => Lesli? What are you doing in Rosewood?

)

[2] => stdClass Object

(

[startTime] => 32423423

[endTime] => 23423423

[data] => You mention my name to anyone ever again

)

[3] => stdClass Object

(

[startTime] => 4345345345

[endTime] => 34234234234

[data] => and you'll live to regret it.

)

[4] => stdClass Object

(

[startTime] => 342342343

[endTime] => 4543534534

[data] => (Aria): We went to a junkyard to take some pictures

)

);

I am sorry i have putting wrong milliseconds in startTime and endTime just describe.

I hope i have describe what i want. Thanks

网友答案:

You can use a custom function like:

function convertKeysToCamelCase($apiResponseArray) {
    $keys = array_map(function ($i) use (&$apiResponseArray) {
    if (is_array($apiResponseArray[$i]))
     $apiResponseArray[$i] = $this->convertKeysToCamelCase($apiResponseArray[$i]);
     $parts = explode('_', $i);
       return array_shift($parts) . implode('', array_map('ucfirst', $parts));
   }, array_keys($apiResponseArray));
   return array_combine($keys, $apiResponseArray);
}
网友答案:

If you are getting this from database, you can make it easier by changing your SELECT query, else use below code, where $input is the array you shown here and $result is your required array, if you want to change current array replace $result with $input.

Use array_map(),

$result = array_map(function($v){
            return (object)array(
                    'startTime' => milliSeconds($v->start_time),
                    'end_time' => milliSeconds($v->end_time),
                    'data' => $v->text
                );
        }, $input);
网友答案:

You probably want to convert between different subtitle formats. There is easier way to do that https://github.com/mantas-done/subtitles

$subtitles = Subtitles::convert('subtitles.srt', 'subtitles.vtt');

But if you still want to get time as (float):

$array = Subtitles::load('subtitles.srt')->getInternalFormat();
print_r($array);

It would print:

Array
(
    [0] => Array
    (
        [start] => 137.44
        [end] => 140.375
        [lines] => Array
            (
                [0] => Senator, we're making
                [1] => our final approach into Coruscant.
            )
    )

    [1] => Array
        (
            [start] => 140.476
            [end] => 142.501
            [lines] => Array
                (
                    [0] => Very good, Lieutenant.
                )
        )
)
相关阅读:
Top