+ 2
Need some help here
How can i printout multiple strings in arrays? if my array look like this $list_array = array ('book', 'pen', 'cupboard', 'pencil') ; How can i print pen and pencil or book,pen, and pencil?
9 Antworten
+ 4
Use a foreach loop or a for loop like this:
$list_array = array("book","pen","cup","pencil");
foreach ($list_array as $list) {
echo $list;
}
+ 3
You can use a for loop:
for ($i = 0; $i < 4; $i++)
echo $arr[$i];
+ 2
Bladimir Reales thank you so much, but that code print all of them, what can i do if i need to print only two of them?
+ 2
For that you can't really use foreach unless you know the exact value of the element:
foreach ($list_array as $i) {
echo $i;
if ($i == "pen")
break;
}
Otherwise, you should use a for loop:
for ($i = 0; $i < 4; $i++) {
echo $list_array[$i];
if ($i == 2)
break;
}
or
for ($i = 0; $i < 2; $i++)
echo $list_array[$i];
+ 2
$list_array = array("book","pen","cup","pencil");
foreach ($list_array as $list) {
if ($list != "pen" && $list != "pencil") {
continue;
}
echo $list;
+ 2
you can do that too :)
+ 2
Bladimir Reales thank you for helping mee!
+ 1
Airree whoaa thank you so much, get it noww
+ 1
You're welcome! ;)