Jump to content


foreach() question.


14 replies to this topic

#1 folta

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 14 May 2006 - 12:38 PM

say i have a string like

$string = "123abc";

i want to go and use each letter in the string seperately so i would have it in an array.
i tried doing it with explode -

$array = explode("", $string);

but that doesn't work. is there a way with foreach?
any suggestions?

Tom

Edited by folta, 14 May 2006 - 12:41 PM.


#2 Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 14 May 2006 - 12:53 PM

This is just a theory
for( $i=0; $i < strlen( $str ); $i++ )
{
  $arr[] = $str[$i];
}
echo prinr_r( $arr )

I think that should do the trick

Edited by Cliff, 14 May 2006 - 12:56 PM.


#3 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 14 May 2006 - 01:17 PM

edit: n/m. Posted later on.

Edited by .Matt, 14 May 2006 - 03:27 PM.


#4 folta

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 14 May 2006 - 01:43 PM

both of those didn't work.
but... if you have a string-

$string = "123456";

then

echo $string{0};

will echo 1

echo $string{2};

will echo 3 and so on.

#5 Futingkiller

    Young Padawan

  • Members
  • Pip
  • 110 posts

Posted 14 May 2006 - 02:01 PM

$arr=array();
for( $i=0; $i < strlen( $str ); $i++ )
{
  $arr[$i] = $str[$i];
}
echo prinr_r( $arr )
Try this. It should work

#6 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 14 May 2006 - 02:03 PM

My apolagies, im getting things muddled me finks. This works (i tried it this time)


$string = "123abc";
$count = strlen( $string );
$i = 0;
$array = array();
while($i < $count+1)
{
		 $array[$i] = $string{$i++};
}

print_r ( $array );


Edit: futingkiller got there first lol

Edited by .Matt, 14 May 2006 - 02:07 PM.


#7 folta

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 14 May 2006 - 02:46 PM

thanx for the help everyone.

#8 Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 14 May 2006 - 03:04 PM

View PostFutingkiller, on May 14 2006, 07:01 PM, said:

$arr=array();
for( $i=0; $i < strlen( $str ); $i++ )
{
  $arr[$i] = $str[$i];
}
echo prinr_r( $arr )
Try this. It should work
are you copying me

#9 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 14 May 2006 - 03:08 PM

You just forgot the $arr = array(); Cliff.

Edited by .Matt, 14 May 2006 - 03:09 PM.


#10 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 14 May 2006 - 07:35 PM

i know topics solved, but shouldnt the
$arr[$i] = $str[$i];

be

$arr[$i] = $str{$i};

???

and the the $i < strlen($str) be $i <= strlen($str) ???

Edited by coolaid, 14 May 2006 - 07:36 PM.


#11 Futingkiller

    Young Padawan

  • Members
  • Pip
  • 110 posts

Posted 15 May 2006 - 07:16 AM

no, it should be just "<" because the length of a string in this case " $string = "123abc";" is=6, and $i starts from 0 to 5-->6 elements.
$arr[$i] = $str{$i}; is correct, it's my mistake.
cliff, i used your script and corected it, sorry for copying (but it's the best way to use, and i usualy use it just like that).
.Matt it wasn't just the $arr=array(); It's " $arr[$i] = $str[$i];" also :blink:
.Matt are you positive that "while($i < $count+1)" can't be replaced with: "while($i < $count)"??

#12 Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 15 May 2006 - 09:19 AM

Well you corrected it wrong 8-)

You don't have to define an array, it becomes one automatically.
You can just do $arr[], as it will move to the next index.

#13 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 15 May 2006 - 09:53 AM

I would've done it this way.
$string = "123abc";
$stop = len($string);
$count = 0;
While ($count <= $stop){
echo $string[$count];
$count = $count + 1;
}


#14 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 15 May 2006 - 11:44 AM

lmao, Futingkiller yes, yes i dont know why i put +1. :) :blink: :)

#15 Futingkiller

    Young Padawan

  • Members
  • Pip
  • 110 posts

Posted 15 May 2006 - 02:06 PM

Cliff, then it's diferent from all the other programing languages that i use :D
i am not that good of a php programer. i am better in javascipt :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users