PHP explode() and implode() can be used to split strings into array or join the array elements as strings.
<span class="nv">$foo</span> <span class="o">=</span> <span class="nx">bars</span><span class="p">()</span>
<span class="nv">$foobars</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="nv">$foo</span><span class="p">,</span> <span class="nv">$bar</span><span class="p">);</span>
<span class="nv">$foobars</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$foo</span><span class="p">;</span>
<span class="nv">$foobars</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$bars</span><span class="p">;</span>
<span class="nv">$foobar</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">'foo'</span> <span class="o">=></span> <span class="nv">$foo</span><span class="p">,</span> <span class="s1">'bars'</span> <span class="o">=></span> <span class="nv">$bars</span><span class="p">);</span>
<span class="nv">$foobar</span><span class="p">[</span><span class="s1">'foo'</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$foo</span><span class="p">;</span>
<span class="nv">$foobar</span><span class="p">[</span><span class="s1">'bars'</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$bars</span><span class="p">;</span>
The square brackets methods
The square brackets method allows you to set up by directly setting the value. For example, to make $foobar[1] = $foo, all you need to do is: <span class="nv">$foobar</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$foos</span><span class="p">;</span>
<span class="nv">$foobar</span><span class="p">[</span><span class="s1">'foo'</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$foos</span><span class="p">;
</span>
<span class="cp"><?php</span>
<span class="nv">$array</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s2">"name"</span><span class="o">=></span><span class="s2">"Toyotas"</span><span class="p">,</span><span class="s2">"type"</span><span class="o">=></span><span class="s2">"Celicas"</span><span class="p">,</span><span class="s2">"colour"</span><span class="o">=></span><span class="s2">"blacks"</span><span class="p">,</span><span class="s2">"manufactured"</span><span class="o">=></span><span class="s2">"1991"</span><span class="p">);</span>
<span class="nv">$array2</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s2">"Toyotas"</span><span class="p">,</span><span class="s2">"Celicas"</span><span class="p">,</span><span class="s2">"blacks"</span><span class="p">,</span><span class="s2">"1991"</span><span class="p">);</span>
<span class="nv">$array3</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s2">"name"</span><span class="o">=></span><span class="s2">"Toyotas"</span><span class="p">,</span><span class="s2">"Celicas"</span><span class="p">,</span><span class="s2">"colours"</span><span class="o">=></span><span class="s2">"black"</span><span class="p">,</span><span class="s2">"1991"</span><span class="p">);</span>
<span class="nb">print_r</span><span class="p">(</span><span class="nv">$array</span><span class="p">);</span>
<span class="nb">print_r</span><span class="p">(</span><span class="nv">$array2</span><span class="p">);</span>
<span class="nb">print_r</span><span class="p">(</span><span class="nv">$array3</span><span class="p">);</span>
<span class="cp">?></span>
<span class="p"> </span>
Explode
Explode function breaks the string into arrays, it accepts three argument, first one is the delimiter that specifies where to break the strings, second one is the strings that needs splitting, and third one is not mandatory but it tells how many arrays to return.
PHP
1
2 3 4 5 6 7 8 9 10 11 12 13 |
<?php
$string_to_explode = ‘this-strings-needs-some-exploding’; //Break string into an arrays $exploded_strings = explode(‘-‘, $string_to_explode); print_r($exploded_strings); //output : // Array ( [0] => this [1] => string [2] => needs [3] => some [4] => exploding ) echo $exploded_strings[0]; echo $exploded_strings[1]; ?> |
Implode
Unlinke explode() function, implode() joins the arrays values into a strings, and it accepts two argument, first argument the separator which specifies what character to use between array elements, and second one is the collection of array value.
PHP
1
2 3 4 5 6 7 8 |
<?php
//array values $array_values = array(‘eat’,‘sleep’,‘walk’,‘read’,‘write’,‘watch’,‘move’); //join all array values with “-” hyphen //output : eat-sleep-walk-read-write-watch-move echo implode(‘-‘, $array_values); ?> |