获取每10个项目的索引

时间:2017-10-19 02:57:06

标签: php arrays

我想获得数组中每10个项目的索引。我所做的是使用模数,但这不是我追求的结果。他给我的是什么

我想要的是获得项目0-9的O获得项目10-9的1。

当我为每个循环递增时,我拥有的是一个ctr。我用JS给出了一个简单的示例,但我在PHP中这样做。我的代码是这样的:

$ctr = 0;
$listjson=Array ( [0] => Array ( [sys_id] => 13 ) [1] => Array ( [sys_id] => 13 ) [2] => Array ( [sys_id] => 13 ) [3] => Array ( [sys_id] => 13 ) [4] => Array ( [sys_id] => 13 ) [5] => Array ( [sys_id] => 13 ) [6] => Array ( [sys_id] => 13 ) [7] => Array ( [sys_id] => 13 ) [8] => Array ( [sys_id] => 13 ) [9] => Array ( [sys_id] => 13 ) [10] => Array ( [sys_id] => 13 ) [11] => Array ( [sys_id] => 13 ) [12] => Array ( [sys_id] => 13 ) [13] => Array ( [sys_id] => 13 ) [14] => Array ( [sys_id] => 13 ) [15] => Array ( [sys_id] => 13 ) [16] => Array ( [sys_id] => 13 ) [17] => Array ( [sys_id] => 13 ) [18] => Array ( [sys_id] => 13 ) [19] => Array ( [sys_id] => 13 ) [20] => Array ( [sys_id] => 13 ) [21] => Array ( [sys_id] => 13 ) [22] => Array ( [sys_id] => 13 ) [23] => Array ( [sys_id] => 13 ) [24] => Array ( [sys_id] => 13 ) [25] => Array ( [sys_id] => 13 ) [26] => Array ( [sys_id] => 13 ) [27] => Array ( [sys_id] => 13 ) [28] => Array ( [sys_id] => 13 ) [29] => Array ( [sys_id] => 13 ) [30] => Array ( [sys_id] => 13 ) [31] => Array ( [sys_id] => 13 ) [32] => Array ( [sys_id] => 13 ) [33] => Array ( [sys_id] => 13 ) [34] => Array ( [sys_id] => 13 ) [35] => Array ( [sys_id] => 13 ) [36] => Array ( [sys_id] => 13 ) [37] => Array ( [sys_id] => 13 ) [38] => Array ( [sys_id] => 13 ) [39] => Array ( [sys_id] => 13 ) [40] => Array ( [sys_id] => 13 ) [41] => Array ( [sys_id] => 13 ) [42] => Array ( [sys_id] => 13 ) [43] => Array ( [sys_id] => 13 ) [44] => Array ( [sys_id] => 13 ) [45] => Array ( [sys_id] => 13 ) [46] => Array ( [sys_id] => 13 ) [47] => Array ( [sys_id] => 13 ) [48] => Array ( [sys_id] => 13 ) [49] => Array ( [sys_id] => 13 ) [50] => Array ( [sys_id] => 13 ) [51] => Array ( [sys_id] => 13 ) [52] => Array ( [sys_id] => 13 ) [53] => Array ( [sys_id] => 13 ) [54] => Array ( [sys_id] => 13 ) [55] => Array ( [sys_id] => 13 ) [56] => Array ( [sys_id] => 13 ) [57] => Array ( [sys_id] => 13 ) [58] => Array ( [sys_id] => 13 ) [59] => Array ( [sys_id] => 13 ) [60] => Array ( [sys_id] => 13 ) [61] => Array ( [sys_id] => 13 ) [62] => Array ( [sys_id] => 13 ) [63] => Array ( [sys_id] => 13 ) [64] => Array ( [sys_id] => 13 ) [65] => Array ( [sys_id] => 13 ) [66] => Array ( [sys_id] => 13 ) [67] => Array ( [sys_id] => 13 ) [68] => Array ( [sys_id] => 13 ) [69] => Array ( [sys_id] => 13 ) [70] => Array ( [sys_id] => 13 ) [71] => Array ( [sys_id] => 13 ) [72] => Array ( [sys_id] => 13 ) [73] => Array ( [sys_id] => 13 ) [74] => Array ( [sys_id] => 13 ) [75] => Array ( [sys_id] => 13 ) [76] => Array ( [sys_id] => 13 ) [77] => Array ( [sys_id] => 13 ) [78] => Array ( [sys_id] => 13 ) [79] => Array ( [sys_id] => 13 ) [80] => Array ( [sys_id] => 13 ) [81] => Array ( [sys_id] => 13 ) [82] => Array ( [sys_id] => 13 ) [83] => Array ( [sys_id] => 13 ) [84] => Array ( [sys_id] => 13 ) [85] => Array ( [sys_id] => 13 ) [86] => Array ( [sys_id] => 13 ) [87] => Array ( [sys_id] => 13 ) [88] => Array ( [sys_id] => 13 ) [89] => Array ( [sys_id] => 13 ) [90] => Array ( [sys_id] => 13 ) [91] => Array ( [sys_id] => 13 ) [92] => Array ( [sys_id] => 13 ) [93] => Array ( [sys_id] => 13 ) [94] => Array ( [sys_id] => 13 ) [95] => Array ( [sys_id] => 13 ) [96] => Array ( [sys_id] => 13 ) [97] => Array ( [sys_id] => 13 ) [98] => Array ( [sys_id] => 13 ) [99] => Array ( [sys_id] => 13 ) [100] => Array ( [sys_id] => 13 ) [101] => Array ( [sys_id] => 13 ) [102] => Array ( [sys_id] => 13 ) [103] => Array ( [sys_id] => 13 ) [104] => Array ( [sys_id] => 13 ) [105] => Array ( [sys_id] => 13 ) [106] => Array ( [sys_id] => 13 ) [107] => Array ( [sys_id] => 13 ) [108] => Array ( [sys_id] => 13 ) [109] => Array ( [sys_id] => 13 ) [110] => Array ( [sys_id] => 13 ) [111] => Array ( [sys_id] => 13 ) [112] => Array ( [sys_id] => 13 ) [113] => Array ( [sys_id] => 13 ) [114] => Array ( [sys_id] => 13 ) [115] => Array ( [sys_id] => 13 ) [116] => Array ( [sys_id] => 13 ) [117] => Array ( [sys_id] => 13 ) [118] => Array ( [sys_id] => 13 ) [119] => Array ( [sys_id] => 13 ) [120] => Array ( [sys_id] => 13 ) [121] => Array ( [sys_id] => 13 ) [122] => Array ( [sys_id] => 13 ) [123] => Array ( [sys_id] => 13 ) [124] => Array ( [sys_id] => 13 ) [125] => Array ( [sys_id] => 13 ) [126] => Array ( [sys_id] => 13 ) [127] => Array ( [sys_id] => 13 ) [128] => Array ( [sys_id] => 13 ) [129] => Array ( [sys_id] => 13 ) [130] => Array ( [sys_id] => 13 ) [131] => Array ( [sys_id] => 13 ) [132] => Array ( [sys_id] => 13 ) [133] => Array ( [sys_id] => 13 ) [134] => Array ( [sys_id] => 13 ) [135] => Array ( [sys_id] => 13 ) [136] => Array ( [sys_id] => 13 ) [137] => Array ( [sys_id] => 13 ) [138] => Array ( [sys_id] => 13 ) [139] => Array ( [sys_id] => 13 ) [140] => Array ( [sys_id] => 13 ) [141] => Array ( [sys_id] => 13 ) [142] => Array ( [sys_id] => 13 ) [143] => Array ( [sys_id] => 13 ) [144] => Array ( [sys_id] => 13 ) [145] => Array ( [sys_id] => 13 ) [146] => Array ( [sys_id] => 13 ) [147] => Array ( [sys_id] => 13 ) [148] => Array ( [sys_id] => 13 ) [149] => Array ( [sys_id] => 13 ) [150] => Array ( [sys_id] => 13 ) [151] => Array ( [sys_id] => 13 ) [152] => Array ( [sys_id] => 13 ) [153] => Array ( [sys_id] => 13 ) [154] => Array ( [sys_id] => 13 ) [155] => Array ( [sys_id] => 13 ) [156] => Array ( [sys_id] => 13 ) [157] => Array ( [sys_id] => 13 ) [158] => Array ( [sys_id] => 13 ) [159] => Array ( [sys_id] => 13 ) [160] => Array ( [sys_id] => 13 ) [161] => Array ( [sys_id] => 13 ) [162] => Array ( [sys_id] => 13 ) [163] => Array ( [sys_id] => 13 ) [164] => Array ( [sys_id] => 13 ) [165] => Array ( [sys_id] => 13 ) [166] => Array ( [sys_id] => 13 ) [167] => Array ( [sys_id] => 13 ) [168] => Array ( [sys_id] => 13 ) [169] => Array ( [sys_id] => 13 ) [170] => Array ( [sys_id] => 13 ) [171] => Array ( [sys_id] => 13 ) [172] => Array ( [sys_id] => 13 ) [173] => Array ( [sys_id] => 13 ) [174] => Array ( [sys_id] => 13 ) [175] => Array ( [sys_id] => 13 ) [176] => Array ( [sys_id] => 13 ) [177] => Array ( [sys_id] => 13 ) [178] => Array ( [sys_id] => 13 ) [179] => Array ( [sys_id] => 13 ) [180] => Array ( [sys_id] => 13 ) [181] => Array ( [sys_id] => 13 ) [182] => Array ( [sys_id] => 13 ) [183] => Array ( [sys_id] => 13 ) [184] => Array ( [sys_id] => 13 ) [185] => Array ( [sys_id] => 13 ) [186] => Array ( [sys_id] => 13 ) [187] => Array ( [sys_id] => 13 ) [188] => Array ( [sys_id] => 13 ) [189] => Array ( [sys_id] => 13 ) [190] => Array ( [sys_id] => 13 ) [191] => Array ( [sys_id] => 13 ) [192] => Array ( [sys_id] => 13 ) [193] => Array ( [sys_id] => 13 ) [194] => Array ( [sys_id] => 13 ) [195] => Array ( [sys_id] => 13 ) [196] => Array ( [sys_id] => 13 ) [197] => Array ( [sys_id] => 13 ) [198] => Array ( [sys_id] => 13 ) [199] => Array ( [sys_id] => 13 ) [200] => Array ( [sys_id] => 13 ) [201] => Array ( [sys_id] => 13 ) [202] => Array ( [sys_id] => 13 ) [203] => Array ( [sys_id] => 13 ) [204] => Array ( [sys_id] => 13 ) [205] => Array ( [sys_id] => 13 ) [206] => Array ( [sys_id] => 13 ) [207] => Array ( [sys_id] => 13 ) [208] => Array ( [sys_id] => 13 ) [209] => Array ( [sys_id] => 13 ) [210] => Array ( [sys_id] => 13 ) [211] => Array ( [sys_id] => 13 ) [212] => Array ( [sys_id] => 13 ) [213] => Array ( [sys_id] => 13 ) [214] => Array ( [sys_id] => 13 ) [215] => Array ( [sys_id] => 13 ) [216] => Array ( [sys_id] => 13 ) [217] => Array ( [sys_id] => 13 ) [218] => Array ( [sys_id] => 13 ) [219] => Array ( [sys_id] => 13 ) [220] => Array ( [sys_id] => 13 ) ) 

$arrayinjssample= Array ( [0] => 2958 [1] => 2960 [2] => 2960 [3] => 2960 [4] => 2960 [5] => 2960 [6] => 2960 [7] => 2960 [8] => 2960 [9] => 2960 [10] => 2960 [11] => 2141 [12] => 2030 [13] => 2030 [14] => 2030 [15] => 2030 [16] => 1656 [17] => 1060 [18] => 1060 [19] => 1060 [20] => 1060 [21] => 1060 [22] => 60 )

foreach ($listjson as $key => $value) {
    $arrayinjssample[$ctr % 9]
    $ctr++;
}

我上面的内容是我有一个带有项目的listjson,对于该数组中的每10个项目,我将从JS中的示例中获取索引。示例是在我的listjson中每10个项目之后,我需要在下面的js示例中获取数组的索引值。

基本上我需要得到的是一个连续的索引号。不像我在上面得到的结果那样返回0。

更新:

  

我想在$ arrayinjssample上获取$ listjson中每10个项目的索引。

示例:

items 0 - 9 in listjson I want to get index 0 in arrayinjssample 
items 10 - 19 in listjson I want to get index 1 in arrayinjssample 
items 20 - 29 in listjson I want to get index 2 in arrayinjssample 

等等

3 个答案:

答案 0 :(得分:1)

你可以简单地通过10的跳跃思考:

for (let i = 0; i > data.length; i +=10) {
    console.log(i)
}

打印:
 0
 10个
 20

编辑:获得0,1,2等等你可以潜入10:

for (let i = 0; i > data.length; i +=10) {
    console.log(i/10)
}

打印:
 0
 1
 2

答案 1 :(得分:1)

如果您使用的是PHP7,则可以使用intdiv功能:

foreach ($listjson as $key => $value) {
    $arrayinjssample[intdiv($key, 10)]
}

或者您可以自己创建此功能:

function intdiv_1($a, $b){
    return ($a - $a % $b) / $b;
}

答案 2 :(得分:0)

要获取数组中的每个第十项,只需在循环中将计数器变量递增10。

let data = [2958,2960,2960,2960,2960,2960,2960,2960,2960,2960,2960,2141,2030,2030,2030,2030,1656,1060,1060,1060,1060,1060,60]




for (var i = 0; i < data.length; i+=10) {
  //i is the index of the 10 element in the data variable
  console.log(data[i]);
}