Monday , 27 May 2019
Breaking News
Home > Tutorials > PHP > Convert comma separated values to array in PHP

Convert comma separated values to array in PHP

Assalam-o-Alaikuim!

Dear friends today we will learn about PHP arrays. We often need to select records from database. There are different methods to select data from Database. But today we will learn something difficult.

Problem:

Suppose you have following string data that you want to convert into an array to perform action on it:

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

and you need to convert it into:

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

Solution

You need to write a function with PHP’s explode function for easy job.

How?

Write following function

Explanation:

You wrote a function where you used explode function 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 any where, but remember you have to pass minimum 1 parameter to function which is $string value. 2nd parameter is optional.

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

 

About Muhammad Faryad

Muhammad Faryad is a professional web application developer. He has been working for 5 years in ICE786 Technologies Pvt. Ltd as a Senior Programmer and Developer. You can contact him on facebook at www.facebook.com/tolamangali

4 comments

  1. Hello Muhammad!
    I am trying to explode array names instead of array id’s with INNER JOIN function. However, system is taking only id’s. Would you please assist? Thank you!

    <?php $query=mysqli_query($con,"select size.id, size.sizeName, products.productSize from size INNER JOIN products ON size.id=products.productSize WHERE products.id='$pid'");
    while($res = mysqli_fetch_array($query)) {
    $size=$res['productSize'];
    $boom = explode(",", $size);
    foreach ($boom as $res){
    echo '’.$res[‘sizeName’].”;
    }} ?>

    • mm

      Hi Lena Please try this:
      ///Change this to
      foreach ($boom as $res){
      echo ‘’.$res[‘sizeName’].”;
      }

      foreach ($boom as $key=>$value){
      echo $key;
      echo $value;
      }

      Hope it will solve your problem. If not please tell me. I am in hurry so can’t test code at the time. Thanks for the comment.

  2. Hello Muhammad! I am trying to explode array names instead if array id’s with INNER JOIN function, but only id’s are shown. Would you please assist? Thank you!

    <?php $query=mysqli_query($con,"select size.id, size.sizeName, products.productSize from size INNER JOIN products ON size.id=products.productSize WHERE products.id='$pid'");
    while($res = mysqli_fetch_array($query)) {
    $size=$res['productSize'];
    $boom = explode(",", $size);
    foreach ($boom as $row){
    echo '’.$row[‘sizeName’].”;
    }} ?>

  3. mm

    Hi Lena Please try this:
    ///Change this to
    foreach ($boom as $res){
    echo ‘’.$res[‘sizeName’].”;
    }

    foreach ($boom as $key=>$value){
    echo $key;
    echo $value;
    }

    Hope it will solve your problem. If not please tell me. I am in hurry so can’t test code at the time. Thanks for the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Ad Test