有没有办法修复 count() 的第一个输出?
Is there anyway to fix the first output of count()?
我有一个电影网站,我想计算每个框的链接数,但第一个是 count();输出为 0,之后它将继续实数,这很糟糕这是我的代码:
<div class="links" style="display: none;">
<?php
$c = 1;
$post_something = $link['links'];
//This will count the number of links
$number = count($post_something);
foreach($post_something as $links){
//This is the output of the count ?>
<p><?php echo $number ?></p>
<a class="download-button" href="<?php echo $links['link_of_field'] ?>">
<?php
if(!empty($links['name_field'])){
echo $links['name_field'];
$c++;
} else {?>
قسمت <?php echo $c;
$c++;
?>
<?php }
?>
</a>
<? }
?>
</div>
</div>
<?php }?>
问题是一开始它的输出是0!
这是var_dump
的结果
array(6) {
[0]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E01_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[1]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E02_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[2]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E03_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[3]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E04_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[4]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E05_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[5]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E06_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" } }
array(6) {
[0]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E01_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[1]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E02_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[2]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E03_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[3]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E04_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[4]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E05_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[5]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E06_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" } }
嗯,从技术上讲,在计算机语言中,所有计数都从 [0]
开始,而不是 [1]
。所以你的代码没有问题。如果它从 0 开始,它工作正常。
代码中的这一行 $number = count($post_something);
应该 return 结果的实际数量 returned。
让我试着用一个例子来解释;
在这样的代码中;
<?php
$cars=array("Volvo","BMW","Toyota");
$totalCount = count($cars);
print_r($cars); // this will print Array ( [0] => Volvo [1] => BMW [2] => Toyota )
echo $totalCount; // this will return 3
?>
所以在上面的示例中,数组从 0 开始打印,但计数产生数组中的项目数,即 3。
我希望这能为您澄清一些事情。如果不愿意,我愿意进一步解释或与您解决这个问题。
我有一个电影网站,我想计算每个框的链接数,但第一个是 count();输出为 0,之后它将继续实数,这很糟糕这是我的代码:
<div class="links" style="display: none;">
<?php
$c = 1;
$post_something = $link['links'];
//This will count the number of links
$number = count($post_something);
foreach($post_something as $links){
//This is the output of the count ?>
<p><?php echo $number ?></p>
<a class="download-button" href="<?php echo $links['link_of_field'] ?>">
<?php
if(!empty($links['name_field'])){
echo $links['name_field'];
$c++;
} else {?>
قسمت <?php echo $c;
$c++;
?>
<?php }
?>
</a>
<? }
?>
</div>
</div>
<?php }?>
问题是一开始它的输出是0!
这是var_dump
的结果array(6) {
[0]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E01_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[1]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E02_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[2]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E03_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[3]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E04_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[4]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E05_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[5]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E06_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" } }
array(6) {
[0]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E01_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[1]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E02_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[2]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E03_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[3]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E04_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[4]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E05_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" }
[5]=> array(2) { ["name_field"]=> string(0) "" ["link_of_field"]=> string(176) "dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E06_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv" } }
嗯,从技术上讲,在计算机语言中,所有计数都从 [0]
开始,而不是 [1]
。所以你的代码没有问题。如果它从 0 开始,它工作正常。
代码中的这一行 $number = count($post_something);
应该 return 结果的实际数量 returned。
让我试着用一个例子来解释;
在这样的代码中;
<?php
$cars=array("Volvo","BMW","Toyota");
$totalCount = count($cars);
print_r($cars); // this will print Array ( [0] => Volvo [1] => BMW [2] => Toyota )
echo $totalCount; // this will return 3
?>
所以在上面的示例中,数组从 0 开始打印,但计数产生数组中的项目数,即 3。
我希望这能为您澄清一些事情。如果不愿意,我愿意进一步解释或与您解决这个问题。