0

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

3 Answers 3

3

Try like this

$others['items'] = explode("|",$other);
$my_count = count($others['items']);
for($i = 0; $i < $my_count; $i++){
    echo $others['items'][$i];
}
Sign up to request clarification or add additional context in comments.

2 Comments

This part, count($others['items']) is not efficent. Better is to use function outside loop like, $total = count($others['items']); and change the loop like, for($i = 0; $i < $total; $i++){...}
Yah thanku @web2students.com I have changed it,thanks for your suggestion
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.