相互减去数组值以创建新数组 [英] subtract array values from each other creating new array

查看:113
本文介绍了相互减去数组值以创建新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我有以下代码,该代码从表单中获取值并创建一个文本文件,其中列出了我的图像路径和持续时间. -

On my site I have the following code, which grabs values from a form and creates a text file with a list of my image paths and duration times. --

/*
This is for looping through the uploaded pictures
and sorting them and creating a text file.
*/

$vid_pix = get_post_meta($v_Id, 'vid_pix', false);

$data = "ffconcat version 1.0";
$line = '';

        usort( $vid_pix, function( $a, $b ){
            $aPor = (int) get_post_meta( $a, 'photo_order', true );
            $bPor = (int) get_post_meta( $b, 'photo_order', true );

            if ( $aPor === $bPor ) {
                return 0;
            }

            return ( $aPor < $bPor ) ? -1 : 1;
        } );

// this function removes the first value and shifts the array back so that key 1 = value 2, etc.

$Pt2 = [];
$n = count( $vid_pix );
for ( $i = 0, $j = 1; $i < $n; $i++, $j++ ) {
    $Pt2[] = ( $n === $j ) ? '6' :
        get_post_meta( $vid_pix[ $j ], 'photo_time', true );
}

        foreach ($vid_pix as $i => $vP ) {

$filename = basename( get_attached_file( $vP ));
$Por = get_post_meta($vP, 'photo_order', true);
$Pt = $Pt2[ $i ];

// try to determine the pic of the placeholder image
if ($vP === end($vid_pix))
        $last_img = $thepath.'/'.$filename;

// THIS IS THE LINE THAT OUTPUTS THE NUMBERS I NEED
$slide_dur = "\r\nduration ".$Pt;

$line .= "file '".$thepath."/".$filename."'".$slide_dur."\r\n"; 

// LAST LINE OF CONCAT TEXT FILE
$lastline = "file '".$last_img."'\r\nduration 2\r\nfile '".$last_img."'";

// PUT TOGETHER ALL THE LINES FOR THE TEXT FILE
$txtc = $data."\r\n".$line.$lastline;

// SAVE THE TEXT FILE
file_put_contents($thepath.'/paths.txt', $txtc);

然后使用5, 10, 13, 16, 6的输出看起来像这样-

Then the output using 5, 10, 13, 16, 6 looks like this --

ffconcat version 1.0
file 'home1.png'
duration 5
file 'home2.png'
duration 10
file 'home3.png'
duration 13
file 'home4.png'
duration 16
file 'home5.png'
duration 6 // last number in regards to my question
file 'home5.png'
duration 2
file 'home5.png'

我需要做的是将每个值与其之前的值相减,然后为该键创建一个新值.这些值将始终是动态的.

What I need to do though is subtract each value with the value before it, and making a new value for that key. The values will always be dynamic.

所以第一个键-5将保持5,它将保持不变.

So first key - 5 would stay 5, it would stay the same.

第二个键-10,将变为5,因为10-5.

The second key - 10, will become 5 because 10-5.

然后第三个键-13将变为3,因为10-13.

Then the third key - 13, will become 3 because 10-13.

第四个键-16将变为3,因为16-13.

The fourth key - 16, will become 3 because 16-13.

我不确定我是否解释正确,所以如果需要更多详细信息,请告诉我.

Im not sure if I explained that right so if any more details are needed let me know.

推荐答案

应该不要太难.我不确定混乱在哪里.如果要减去,就减去?因此,仅输出$Pt - $Pt2[$i-1],而不是输出$Pt.您可能想要添加一个条件,以在$i === 0时仅使用$Pt.

Shouldn't be too hard; I'm not sure where the confusion is. If you want to subtract, just subtract? So instead of outputting $Pt, just output $Pt - $Pt2[$i-1]. You'll probably want to add a condition to just use $Pt if $i === 0.

这篇关于相互减去数组值以创建新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆