join()

The join() method join array elements with a string

Example.php
<?php use kim\present\utils\arrays\ArrayUtils;

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot"]);

//Joining array with multiple elements
$arrayUtils->join();         // "Apple,Banana,Carrot"
$arrayUtils->join(" and ");  // "Apple and Banana and Carrot"
$arrayUtils->join(" || ");   // "Apple || Banana || Carrot"

//Joining with prefix
$arrayUtils->join(" and ", "I like ");
// expected output: "I like  Apple and Banana and Carrot"

//Joining with prefix and suffix
$arrayUtils->join(" and ", "I like ", " little bit");
// expected output: "I like Apple and Banana and Carrot little bit"

Syntax

$arrayUtils->join(string $glue = ",", string $prefix = "", string $suffix = "") : string;

Parameter

  • The string to separate each pair of adjacent elements. Default: ","

  • The prefix string. Default: ""(empty)

  • The suffix string. Default: ""(empty)

Return value

  • A string with all array elements joined

Prefixing

ArrayUtils::joinFrom(iterable $from, string $glue = ",", string $prefix = "", string $suffix = "") : string;

References

Last updated