KhairulAlam's avatar

How to separate each array index into new arrays. (PHP Specific)

Hi, this question doesn't relate to Laravel directly so I apologize in advance.

Suppose I have an array like this -

Array ( [0] => 21,"Monipur","[email protected]",466464564,Admin,1,"Monipur","Terry","[email protected]",466464564,School,"Dhaka",1213","Dhaka" [1] => 25,"Monipur High","terry123@gmail.com",78997895,Admin,1,"Monipur High","Terry","terry123@gmail.com",78997895,School,"Dhaka",1213","Dhaka" [2] => 26,"Monipur High","[email protected]",78456644,Admin,1,"Monipur High","Terry","[email protected]",78456644,School,"Dhaka",1212","Dhaka" [3] => 27,"Monipur High","[email protected]",4546464456,Admin,1,"Monipur High","Harry","[email protected]",4546464456,School,"Dhaka",1212","Mirpur" [4] => 30,"Hello High","[email protected]",54464464,Admin,1,"Hello High","Hello","[email protected]",54464464,School,"Dhaka",1211","Dhanmondi" [5] => 32,"Bengali School","[email protected]",789756465654,Admin,1,"Bengali School","Tazin","[email protected]",789756465654,School,"Dhaka",1213","Mirpur" )

As you can see the values in the array are comma separated and doesn't have any key themselves. But I need to print this list within a html table. So I need to separate each index into a separate array like so -

Array ( [0] => 21 [1] => "Monipur" [2] => "[email protected]" [3] => 466464564 [4] => Admin [5] => 1 [6] => "Monipur" [7] => "Terry" [8] => "[email protected]" [9] => 466464564 [10] => School [11] => "Dhaka" [12] => 1213" [13] => "Dhaka" )

I can extract the values into an array for single index but I'm not sure how to do this for all the indexes in the array. In other words, I'm stuck with the loop for this procedure. I'm sure it's easy but I'm just not getting it. Please help. TIA.

0 likes
3 replies
ohffs's avatar

Have a look at PHP's str_getcsv function - I think that's what you're after? Something like :

$splitArrays = [];
foreach ($originalArray as $line) {
  $splitArrays[] = str_getcsv($line);
}
// $splitArrays is now an array - each entry being itself being an array of the values from the original line
1 like
aalaap's avatar

You're right about it being easy. @ohffs's suggestion is correct, but here is a more efficient way to use it:

array_map('str_getcsv', $array);

The output will be like this:

     [
       "21",
       "Monipur",
       "[email protected]",
       "466464564",
       "Admin",
       "1",
       "Monipur",
       "Terry",
       "[email protected]",
       "466464564",
       "School",
       "Dhaka",
       "1213"",
       "Dhaka",
     ],
     [
       "25",
       "Monipur High",
       "[email protected]",
       "78997895",
       "Admin",
       "1",
       "Monipur High",
       "Terry",
       "[email protected]",
       "78997895",
       "School",
       "Dhaka",
       "1213"",
       "Dhaka",
     ],
    // ...
   ]
KhairulAlam's avatar

I've done it. I used the str_getcsv() function to extract the values. Thank you both for your helpful inputs. :)

Please or to participate in this conversation.