如何将数组元素的索引动态存储到变量中?

时间:2022-06-21 15:42:16

I'm trying to make a photo gallery with multiple galleries listed that I need to paginate.

我正在尝试制作一个列有多个画廊的照片库,我需要对其进行分页。

So, this variable

所以,这个变量

$galleries = Photograph::find_by_sql($sql);

$ galleries = Photograph :: find_by_sql($ sql);

holds an array:

持有一个数组:

Array
(
[0] => Photograph Object
    (
        [id] => 
        [gallery_name] => candies
    )

[1] => Photograph Object
    (
        [id] => 
        [gallery_name] => icecream
    )

[2] => Photograph Object
    (
        [id] => 
        [gallery_name] => pastries
    )

[3] => Photograph Object
    (
        [id] => 
        [gallery_name] => chocolate
    )
)

(I've shortened it for convenience)

(为方便起见,我缩短了它)

I'm using these two variables to set a selected gallery:

我正在使用这两个变量来设置选定的图库:

$newest_gallery = reset($galleries);
$gallery_name = (isset($_GET['subj']) ? $_GET['subj'] : $newest_gallery->gallery_name);

I was able to set the selected gallery by using the gallery_name, however, I'm unable to paginate the galleries since I need to somehow dynamically store a numerical index of an array element (which holds the gallery_name) into a variable in order to create the pagination function, since I need an integer value for this purpose. So, I basically need to get the index of an array element. Is there a way to do this?

我能够使用gallery_name设置选定的图库,但是,我无法对图库进行分页,因为我需要以某种方式将数组元素(包含gallery_name)的数字索引动态存储到变量中以便创建分页功能,因为我需要一个整数值来达到这个目的。所以,我基本上需要获取数组元素的索引。有没有办法做到这一点?

2 个解决方案

#1


0  

If I understand your question correctly, you have to make a traverse over the array:

如果我正确理解你的问题,你必须遍历数组:

## subj is set - search for it in the array of the objects
if (isset($_GET['subj'])) {
  foreach ($galleries as $id => $gallery) {
    if ($gallery->gallery_name == $_GET['subj']) {
       ## we've found a match - note the id and the name
       $gallery_name = $_GET['subj'];
       $gallery_id =$id;
       break;
    }
  }
}
## if subj was not specified or we found no match in the array 
if (!isset($gallery_id)) {
  reset($galleries);
  $gallery_id = key($galleries);
  $gallery_name = $galleries[$gallery_id]->gallery_name;
}

#2


0  

Just loop through your array of galleries and add set the value for id yourself:

只需遍历您的图库数组并自行添加设置id的值:

$i = 0;
foreach($galleries as $gallery){
    $gallery['id'] = $i;
    $i++;
}

#1


0  

If I understand your question correctly, you have to make a traverse over the array:

如果我正确理解你的问题,你必须遍历数组:

## subj is set - search for it in the array of the objects
if (isset($_GET['subj'])) {
  foreach ($galleries as $id => $gallery) {
    if ($gallery->gallery_name == $_GET['subj']) {
       ## we've found a match - note the id and the name
       $gallery_name = $_GET['subj'];
       $gallery_id =$id;
       break;
    }
  }
}
## if subj was not specified or we found no match in the array 
if (!isset($gallery_id)) {
  reset($galleries);
  $gallery_id = key($galleries);
  $gallery_name = $galleries[$gallery_id]->gallery_name;
}

#2


0  

Just loop through your array of galleries and add set the value for id yourself:

只需遍历您的图库数组并自行添加设置id的值:

$i = 0;
foreach($galleries as $gallery){
    $gallery['id'] = $i;
    $i++;
}