Monday , 25 September 2023
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 the 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

6 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.

  4. hai muhammad
    i am new in php. i have following inserted values in mysql table like following.

    id invoiceno indate statemiles
    310 IN/10-11/004 02-15-2020 MI,108.53
    OH,194.57
    PA,188.22
    WA,238.57

    308 IN/10-11/001 02-05-2020 IA,59.62
    MI,170.99
    IN,152.14
    OH,241.14
    PA,188.22
    WA,238.57

    I want to display results by date filter and output like following. Is this possible?
    Awaiting for your reply. Thanks
    STATE MILES
    MI 279.52
    OH 435.71
    PA 376.44
    WA 477.14
    IA 59.62
    IN 152.14

    Total 1780.57

Leave a Reply to Muhammad Faryad Cancel reply

Ad Test

%d bloggers like this: