PHP Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
quadoc
Forum Contributor
Posts: 137
Joined: Fri Jul 01, 2005 5:33 pm
Location: Atlanta, GA

PHP Array

Post by quadoc »

I've the following string $st = "1,5,3,2" and I want to put the value into an array $a like
a[1] = "1"
a[2] = "5"
a[3] = "3"
a[4] = "2"

How do I do that? Could someone posts some tips. Thanks...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

$VariableName = array(
  '1' => 'String Text',
  '2' => 'String Text',
  '3' => 'String Text',
  '4' => 'String Text',
  '5' => 'String Text'
);
EDIT: Oh I just reread your question. You will want to use the explode() function.
User avatar
quadoc
Forum Contributor
Posts: 137
Joined: Fri Jul 01, 2005 5:33 pm
Location: Atlanta, GA

Post by quadoc »

I'm sorry, I want to do it using php code. Thanks...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
quadoc
Forum Contributor
Posts: 137
Joined: Fri Jul 01, 2005 5:33 pm
Location: Atlanta, GA

Post by quadoc »

How do I explode the string with index starting with 1 not 0?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't. Arrays count from zero. What do you need it to start from one for?
User avatar
quadoc
Forum Contributor
Posts: 137
Joined: Fri Jul 01, 2005 5:33 pm
Location: Atlanta, GA

Post by quadoc »

But is there a way to start index from 1? Thanks...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Reguardless when using an array, it will be indexed starting at 0.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the only way is basically brute forcing it, by either creating the array yourself or putting something into the zero slot when the array is initially created then removing it.

Tell us what you actually want to do with it, maybe there's another more simple solution.
User avatar
NightFox
Forum Newbie
Posts: 13
Joined: Sat Mar 11, 2006 3:20 am

Re: PHP Array

Post by NightFox »

quadoc wrote:I've the following string $st = "1,5,3,2" and I want to put the value into an array $a like
a[1] = "1"
a[2] = "5"
a[3] = "3"
a[4] = "2"

How do I do that? Could someone posts some tips. Thanks...
If you really want to start it at 1 instead of zero, just add a comma to the beginning of your list like this:

Code: Select all

$st = "1,5,3,2"; // your original
$st = ",1,5,3,2"; // added extra comma
$a = explode(",",$st);
unset($a[0]);
But this is completely pointless when you can just have your code start with zero instead of 1.

just a little fyi, in case you didn't know. :)

Code: Select all

$bob[] = "hello"; // saves to $bob[0]
$bob[] = "yellow"; // saves to $bob[1] and so on...
Post Reply