+ 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?

15th Jun 2019, 1:47 PM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
9 Answers
+ 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; }
15th Jun 2019, 2:04 PM
Bladimir Reales
Bladimir Reales - avatar
+ 3
You can use a for loop: for ($i = 0; $i < 4; $i++) echo $arr[$i];
15th Jun 2019, 1:51 PM
Airree
Airree - avatar
+ 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?
15th Jun 2019, 2:35 PM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
+ 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];
15th Jun 2019, 2:53 PM
Airree
Airree - avatar
+ 2
$list_array = array("book","pen","cup","pencil"); foreach ($list_array as $list) { if ($list != "pen" && $list != "pencil") { continue; } echo $list;
15th Jun 2019, 3:08 PM
Bladimir Reales
Bladimir Reales - avatar
+ 2
you can do that too :)
15th Jun 2019, 3:10 PM
Bladimir Reales
Bladimir Reales - avatar
+ 2
Bladimir Reales thank you for helping mee!
15th Jun 2019, 3:21 PM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
+ 1
Airree whoaa thank you so much, get it noww
15th Jun 2019, 2:59 PM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
+ 1
You're welcome! ;)
15th Jun 2019, 3:33 PM
Bladimir Reales
Bladimir Reales - avatar