计算数组中的项数

时间:2013-05-27 05:13:02

标签: php arrays

我有一个字符串如下所示:

SIM types:Azadi|Validity:2 Nights|Expirable:yes

我有以下代码按|分隔它们,然后逐行显示它们

$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";

$others['items'][] = explode("|",$other);
for($i = 0; $i < count($others['items']); $i++){
    echo $others['items'][$i];
}

for循环仅迭代一次并仅打印第一个值。这就是我现在得到的:

SIM类型:Azadi

3 个答案:

答案 0 :(得分:3)

试试这个

$others['items'] = explode("|",$other);
$my_count = count($others['items']);
for($i = 0; $i < $my_count; $i++){
    echo $others['items'][$i];
}

答案 1 :(得分:1)

更改

$others['items'][] = explode("|",$other);

$others['items'] = explode("|",$other);

删除[]

爆炸将返回一个数组。参考:http://php.net/manual/en/function.explode.php

$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";

$others['items'] = explode("|",$other);
for($i = 0; $i < count($others['items']); $i++){
    echo $others['items'][$i];
}

答案 2 :(得分:0)

试试这个:

<?php

$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";

$others = explode("|",$other);
$total = count($others);

for($i = 0; $i < $total; $i++){
    echo $others[$i];
}
?>
相关问题