![]() In this article, you learned about the implode() function in PHP and how it works. ![]() You can use the PHP View Chrome extension to format your printed array so it can look better: To prove that the original array is never modified, I’ll print the array alongside the imploded variables: "Kolade", ![]() $newPersonKeys = implode(", ", array_keys($person)) $newPersonValues = implode(", ", $person)."" To print the indexes too, you need to attach the array to the array_keys() method while printing the array: "Kolade", You can see the indexes were not printed. Let’s see how implode() works with associative arrays. You define a named index with an associative array. Examples of Implode with an Associative Array You can see it’s better to specify a separator so you can see the values well. In the example below, I passed in an empty space, comma, and hyphen as separators: "."" Note that I did not pass in a separator and implode() still works fine. You can also assign the indexes if you want.īelow is an example of how implode() works with an indexed array: In PHP, an indexed array is what it sounds like – each value in the array has an index automatically assigned to it. Examples of Implode with an Indexed Array In the syntax above, an empty space (" ") is the separator, and $array is the array. The full syntax of an implode() looks like this: implode(" ", $array) NB: implode() doesn’t work with nested arrays. The array on the other hand could be an associative array or an indexed array. If you don’t pass in the separator, implode() still works. It is valid as long as you specify it in quotes. The separator could be any character or an empty string. Implode() takes in two values as parameters – the separator and the array you want to convert to a string. Once you pass in the array to implode(), it joins all the values to a string. It doesn’t matter whether the array is an indexed or associative array. implode() doesn’t modify the original array. In PHP, the implode() function is a built-in function that takes an array and converts it to a string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |