计算自索引和数组以来哪个位置是我的图片

计算自索引和数组以来哪个位置是我的图片

问题描述:

$quarter is a array who contain the list of all quarter in my database and each quarter have a total number of picture in each quarter (A, B, C)

$quarter = ['A' => '2', 'B' => '5', 'C' => '3'];

Quarter A has 2 pictures (0001 and 0002) Quarter B has 5 pictures (0001 to 0005) Quarter C has 3 pictures (0001 to 0003)

The index is the total position of the picture without worry about the quarter

$index = 3;

And I would like to return me

['quarter' => 'B', 'index' => 1]

Because the third picture is in quarter B, The quarter A has two first picture and the quarter B begin to third pictures. I would like to create algo who return the position of my picture (quarter with index of its quarter) since the number of pictures

Example 2

$index = 7;
$begin = 'B'

It return me

['quarter' => 'C', 'index' => 2]

Because it begin the calcul in quarter B. Of course this example has alpha quarter but In fact my quarters is a number.

Thanks

$ quarter是一个数组,其中包含我数据库中所有季度的列表,每个季度都有一个图片总数 在每个季度(A,B,C) p>

  $ quarter = ['A'=>  '2','B'=>  '5','C'=>  '3']; 
  code>  pre> 
 
 

A区有2张图片(0001和0002) Quarter B有5张图片(0001到0005) Quarter C有3张图片( 0001至0003) p>

索引是图片的总位置,无需担心四分之一 p>

  $ index = 3; 
   code>  pre> 
 
 

我想回复我 p>

  ['quarter'=>  'B','index'=>  1] 
  code>  pre> 
 
 

因为第三张图片位于B区,A区有两张第一张图片,四分之一B开始第三张图片。 我想创建 自图片数量以来返回图片位置(季度指数为四分之一)的算法 p>

示例2 p>

  $ index  = 7; 
 $ begin ='B'
  code>  pre> 
 
 

它返回我 p>

  ['quarter'=  >  'C','index'=>  2] 
  code>  pre> 
 
 

因为它在B区开始计算。当然这个例子有alpha季度但事实上我的季度是一个数字。 p>

谢谢 p> div>

$quarter = ['A' => '2', 'B' => '5', 'C' => '3'];
$index = 7;
$begin = 'B';

// If $index more than amount of pics upto array end, result is empty array
$res = [];

// Remove items before `B` key
$temp = array_slice($quarter, array_search($begin, array_keys($quarter)));
foreach($temp as $k => $pics) {
   if ($index <= $pics) { $res = [$k => $index]; break; }
   else $index -= $pics;
 }  
print_r($res) ;