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

array key reassignment methodology

P: n/a
I would like some assistance in methodology to reassign/reset array keys to
consecutive numbering, while keeping in sync a second array that uses the
key of the first as it's key set.

If I have read a solution on php.net, I didn't understand what I was
reading.
At this stage of bumbling around, I am getting rather confused/frustrated.
...and getting mind around multidimensional arrays hurts :)

*** note php4 only available ***
$links is used to generate main page links.
$gallery is used to generate links on the $links page that has the same key
value.

The output below was produced using my config generator form, which enables
adding/deletion of links and associated galleries, which accounts for the
gaps in the $links key.

///// LINK LIST (TOP OF PAGE LINKS) /////
$links[0]=array('home'=>'Home');
$links[1]=array('service'=>'Services');
$links[3]=array('people'=>'Portraits');
$links[5]=array('fundraise'=>'Fundraise');
$links[6]=array('events'=>'Events');
$links[7]=array('contact'=>'Contact');
///// GALLERY LIST /////
$gallery[0]=array('service'=>'Service','reception'=>'Receptio n');
$gallery[3]=array('family'=>'Family','children'=>'Children',' adults'=>'Adult
s');
$gallery[5]=array('cards'=>'Cards','leaflets'=>'Leaflets');



Aug 12 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"PhilM" <ph***@nospam.com.am> wrote in message
news:42**********************@news.optusnet.com.au ...
I would like some assistance in methodology to reassign/reset array keys to consecutive numbering, while keeping in sync a second array that uses the
key of the first as it's key set.
*** note php4 only available ***
$links is used to generate main page links.
$gallery is used to generate links on the $links page that has the same key value.

The output below was produced using my config generator form, which enables adding/deletion of links and associated galleries, which accounts for the
gaps in the $links key.

///// LINK LIST (TOP OF PAGE LINKS) /////
$links[0]=array('home'=>'Home');
$links[1]=array('service'=>'Services');
$links[3]=array('people'=>'Portraits');
$links[5]=array('fundraise'=>'Fundraise');
$links[6]=array('events'=>'Events');
$links[7]=array('contact'=>'Contact');
///// GALLERY LIST /////
$gallery[0]=array('service'=>'Service','reception'=>'Receptio n');
$gallery[3]=array('family'=>'Family','children'=>'Children',' adults'=>'Adult s');
$gallery[5]=array('cards'=>'Cards','leaflets'=>'Leaflets');

ok, this is what I eventually came up with... is there a smoother way?

$indexer=0;
foreach (array_keys($links) as $linkKey){
$collecter[$indexer]=$linkKey;
$indexer++;
}
foreach (array_keys($collecter) as $linkKey){
if ($links[$collecter[$linkKey]]){
$linksHolder[$linkKey]=$links[$collecter[$linkKey]];}
if ($gallery[$collecter[$linkKey]]){
$galleryHolder[$linkKey]=$gallery[$collecter[$linkKey]];}
}
$links=$linksHolder;$gallery=$galleryHolder;
Aug 12 '05 #2

P: n/a
First off, why is consecutive numbering important? It may or may not
be, depending on what you're trying to accomplish.

Why not just combine the two arrays into one to make life easier? For
instance, you might have something like:

$myArray[0]['links'] = array('home'=>'Home');
$myArray[1]['links'] = array('service'=>'Services');
$myArray[3]['links'] = array('people'=>'Portraits');
$myArray[5]['links'] = array('fundraise'=>'Fundraise');
$myArray[6]['links'] = array('events'=>'Events');
$myArray[7]['links'] = array('contact'=>'Contact');
///// GALLERY LIST /////
$myArray[0]['gallery'] =
array('service'=>'Service','reception'=>'Reception ');
$myArray[3]['gallery'] =
array('family'=>'Family','children'=>'Children','a dults'=>'Adult
s');
$myArray[5]['gallery'] =
array('cards'=>'Cards','leaflets'=>'Leaflets');

Then you can re-index the whole thing in a continuous way without
losing your link/gallery association:

$myArray = array_merge($myArray); //just re-indexes the array

Aug 12 '05 #3

P: n/a
First off, why is consecutive numbering important? It may or may not
be, depending on what you're trying to accomplish. Ultimately I want to be able to set the order in which the links appear on
the page. I figure taht it would be easier if they are in the array in the
correct order. I can then display the keys as the position, then reassign
keys on form return(submit).
Why not just combine the two arrays into one to make life easier? For
instance, you might have something like:

$myArray[0]['links'] = array('home'=>'Home');
$myArray[1]['links'] = array('service'=>'Services');
$myArray[3]['links'] = array('people'=>'Portraits');
$myArray[5]['links'] = array('fundraise'=>'Fundraise');
$myArray[6]['links'] = array('events'=>'Events');
$myArray[7]['links'] = array('contact'=>'Contact');
///// GALLERY LIST /////
$myArray[0]['gallery'] =
array('service'=>'Service','reception'=>'Reception ');
$myArray[3]['gallery'] =
array('family'=>'Family','children'=>'Children','a dults'=>'Adult
s');
$myArray[5]['gallery'] =
array('cards'=>'Cards','leaflets'=>'Leaflets');

Then you can re-index the whole thing in a continuous way without
losing your link/gallery association:

$myArray = array_merge($myArray); //just re-indexes the array


Why not indeed ;) Thx for that. Will have a go at it later.
Q:-
To determine if a gallery then exists, I assume I would just have to do
something like:-
if ($myArray[0]['gallery']){ do gallery stuff }
???
Aug 12 '05 #4

P: n/a
Sure, or to be very thorough, you could do:

if(is_array($myArray[0]['gallery'])) { //do gallery stuff }

Aug 12 '05 #5

P: n/a
Just create a new array using array_value(). The original array should
be left alone. Modifying configuration data in the middle of a program
is usually a bad idea.

Aug 12 '05 #6

P: n/a

"ZeldorBlat" <ze********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Sure, or to be very thorough, you could do:

if(is_array($myArray[0]['gallery'])) { //do gallery stuff }


thx again.
Aug 13 '05 #7

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Just create a new array using array_value(). The original array should
be left alone. Modifying configuration data in the middle of a program
is usually a bad idea.

Understood.
What I have set up though, is a config tool that will edit the configuration
file which is separated from the site. When editing is complete, requires a
form submit to transfer to the local site directory where the page can read
it. Then it needs to be uploaded to the live site via ftp.
Odd perhaps, but this whole project is aimed (from my point of view) to the
maintaining an accurate or advanced offline backup of the live site, by
someone who is not web savvy.
Aug 13 '05 #8

P: n/a
In article <42**********************@news.optusnet.com.au>, PhilM wrote:
I would like some assistance in methodology to reassign/reset array keys to
consecutive numbering, while keeping in sync a second array that uses the
key of the first as it's key set. adding/deletion of links and associated galleries, which accounts for the
gaps in the $links key.

///// LINK LIST (TOP OF PAGE LINKS) /////
$links[0]=array('home'=>'Home');
$links[1]=array('service'=>'Services');
$links[3]=array('people'=>'Portraits');
$links[5]=array('fundraise'=>'Fundraise');
$links[6]=array('events'=>'Events');
$links[7]=array('contact'=>'Contact');
///// GALLERY LIST /////
$gallery[0]=array('service'=>'Service','reception'=>'Receptio n');
$gallery[3]=array('family'=>'Family','children'=>'Children',' adults'=>'Adult
s');
$gallery[5]=array('cards'=>'Cards','leaflets'=>'Leaflets');


hmmm... gallery keys to stay synced with links keys?
something like this?

$n=0;
foreach ( $links as $key => $val ){
$links[$key]=NULL;
$links[$n]=$val;
$val=gallery[key];
$gallery[$key]=NULL;
$gallery$[$n]=$val;
$n++;
};

--

Bye.
Jasen
Aug 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.