Joinging the list to string

Joined
Dec 2, 2024
Messages
2
Reaction score
0
Given a list a = ['a', 'b', 'c', 'd'] and an empty string s = '', how can I use a for-each loop to add each element of the list to s so that it stores the result as s = 'abcd'? Should I use add or push?
 
Joined
Jul 4, 2023
Messages
565
Reaction score
74
The simplest way is to use the implode
PHP:
$a = ['a', 'b', 'c', 'd'];
$s = implode('', $a);
echo $s; // 'abcd'
PHP:
$a = ['a', 'b', 'c', 'd'];
$s = join('', $a);
echo $s; // 'abcd'
join is an alias for implode, so it works the same way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top