Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does explode(implode()) make any sense in PHP?

Tags:

arrays

php

I found this piece of code in the Phoronix Test Suite:

$os_packages_to_install = explode(' ', implode(' ', $os_packages_to_install));

I've seen it before and I don't see it's point. What does it do?

like image 925
f.ardelian Avatar asked Dec 05 '22 21:12

f.ardelian


2 Answers

It will return an array but the difference with $os_packages_to_install is that if a value of $os_packages_to_install contains a space, it will also be splitted.

so:

["hjk jklj","jmmj","hl mh","hlm"]

implode gives:

"hjk jklj jmmj hl mh hlm

explode again will give:

["hjk","jklj","jmmj","hl","mh","hlm"]
like image 103
beardhatcode Avatar answered Dec 10 '22 11:12

beardhatcode


A google search of the line came up with this:

Rebuild the array index since some OS package XML tags provide multiple package names in a single string

Basically, it's because the original array might look like this:

$os_packages_to_install = array(
  'package1',
  'package2 package3'
);

When it needs to look like this:

$os_packages_to_install = array(
  'package1',
  'package2',
  'package3'
);

Source: http://www.phorogit.com/index.php?p=phoronix-test-suite.git&dl=plain&h=7c5f0c0cf91dc61c1f220b0871040d4441836436.

like image 33
Francois Deschenes Avatar answered Dec 10 '22 11:12

Francois Deschenes