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
mcoelho123
Forum Commoner
Posts: 37 Joined: Tue Jun 06, 2006 6:27 am
Post
by mcoelho123 » Tue Jun 06, 2006 10:26 am
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
MrPotatoes
Forum Regular
Posts: 617 Joined: Wed May 24, 2006 6:42 am
Post
by MrPotatoes » Tue Jun 06, 2006 10:29 am
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 » Tue Jun 06, 2006 10:34 am
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
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Jun 06, 2006 11:14 am
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 » Tue Jun 06, 2006 11:16 am
because i want to assign the same values and the same keys, or there are another way to post an array?
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Jun 06, 2006 11:22 am
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 » Tue Jun 06, 2006 11:27 am
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
PrObLeM
Forum Contributor
Posts: 418 Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:
Post
by PrObLeM » Tue Jun 06, 2006 11:29 am
mcoelho123
Forum Commoner
Posts: 37 Joined: Tue Jun 06, 2006 6:27 am
Post
by mcoelho123 » Tue Jun 06, 2006 11:38 am
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
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Jun 06, 2006 11:47 am
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 » Tue Jun 06, 2006 11:57 am
OK, good
Thanks a lot