Reputation: 6956
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?
Upvotes: 4
Views: 517
Reputation: 4773
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"]
Upvotes: 8
Reputation: 2561
if string contains whitespace like $str[0] = "abcd bce"; $str[1] = "bcde sdf"; and if one executes your command then .
it will be splitted in array with 4 records rather then splitting in 2.
Upvotes: 0
Reputation: 24989
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'
);
Upvotes: 6
Reputation: 8089
it may, if input array is associative:
$os_packages_to_install = array('key'=>'val1','val2','val3');
var_dump($os_packages_to_install);
var_dump(explode(' ', implode(' ', $os_packages_to_install)));
output is:
array(3) { ["key"]=> string(4) "val1" [0]=> string(4) "val2" [1]=> string(4) "val3" }
array(3) { [0]=> string(4) "val1" [1]=> string(4) "val2" [2]=> string(4) "val3" }
Upvotes: 1
Reputation: 3612
Yes, if strings in array $os_packages_to_install
has whitespace characters.
Upvotes: 1