deviloper
deviloper

Reputation: 7240

counting the number of items in an array

I have a string as shown bellow:

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

I have the following code to seperate them by | and then show them line by line

$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];
}

but the for loop is iterated only once and prints only the first value. This is what i am getting now:

SIM types:Azadi

Upvotes: 0

Views: 92

Answers (3)

GautamD31
GautamD31

Reputation: 28763

Try like this

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

Upvotes: 3

Vijaya Pandey
Vijaya Pandey

Reputation: 4282

Try this:

<?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];
}
?>

Upvotes: 0

Prasanth Bendra
Prasanth Bendra

Reputation: 32740

Change

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

to

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

remove []

Explode will return a array. ref: 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];
}

Upvotes: 1

Related Questions