【发布时间】:2019-02-16 01:47:12
【问题描述】:
我的网站上有以下函数,它从$vid_pix 中获取所有值,并echo 每个它们或foreach 循环中的任何相关变量。
这很好用,但是我有一个变量 - $Pt,它也会回显。
虽然我现在要做的是 - 跳过$Pt 中的第一个值。还要为最后一个实例设置一个静态值,因为所有内容都向上移动 1,最后一个实例没有任何值。
我试过array_splice 和unset,但它没有跳过第一个$Pt 值。
如果我有 -
[vp 1]->[5]
[vp 2]->[10]
[vp 3]->[15]
[vp 4]->[20]
我需要 -
[vp 1]->[10]
[vp 2]->[15]
[vp 3]->[20]
[vp 4]->[x]
(x = 我必须为最后一个变量分配一个静态值。)
我的功能(为简单起见,已精简)
$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';
}
}
为了更好地理解,这里是我试图在其中实现的特定功能的完整代码--
/*
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);
更新
var_dump 结果-
string(1) "7"
string(1) "2"
string(1) "6"
string(1) "9"
以下链接包含保存$Pt变量的原始代码——here
【问题讨论】:
-
array_shift() 怎么样?
-
或者不要丢弃它,而是使用 for 循环,将数组从第二个元素迭代到最后一个元素。
-
@dnFer 因为那样我将不得不改变其他的事情,所以我尽量保持简单。
-
您的示例与您的解释不符。您的示例暗示您要跳过第一个元素
vp 1,但您的解释表明get_post_meta的输出不是您想要的。请澄清。 -
不跳过$Vp的第一个元素,跳过第一个$Pt。 $Vp 是一个 ID 号,$Pt 是在与该 ID 关联的表单中输入的数字值。所以我希望 ID # 1 具有来自 ID # 2 的 $Pt 值......现在有意义吗? @DevDonkey
标签: php arrays wordpress foreach key-value