Comma-separated values to array in PHP

Dear visitors today we will learn about Comma-separated Values to Array in PHP. We often need to select records from the database. There are different methods to select data from the Database. But today we will learn something difficult.

Problem (Comma-separated values):

Suppose you have a string data that are comma-separated. Now you need to convert that data into array and you want each word that is separated with comma into array value. So what will you do to get the required value? that is the problem. Example tact with a variable is given below:

$string = “one,two,three,four”;

and you need to convert it into array and store all of these values into a variable to perform specific action:

$string_array = array(“one”,”two”,”three”,”four”);

The solution as array explode

You understood the problem and now want to solve it? Of course, it is an easy job. A nice function that is available in PHP is the “Explode” function. This function Breaks a string into an array. Syntax of this function is as under:

explode(separator,string,limit)

Where

separatorRequired. Specifies where to break the string
stringRequired. The string to split
limitOptional. Specifies the number of array elements to return. Possible values: Greater than 0 – Returns an array with a maximum of the limit element(s)Less than 0 – Returns an array except for the last -limit elements()0 – Returns an array with one element

How to solve the array problem?

Write following function or copy paste into your favorite IDE

<?php
function comma_separated_to_array($string_value, $separator = ',')
{
  //Explode on comma
  $vals     =   explode($separator, $string_value);
  $count    =   count($vals);
  $val      =   array();
  //Trim whitespace
  for($i=0;$i<=$count-1;$i++) {
    $val[]   .=   $vals[$i];
  }
  return $val;
}
?>

Explanation:

You wrote a function for Comma-separated Values to Array in PHP where you used explode function to break the string value into an array and then with the use of count function and for loop you add all exploded values into an array and then return it. Now you can use this function anywhere, but remember you have to pass a minimum 1 parameter to a function which is $string value. 2nd parameter is optional. However, if you have something else in place of the comma “,” then you have to put that specific character in place of the comma.

Second Method

Here is a short method that will give you an array with keys and value. In this method, you don’t need to apply a loop. The problem with this method is that you will get a complete array with keys and values.

<?php
function comma_separated_to_array($string_value, $separator = ',')
{
  //Explode on comma
  $vals     =   explode($separator, $string_value);
  return $val;
}
?>

Now this method/function is ready to use. Use it anywhere just write function and put value and echo them like this:

<?php
echo comma_separated_to_array('Apple,Mango,Melon,Orange',',');

Hope you enjoyed the tutorials. See you in next tutorial.

If you still need help then you can comment or contact HERE for more help on PHP array.

Here are some more tutorials you may like to read.

Leave a Reply - I will show after approval