omg omg - 5 months ago 10
PHP Question

How do I combine these words in an array with " " in PHP?

I want to combine the [word] part of each array with " ", and the array structure is as follows:

Array ( [0] => Array ( [word] => Hello [off] => 0 [len] => 5 [idf] => 4.0235948562622 [attr] => en ) [1] => Array ( [word] => , [off] => 5 [len] => 1 [idf] => 0 [attr] => un ) [2] => Array ( [word] => long [off] => 6 [len] => 4 [idf] => 3.465735912323 [attr] => en ) )

Array ( [0] => Array ( [word] => time [off] => 11 [len] => 4 [idf] => 3.465735912323 [attr] => en ) )

Array ( [0] => Array ( [word] => no [off] => 16 [len] => 2 [idf] => 1.7328679561615 [attr] => en ) )

Array ( [0] => Array ( [word] => see [off] => 19 [len] => 3 [idf] => 2.7465307712555 [attr] => en ) [1] => Array ( [word] => ! [off] => 22 [len] => 1 [idf] => 0 [attr] => un ) [2] => Array ( [word] => How [off] => 23 [len] => 3 [idf] => 2.7465307712555 [attr] => en ) )

Array ( [0] => Array ( [word] => are [off] => 27 [len] => 3 [idf] => 2.7465307712555 [attr] => en ) )

Array ( [0] => Array ( [word] => you [off] => 31 [len] => 3 [idf] => 2.7465307712555 [attr] => en ) )

Array ( [0] => Array ( [word] => recently [off] => 35 [len] => 8 [idf] => 5.1986036300659 [attr] => en ) [1] => Array ( [word] => ? [off] => 43 [len] => 1 [idf] => 0 [attr] => un ) )


Is there a built-in function of PHP that can do this? Because I think looping is less efficient.

Answer

implode + array_map?

function func($a) {
   return $a['word'];
}
implode(' ', array_map('func', $x));
Comments