By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

Explode into an array not beginning at 0 ?

P: n/a
I would like to Explode a string into an array that does not begin at
0 but I can't get it to work.

For example: $MyInfo = array(1 =27,68,31,19,40);
will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40

But, I'd like to process a string: "27,68,31,19,40" such that it would
get the same result.

I tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =$MyString);
But that doesn't work.

I also tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =explode($MyString, ","));
That doesn't do it either.

How can this be done?
Feb 11 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 11 Feb, 16:02, Jack <ironwoodcan...@gmail.comwrote:
I would like to Explode a string into an array that does not begin at
0 but I can't get it to work.

For example: $MyInfo = array(1 =27,68,31,19,40);
will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40

But, I'd like to process a string: "27,68,31,19,40" such that it would
get the same result.

I tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =$MyString);
But that doesn't work.

I also tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =explode($MyString, ","));
That doesn't do it either.

How can this be done?
It might help if you tell us why you feel the need to do this.
Feb 11 '08 #2

P: n/a
On Mon, 11 Feb 2008 17:02:36 +0100, Jack <ir************@gmail.comwrote:
I would like to Explode a string into an array that does not begin at
0 but I can't get it to work.

For example: $MyInfo = array(1 =27,68,31,19,40);
will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40

But, I'd like to process a string: "27,68,31,19,40" such that it would
get the same result.

I tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =$MyString);
But that doesn't work.

I also tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =explode($MyString, ","));
That doesn't do it either.

How can this be done?

You shouldn't want it, but here you go:
Easiest:
<?php
$MyString = "27,68,31,19,40"
$MyArray = explode(',', '0,'.$MyString);
unset($MyArray[0]);
?>
More verbose:
<?php
$MyString = "27,68,31,19,40"
$MyArray = explode(',',$MyString);
array_unshift($MyArray,null);
unset($MyArray[0]);
?>
--
Rik Wasmus
Feb 11 '08 #3

P: n/a
On Mon, 11 Feb 2008 08:05:47 -0800 (PST), Captain Paralytic
<pa**********@yahoo.comwrote:
>On 11 Feb, 16:02, Jack <ironwoodcan...@gmail.comwrote:
>I would like to Explode a string into an array that does not begin at
0 but I can't get it to work.

For example: $MyInfo = array(1 =27,68,31,19,40);
will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40

But, I'd like to process a string: "27,68,31,19,40" such that it would
get the same result.

I tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =$MyString);
But that doesn't work.

I also tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =explode($MyString, ","));
That doesn't do it either.

How can this be done?

It might help if you tell us why you feel the need to do this.
How does that impact the the issue of "how can this be done" ?
Feb 11 '08 #4

P: n/a
On Mon, 11 Feb 2008 17:13:09 +0100, Jack <ir************@gmail.comwrote:
On Mon, 11 Feb 2008 08:05:47 -0800 (PST), Captain Paralytic
<pa**********@yahoo.comwrote:
>On 11 Feb, 16:02, Jack <ironwoodcan...@gmail.comwrote:
>>I would like to Explode a string into an array that does not begin at
0 but I can't get it to work.

For example: $MyInfo = array(1 =27,68,31,19,40);
will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40

But, I'd like to process a string: "27,68,31,19,40" such that it would
get the same result.

I tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =$MyString);
But that doesn't work.

I also tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =explode($MyString, ","));
That doesn't do it either.

How can this be done?

It might help if you tell us why you feel the need to do this.

How does that impact the the issue of "how can this be done" ?
We might offer a better solution to the problem then one that will throw
other/fellow/future coders off balance.
--
Rik Wasmus
Feb 11 '08 #5

P: n/a
..oO(Jack)
>On Mon, 11 Feb 2008 08:05:47 -0800 (PST), Captain Paralytic
<pa**********@yahoo.comwrote:
>>It might help if you tell us why you feel the need to do this.

How does that impact the the issue of "how can this be done" ?
It looks as if you just want the indexing to start at 1 instead of 0.
But usually this is not necessary, since you can always adjust it as
necessary when accessing the array, for example by giving another start
value for a loop or by using something like $yourArray[$yourIndex-1].

Arrays in PHP are always zero-based, many array handling functions
expect or return them this way.

So, the question "Why?" is valid.

Micha
Feb 11 '08 #6

P: n/a
On Feb 11, 11:13 am, Jack <ironwoodcan...@gmail.comwrote:
On Mon, 11 Feb 2008 08:05:47 -0800 (PST), Captain Paralytic

<paul_laut...@yahoo.comwrote:
On 11 Feb, 16:02, Jack <ironwoodcan...@gmail.comwrote:
I would like to Explode a string into an array that does not begin at
0 but I can't get it to work.
For example: $MyInfo = array(1 =27,68,31,19,40);
will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40
But, I'd like to process a string: "27,68,31,19,40" such that it would
get the same result.
I tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =$MyString);
But that doesn't work.
I also tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =explode($MyString, ","));
That doesn't do it either.
How can this be done?
It might help if you tell us why you feel the need to do this.

How does that impact the the issue of "how can this be done" ?
Because, according to "How To Ask Questions the Smart Way" you should
describe the /goal/ you are after, not the intermediate step you might
be stuck on:

<http://catb.org/~esr/faqs/smart-questions.html#goal>
Feb 11 '08 #7

P: n/a
On Feb 11, 6:02 pm, Jack <ironwoodcan...@gmail.comwrote:
I would like to Explode a string into an array that does not begin at
0 but I can't get it to work.

For example: $MyInfo = array(1 =27,68,31,19,40);
will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40

But, I'd like to process a string: "27,68,31,19,40" such that it would
get the same result.

I tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =$MyString);
But that doesn't work.

I also tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =explode($MyString, ","));
That doesn't do it either.

How can this be done?
-------------
<?
$MyString = "27,68,31,19,40";
$MyArray=explode(",",$MyString);
for($a=0;$a<sizeof($MyArray);$a++)
echo $MyArray[$a] . " ";
?>

Feb 12 '08 #8

P: n/a
On Feb 12, 12:12 pm, Betikci Boris <pard...@gmail.comwrote:
On Feb 11, 6:02 pm, Jack <ironwoodcan...@gmail.comwrote:
I would like to Explode a string into an array that does not begin at
0 but I can't get it to work.
For example: $MyInfo = array(1 =27,68,31,19,40);
will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40
But, I'd like to process a string: "27,68,31,19,40" such that it would
get the same result.
I tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =$MyString);
But that doesn't work.
I also tried:
$MyString = "27,68,31,19,40"
$MyInfo = array(1 =explode($MyString, ","));
That doesn't do it either.
How can this be done?

-------------
<?
$MyString = "27,68,31,19,40";
$MyArray=explode(",",$MyString);
for($a=0;$a<sizeof($MyArray);$a++)
echo $MyArray[$a] . " ";
?>
<?
$MyString = "27,68,31,19,40";
$MyArray=explode(",",$MyString);
for($a=0;$a<sizeof($MyArray);$a++)
$MyTempArray[$a+1]=$MyArray[$a];

for($a=0;$a<sizeof($MyArray);$a++)
$MyArray[$a]=$MyTempArray[$a];

echo $MyArray[1] //Output would be 27;
?>
Feb 12 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.