我試圖用for循環填充數組。這樣做是爲了獲得某本書的頁數。但是在執行代碼時,它會跳過數組中的第一個對象。誰能告訴我爲什麼? (我認爲這是因爲$ I從1開始,而不是0,但似乎並沒有改變任何東西)PHP For循環跳過第一個結果
if(!empty($article['finishing'])){
$numPages = $article['copies'];
$arrayIndexNumber = [];
for($i=1; $i <= $numPages; $i++){
$arrayIndexNumber[] = $i;
}
if(count($arrayIndexNumber) >= 1){
if(count($arrayIndexNumber) == 1){
$output['attributes']['EFPageRange'] = 1;
$print_jobs[$article['id']][] = $output;
}
if(count($arrayIndexNumber) > 1){
$comma_separated1 = implode(", ", ['1', $article['copies']]);
$output['attributes']['EFPageRange'] = $comma_separated1;
$print_jobs[$article['id']][] = $output;
}
array_shift($arrayIndexNumber);
array_pop($arrayIndexNumber);
$comma_separated2 = implode(", ", $arrayIndexNumber);
$output['attributes']['EFPageRange'] = $comma_separated2;
if(count($arrayIndexNumber) >= 2){
$print_jobs[$article['id']][] = $output;
}
}
$article['file_url'] = 'i has finishing';
$output['attributes']['username'] = $article['file_url'];
}
上面的代碼輸出:的
[0] => Array
(
[attributes] => Array
(
[title] => 277569
[EFPrintSize] => a4
[num copies] => 1
[num pages] => 119
[EFPCName] => 80
[EFDuplex] => TopTop
[EFPageRange] => 1, 119
)
)
代替:
[0] => Array
(
[attributes] => Array
(
[title] => 277564
[EFPrintSize] => a4
[num copies] => 1
[num pages] => 45
[EFPCName] => 80
[EFDuplex] => false
[EFPageRange] => 1, 45
[username] => i has finishing
[EFColorMode] => Grayscale
)
)
通過這樣做,我的「PageRange」屬性將不再匹配 – Dannylycka
我剛剛告訴你哪裏做錯了。如果正常的迭代不起作用,並打破了代碼的其餘部分,則應該修復那些破壞正常邏輯流程的部分。不要打我,不要打php的內部邏輯 - 只是去錯誤的邏輯根據錯誤的假設修復 - )) – marmeladze
只是因爲indizies從零開始,並不意味着每個使用數組的循環都需要從零開始。 –