开发者

Does explode(implode()) make any sense in 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?


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"]


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.


Yes, if strings in array $os_packages_to_install has whitespace characters.


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" } 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜