Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to explode a string using a foreach

I got this string

$string = '1:23:health,2:24:mana';
$v = explode(",", $string);
foreach($v as $data)
{
    echo $data[0][1];
}

What im looking is to making a "multidimensional string" by exploding ‘,’

Wanna do this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data[0][0] = 1;
data[0][1] = 23;
data[0][2] = "health";

How can i do this in PHP?

>Solution :

$string = '1:23:health,2:24:mana';

$result = [];
foreach (explode(',', $string) as $step) {
  $result[] = explode(':', $step);
}

print_r($result);

An alternative is:

$string = '1:23:health,2:24:mana';

$result = array_chunk(preg_split('/[:,]/', $string), 3);

print_r($result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading