php计算分解后的字符串数 [英] php count the number of strings after exploded

查看:98
本文介绍了php计算分解后的字符串数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

<?php

$string = 'a|b|c|d|e|f';

$tags = explode('|' , $string);


foreach($tags as $i =>$key) {
$i >0;
    echo $i.' '.$key .'</br>';

}

?>

输出为

0 a
1 b
2 c
3 d
4 e
5 f

我试图计算字符串后,我爆炸的数量| (我的例子应该是6)也需要我的$ i从1开始不是0

What i'm try to count the number of strings after i exploded | (it should be 6 for my example) also i need my $i to start from 1 not 0

有什么想法吗?

谢谢。

推荐答案

<?php

$string = 'a|b|c|d|e|f';

$tags = explode('|' , $string);


foreach($tags as $i =>$key) {
$i >0;
    echo $i.' '.$key .'</br>';

}

?>

尝试使用:

echo count($tags); // Output of 6

数组以键值0开始,而不是一个。所以当使用除了count之外的任何东西,你将不断地得到比你预期的少1(除非你在计数之前修改数组)

Arrays start with a key of 0, not one. So when using anything else apart from count, you will constantly get 1 less than your expected (unless you modify the array prior to counting)

这篇关于php计算分解后的字符串数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆