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