PHP - 为每个项目创建唯一的 ID 号
PHP - Create unique id number for each item
我有一个图像数组循环,它成功循环了我的图像项。但是,我需要为每个项目的 'data-slide-index' 属性指定一个唯一编号,从 0 开始,为每个后续项目添加 1(如果总共有 10 个图像,则为 0 - 9)。我意识到这可能很简单,但我是一个 PHP 新手,所以非常感谢关于最好、最简单的方法的提示。
<?php $images = get_post_meta(get_the_ID(), "images", true);
$images = unserialize($images);
foreach($images as $image) {
$ar[] = array("order" => $image['order'], "img_id" => $image['image_id'], "desc" => $image["desc"]);
}
asort($ar);
foreach($ar as $item) {
$image_id = $item['img_id'];
$media_med = wp_get_attachment_image_src( $image_id, "medium", false);
$media_full = wp_get_attachment_image_src( $image_id, "full", false);
echo "<a data-slide-index='" . $count ."' href=''><img data-title='" . $item["desc"] . "' data-big='". $media_full[0] . "' src='" . $media_med[0] . "'></a>";
} ?>
<?php $images = get_post_meta(get_the_ID(), "images", true);
$images = unserialize($images);
foreach($images as $image) {
$ar[] = array("order" => $image['order'], "img_id" => $image['image_id'], "desc" => $image["desc"]);
}
asort($ar);
//Create count variable
$i=0;
foreach($ar as $item) {
$image_id = $item['img_id'];
$media_med = wp_get_attachment_image_src( $image_id, "medium", false);
$media_full = wp_get_attachment_image_src( $image_id, "full", false);
//assign the number to the slide index
echo "<a data-slide-index='".$i."' href=''><img data-title='" . $item["desc"] . "' data-big='". $media_full[0] . "' src='" . $media_med[0] . "'></a>";
//plus up the variable for each loop item
$i++;
} ?>
我有一个图像数组循环,它成功循环了我的图像项。但是,我需要为每个项目的 'data-slide-index' 属性指定一个唯一编号,从 0 开始,为每个后续项目添加 1(如果总共有 10 个图像,则为 0 - 9)。我意识到这可能很简单,但我是一个 PHP 新手,所以非常感谢关于最好、最简单的方法的提示。
<?php $images = get_post_meta(get_the_ID(), "images", true);
$images = unserialize($images);
foreach($images as $image) {
$ar[] = array("order" => $image['order'], "img_id" => $image['image_id'], "desc" => $image["desc"]);
}
asort($ar);
foreach($ar as $item) {
$image_id = $item['img_id'];
$media_med = wp_get_attachment_image_src( $image_id, "medium", false);
$media_full = wp_get_attachment_image_src( $image_id, "full", false);
echo "<a data-slide-index='" . $count ."' href=''><img data-title='" . $item["desc"] . "' data-big='". $media_full[0] . "' src='" . $media_med[0] . "'></a>";
} ?>
<?php $images = get_post_meta(get_the_ID(), "images", true);
$images = unserialize($images);
foreach($images as $image) {
$ar[] = array("order" => $image['order'], "img_id" => $image['image_id'], "desc" => $image["desc"]);
}
asort($ar);
//Create count variable
$i=0;
foreach($ar as $item) {
$image_id = $item['img_id'];
$media_med = wp_get_attachment_image_src( $image_id, "medium", false);
$media_full = wp_get_attachment_image_src( $image_id, "full", false);
//assign the number to the slide index
echo "<a data-slide-index='".$i."' href=''><img data-title='" . $item["desc"] . "' data-big='". $media_full[0] . "' src='" . $media_med[0] . "'></a>";
//plus up the variable for each loop item
$i++;
} ?>