Jump to content

Splitting one value into many.


Thauwa

Recommended Posts

Heya guys!

What's up?

 

OK here's my problem:

I got a variable, $var1, which is to have values like

"groan,100,200,350,50"

 

I need to know how to split this $var1 into $split1, $split2, $split3, $split4, $split5, where

$split1 = "groan";

$split2 = "100";

$split3 = "200";

$split4 = "350";

$split5 = "50";

 

Thanks in advance people!

 

P.S. In my case I am going to use some of the values for calculations. So I don't think that its easy with arrays, which are difficult for me to understand :( .

P.S.S. When I am retrieving data from the database, many variables like $var1 that need to be split are being extracted.

I hope that you understand.

 

Thanks and Regards,

Thauwa

Link to comment
https://forums.phpfreaks.com/topic/221220-splitting-one-value-into-many/
Share on other sites

use the explode function

http://php.net/manual/en/function.explode.php

It will return an array.

 

$var1='groan,100,200,350,50'

 

$array1 = explode(',',$var1);

 

print_r($array1);

 

foreach($array1 AS $var){

  echo $var;

}

 

for($i=0;$i<count($array1);$i++){

  ${'split'.$i}=$array1[$i];

}

 

echo $split1.$split2.$var3.$split4.$split5;

Thanks for the quick reply! I think that that's what I wanted. :D

One small annoying question.

In:

 

for($i=0;$i<count($array1);$i++){

  ${'split'.$i}=$array1[$i];

}

echo $split1.$split2.$var3.$split4.$split5;

 

I necessarily needn't echo the data right?

 

 

Thanks a lot MMDE. :D

no, you don't. just trying to show you what it does quickly...

 

Exactly what you want:

 

$var1='groan,100,200,350,50';

$array1 = explode(',',$var1);

for($i=0;$i<count($array1);$i++){

  ${'split'.$i}=$array1[$i];

}

 

I would have used the $array1 array though.

like this:

$array1['0']

that is the $split1 variable you want.

 

$array1['1']

that is the $split2 variable you want.

 

etc, just an explanation on how arrays work.

In all the manuals, tutorials and sample codes I've read, I've never found a better example showing the action of arrays.

Thank you MMDE. I really owe you.

 

And thanks BlueSkylS. I couldn't understand "quote numeric array keys", for I am new to arrays. If only you'd elaborate a bit...........

 

 

Thanks, Regards and Well Wishes ,

Thauwa

 

P.S. I was serious with my first sentence... -_-

 

for($i=0;$i<count($array1);$i++){

  ${'split'.($i+1)}=$array1[$i];

}

 

sorry, I forgot to make it $i+1 (the name of the split variables).

only difference is that $split1==$array1[0] now. lol

 

oh and what he meant is that 1 is a number, and not a string.

meaning:

 

array keys can be named lots of stuff, not just numbers, but also string, just like a variables.

 

$arraykey='lol';

 

$array2[$arraykey]='hello world';

 

echo $array2[lol];

^ won't work.

 

echo $array2['lol'];

^ works. I need the apostrophes to show that it's a string, not a function or something else.

 

if you try to echo only a number, you will see it works without apostrophes. It's the same thing here:

 

$arraykey=1;

 

$array3[$arraykey]='hello world';

 

echo $array3[1];

^ works

 

echo $array3['1'];

^ works

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.