help with 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
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

help with array

Post by mcoelho123 »

How can i do to increment the key value with code, like this example

This is what i have

Code: Select all

$email[0] = value1
$email[1] = value2
$email[2] = value3
$email[3] = value4
$email[4] = value5
And i want the result like this:

Code: Select all

$email[1] = value1
$email[2] = value2
$email[3] = value3
$email[4] = value4
$email[5] = value5
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

to keep code generic do this:

Code: Select all

for ($i = 0; $i < 5; $i++)
{
echo "\$email[$i] = " . $email[$i] . '<BR>';
}
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

Post by mcoelho123 »

maybe i dont explain well

i want that the keys itself to increment

because i use the explode() and the key starts with 0, like 0,1,2,3,4,5, ...

and i want the key to start in number 1 not 0
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

mcoelho123 wrote:maybe i dont explain well

i want that the keys itself to increment

because i use the explode() and the key starts with 0, like 0,1,2,3,4,5, ...

and i want the key to start in number 1 not 0
simply how php uses keys internally, why do you need it to start on 1?

Code: Select all

$array = array('blah1', 'blah2', 'blah3');

foreach ($array as $key => $piece)
{
   $newArray[$key+1] = $piece;
}
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

Post by mcoelho123 »

because i want to assign the same values and the same keys, or there are another way to post an array?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

mcoelho123 wrote:because i want to assign the same values and the same keys, or there are another way to post an array?
]

I don't understand.
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

Post by mcoelho123 »

i have this array in a page

Code: Select all

$email[1] = "caselcoop@caselcoop.pt";
$email[2] = "webmaster@caselcoop.pt";
$email[3] = "marco@caselcoop.pt";
$email[4] = "carlos.coradinho@caselcoop.pt";
$email[5] = "anabela.fialho@caselcoop.pt";
$email[6] = "jorge.ramelhete@caselcoop.pt";
and i want this array in other page without copy and past because if i need to modify, i only modify in one page

So i use implode before post in one page and explode the array on the second.
But when i do the explode the keys are not the same
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

maybe try serialize ( http://www.php.net/serialize ) and unserialize ( http://www.php.net/unserialize )
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

Post by mcoelho123 »

how, i tried this

Code: Select all

$mails = $_POST['mails'];

$email[1] = "caselcoop@caselcoop.pt";
$email[2] = "webmaster@caselcoop.pt";
$email[3] = "marco@caselcoop.pt";
$email[4] = "carlos.coradinho@caselcoop.pt";
$email[5] = "anabela.fialho@caselcoop.pt";
$email[6] = "jorge.ramelhete@caselcoop.pt";
$mails = serialize($email);

Code: Select all

$mails = unserialize($mails) ;

foreach ($mails as $key => $value) {
  echo $key . "=>" . $value . "<br>";

}
and dont work
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

mcoelho123 wrote:i have this array in a page

Code: Select all

$email[1] = "caselcoop@caselcoop.pt";
$email[2] = "webmaster@caselcoop.pt";
$email[3] = "marco@caselcoop.pt";
$email[4] = "carlos.coradinho@caselcoop.pt";
$email[5] = "anabela.fialho@caselcoop.pt";
$email[6] = "jorge.ramelhete@caselcoop.pt";
and i want this array in other page without copy and past because if i need to modify, i only modify in one page.
Why no just include() the file within the other files?
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

Post by mcoelho123 »

OK, good
Thanks a lot
Post Reply