首页 > 解决方案 > skip first value for variable inside of foreach loop php

问题描述

I have the following function on my site which grabs all the values from $vid_pix, and echos each them or any associated variables in a foreach loop.

Which works fine, however I have a variable - $Pt that also gets echoed out as well.

Right now though what I am trying to do is - skip the first value in $Pt. Also set a static value for the last instance since everything is being moved up 1, leaving the last with no value.

I've tried array_splice and unset, but it's not skipping the first $Pt value.

So if I had -

[vp 1]->[5]
[vp 2]->[10]
[vp 3]->[15]
[vp 4]->[20]

I would need -

[vp 1]->[10]
[vp 2]->[15]
[vp 3]->[20]
[vp 4]->[x]

(x = I would have to assign a static value for the last variable.)

My function (stripped down for simplicity)

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

foreach ($vid_pix as $vP) {
    $Pt = get_post_meta($vP, 'photo_time', false);
    array_splice($Pt, 0, 1);
    //unset($Pt[0]);
    $Pt = get_post_meta($vP, 'photo_time', true);
    echo $Pt;

    if (last -> $Pt) { // something like this for the last value
        $Pt = '5';
    }
 }

To put things into better context, here's the full code for the specific function I'm trying to achieve this within --

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

        foreach ($vid_pix as $vP) {
$filename = basename( get_attached_file( $vP ));
$Pt = get_post_meta($vP, 'photo_time', true);
$Por = get_post_meta($vP, 'photo_order', true);

$static_value=25;
$array=$Pt;

reset($array);//reset the internal pointer
while(false!==($key=key($array))&&null!==key($array)){//check for current key validity
    $next=next($array);//get the next value and move the pointer
    $array[$key]=$next&&isset($array[$key])?$next:$static_value;//assign the next value to the current key  if valid or the static value if false 
}

var_dump($Por);
var_dump($array);

// try to determine the pic of the placeholder image

if ($vP === end($vid_pix))
        $last_img = $thepath.'/'.$filename;


if ($vstyle === 'Custom') { // if custom timing is chosen

$slide_dur = "\r\nduration ".$Pt;

$filename = basename( get_attached_file( $vP ));
$line .= "file '".$thepath."/".$filename."'".$slide_dur."\r\n";


} else { // if custom timing is NOT chosen

$filename = basename( get_attached_file( $vP ));
$line .= "file '".$thepath."/".$filename."'".$slide_dur."\r\n";

}

$total_items = count($vid_pix);

if ($total_items > 1) { // if total items is more than one

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

$isitone = "";
$solopic = "";


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

} else { // if total items is less than one

$isitone = "true";
$solopic = "-loop 1 -probesize 10M -i ".$thepath."/".$filename;

}
}

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

UPDATE

var_dump results -

string(1) "7"
string(1) "2"
string(1) "6"
string(1) "9"

The following link contains the original code which saves the $Pt variable -- here

标签: phparrayswordpressforeachkey-value

解决方案


更新#1

尝试array_shift()像这样使用:

foreach ($vid_pix as $vP) {
    $Pt2 = get_post_meta($vP, 'photo_time', false);
    $Pt = array_shift( $Pt2 ); // get first value
    $Pt2[] = 'static value';
    var_dump( $Pt, $Pt2 ); // test
}

更新#2

如果每个图像/附件实际上只有一个 photo_time元数据(附件 ID 在$vid_pix数组中),那么这应该有效:

$Pt2 = [];
$n = count( $vid_pix );
for ( $i = 0, $j = 1; $i < $n; $i++, $j++ ) {
    $Pt2[] = ( $n === $j ) ? 'static value' :
        get_post_meta( $vid_pix[ $j ], 'photo_time', true );
}
echo implode( ', ', $Pt2 ); // test

更新#3

在上面/之前添加上面的($Pt2)代码,然后添加,并更改如下所示:foreach$i =>$Pt

foreach ($vid_pix as $i => $vP) {
  $filename = basename( get_attached_file( $vP ));
  $Pt = $Pt2[ $i ];
  ...
}

推荐阅读