array key reassignment methodology

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PhilM

    #1

    array key reassignment methodology

    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 multidimensiona l 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('fundrai se'=>'Fundraise ');
    $links[6]=array('events' =>'Events');
    $links[7]=array('contact '=>'Contact');
    ///// GALLERY LIST /////
    $gallery[0]=array('service '=>'Service','r eception'=>'Rec eption');
    $gallery[3]=array('family' =>'Family','chi ldren'=>'Childr en','adults'=>' Adult
    s');
    $gallery[5]=array('cards'= >'Cards','leafl ets'=>'Leaflets ');









  • PhilM

    #2
    Re: array key reassignment methodology


    "PhilM" <philm@nospam.c om.am> wrote in message
    news:42fbfceb$0 $8999$afc38c87@ news.optusnet.c om.au...[color=blue]
    > I would like some assistance in methodology to reassign/reset array keys[/color]
    to[color=blue]
    > 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[/color]
    key[color=blue]
    > value.
    >
    > The output below was produced using my config generator form, which[/color]
    enables[color=blue]
    > 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('fundrai se'=>'Fundraise ');
    > $links[6]=array('events' =>'Events');
    > $links[7]=array('contact '=>'Contact');
    > ///// GALLERY LIST /////
    > $gallery[0]=array('service '=>'Service','r eception'=>'Rec eption');
    >[/color]
    $gallery[3]=array('family' =>'Family','chi ldren'=>'Childr en','adults'=>' Adult[color=blue]
    > s');
    > $gallery[5]=array('cards'= >'Cards','leafl ets'=>'Leaflets ');
    >[/color]
    ok, this is what I eventually came up with... is there a smoother way?

    $indexer=0;
    foreach (array_keys($li nks) as $linkKey){
    $collecter[$indexer]=$linkKey;
    $indexer++;
    }
    foreach (array_keys($co llecter) as $linkKey){
    if ($links[$collecter[$linkKey]]){
    $linksHolder[$linkKey]=$links[$collecter[$linkKey]];}
    if ($gallery[$collecter[$linkKey]]){
    $galleryHolder[$linkKey]=$gallery[$collecter[$linkKey]];}
    }
    $links=$linksHo lder;$gallery=$ galleryHolder;


    Comment

    • ZeldorBlat

      #3
      Re: array key reassignment methodology

      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('fundrais e'=>'Fundraise' );
      $myArray[6]['links'] = array('events'= >'Events');
      $myArray[7]['links'] = array('contact' =>'Contact');
      ///// GALLERY LIST /////
      $myArray[0]['gallery'] =
      array('service' =>'Service','re ception'=>'Rece ption');
      $myArray[3]['gallery'] =
      array('family'= >'Family','chil dren'=>'Childre n','adults'=>'A dult
      s');
      $myArray[5]['gallery'] =
      array('cards'=> 'Cards','leafle ts'=>'Leaflets' );

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

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

      Comment

      • PhilM

        #4
        Re: array key reassignment methodology

        [color=blue]
        > First off, why is consecutive numbering important? It may or may not
        > be, depending on what you're trying to accomplish.[/color]
        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).
        [color=blue]
        > 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('fundrais e'=>'Fundraise' );
        > $myArray[6]['links'] = array('events'= >'Events');
        > $myArray[7]['links'] = array('contact' =>'Contact');
        > ///// GALLERY LIST /////
        > $myArray[0]['gallery'] =
        > array('service' =>'Service','re ception'=>'Rece ption');
        > $myArray[3]['gallery'] =
        > array('family'= >'Family','chil dren'=>'Childre n','adults'=>'A dult
        > s');
        > $myArray[5]['gallery'] =
        > array('cards'=> 'Cards','leafle ts'=>'Leaflets' );
        >
        > Then you can re-index the whole thing in a continuous way without
        > losing your link/gallery association:
        >
        > $myArray = array_merge($my Array); //just re-indexes the array
        >[/color]

        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 }
        ???


        Comment

        • ZeldorBlat

          #5
          Re: array key reassignment methodology

          Sure, or to be very thorough, you could do:

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

          Comment

          • Chung Leong

            #6
            Re: array key reassignment methodology

            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.

            Comment

            • PhilM

              #7
              Re: array key reassignment methodology


              "ZeldorBlat " <zeldorblat@gma il.com> wrote in message
              news:1123856092 .428970.143270@ g47g2000cwa.goo glegroups.com.. .[color=blue]
              > Sure, or to be very thorough, you could do:
              >
              > if(is_array($my Array[0]['gallery'])) { //do gallery stuff }[/color]

              thx again.


              Comment

              • PhilM

                #8
                Re: array key reassignment methodology

                "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
                news:1123858762 .922486.265180@ g49g2000cwa.goo glegroups.com.. .[color=blue]
                > 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.
                >[/color]
                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.


                Comment

                • Jasen Betts

                  #9
                  Re: array key reassignment methodology

                  In article <42fbfceb$0$899 9$afc38c87@news .optusnet.com.a u>, PhilM wrote:[color=blue]
                  > 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.[/color]
                  [color=blue]
                  > 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('fundrai se'=>'Fundraise ');
                  > $links[6]=array('events' =>'Events');
                  > $links[7]=array('contact '=>'Contact');
                  > ///// GALLERY LIST /////
                  > $gallery[0]=array('service '=>'Service','r eception'=>'Rec eption');
                  > $gallery[3]=array('family' =>'Family','chi ldren'=>'Childr en','adults'=>' Adult
                  > s');
                  > $gallery[5]=array('cards'= >'Cards','leafl ets'=>'Leaflets ');
                  >[/color]

                  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

                  Comment

                  Working...