如何在Array中进行更改并保存

时间:2015-07-16 12:40:42

标签: php arrays

我有一个字幕数组,包含start_time,end_time和text我想在其中进行更改。

这是我的数组

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

我想改变

  

startTime中的start_time

     在endTime中

end_time

     

数据中的文字

     

你可以在start_time和。中看到这种格式(00:00:01,280)   end_time我想以毫秒为单位更改此时间   创建一个将此时间转换为毫秒的函数

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

我希望你能在我希望我的数组改变之后理解我想在数组中改变的内容

结果

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

对不起,我在startTime中输入了错误的毫秒,而endTime只是描述。

我希望我已经描述了我想要的东西。感谢

3 个答案:

答案 0 :(得分:0)

您可以使用以下自定义功能:

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

答案 1 :(得分:0)

如果您是从数据库获取此信息,则可以通过更改SELECT查询来简化此操作,否则请使用以下代码,其中$input是您在此处显示的数组,而$result是您需要的数组,如果您想要使用$result更改当前数组替换$input

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

答案 2 :(得分:0)

您可能希望在不同的字幕格式之间进行转换。有更简单的方法https://github.com/mantas-done/subtitles

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

但如果你仍然希望得到时间(浮动):

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

它会打印出来:

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