Inserting into an array...

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

    #1

    Inserting into an array...

    Hi

    if I have an array of [a, b, d] and want to insert c, what is the way
    then?

    Sonnich

  • Tyrone Slothrop

    #2
    Re: Inserting into an array...

    On 23 Mar 2007 06:30:46 -0700, "Sonnich"
    <sonnich.jensen @elektrobit.com wrote:
    >Hi
    >
    >if I have an array of [a, b, d] and want to insert c, what is the way
    >then?
    >
    >Sonnich
    array_push()

    You can always run sort if you want in alphabetic order.

    Comment

    • Erwin Moller

      #3
      Re: Inserting into an array...

      Sonnich wrote:
      Hi
      >
      if I have an array of [a, b, d] and want to insert c, what is the way
      then?
      >
      Sonnich
      $testArr = array('a','b',' d');
      // one of the 1000 ways to manipulate an array in PHP:
      $testArr[] = 'c';
      // [] without a number or string means just add at the end

      // results in an array 'a','b','d','c'

      If you want to sort it, use sort() or usort() if you need anything fancy.

      Regards,
      Erwin Moller

      Comment

      • shimmyshack

        #4
        Re: Inserting into an array...

        On 23 Mar, 13:30, "Sonnich" <sonnich.jen... @elektrobit.com wrote:
        Hi
        >
        if I have an array of [a, b, d] and want to insert c, what is the way
        then?
        >
        Sonnich

        $array = array('a','b',' d');
        $array[] = 'c';

        //and if you want the array to be sorted alphabetically then use
        sort($array);

        //view result
        //print_r($array) ;




        Comment

        • shimmyshack

          #5
          Re: Inserting into an array...

          On 23 Mar, 13:46, "shimmyshac k" <matt.fa...@gma il.comwrote:
          On 23 Mar, 13:30, "Sonnich" <sonnich.jen... @elektrobit.com wrote:
          >
          Hi
          >
          if I have an array of [a, b, d] and want to insert c, what is the way
          then?
          >
          Sonnich
          >
          $array = array('a','b',' d');
          $array[] = 'c';
          >
          //and if you want the array to be sorted alphabetically then use
          sort($array);
          >
          //view result
          //print_r($array) ;
          you just pipped me Erwin - soz!

          Comment

          • Sonnich

            #6
            Re: Inserting into an array...

            On Mar 23, 3:45 pm, Erwin Moller
            <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
            Sonnich wrote:
            Hi
            >
            if I have an array of [a, b, d] and want to insert c, what is the way
            then?
            >
            Sonnich
            >
            $testArr = array('a','b',' d');
            // one of the 1000 ways to manipulate an array in PHP:
            $testArr[] = 'c';
            // [] without a number or string means just add at the end
            >
            // results in an array 'a','b','d','c'
            >
            If you want to sort it, use sort() or usort() if you need anything fancy.
            >
            Regards,
            Erwin Moller
            I am inserting but cannot sort it, as I have 3 arrays linked together,
            and the codes are not as ligical as here.
            IT is a tree (like e.g. of folders) with another array with e.g.
            properties or anything.

            Therefore I use the array_search which tells where something is, then
            after that I want to insert something.

            /S


            Comment

            • Erwin Moller

              #7
              Re: Inserting into an array...

              shimmyshack wrote:
              On 23 Mar, 13:46, "shimmyshac k" <matt.fa...@gma il.comwrote:
              >On 23 Mar, 13:30, "Sonnich" <sonnich.jen... @elektrobit.com wrote:
              >>
              Hi
              >>
              if I have an array of [a, b, d] and want to insert c, what is the way
              then?
              >>
              Sonnich
              >>
              >$array = array('a','b',' d');
              >$array[] = 'c';
              >>
              >//and if you want the array to be sorted alphabetically then use
              >sort($array) ;
              >>
              >//view result
              >//print_r($array) ;
              >
              you just pipped me Erwin - soz!
              Hehe, no problem of course.
              I think it is funny to see how we both gave ALMOST the same example. ;-)

              Regards,
              Erwin Moller

              Comment

              • Erwin Moller

                #8
                Re: Inserting into an array...

                Sonnich wrote:
                On Mar 23, 3:45 pm, Erwin Moller
                <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
                >Sonnich wrote:
                Hi
                >>
                if I have an array of [a, b, d] and want to insert c, what is the way
                then?
                >>
                Sonnich
                >>
                >$testArr = array('a','b',' d');
                >// one of the 1000 ways to manipulate an array in PHP:
                >$testArr[] = 'c';
                >// [] without a number or string means just add at the end
                >>
                >// results in an array 'a','b','d','c'
                >>
                >If you want to sort it, use sort() or usort() if you need anything fancy.
                >>
                >Regards,
                >Erwin Moller
                >
                I am inserting but cannot sort it, as I have 3 arrays linked together,
                and the codes are not as ligical as here.
                IT is a tree (like e.g. of folders) with another array with e.g.
                properties or anything.
                >
                Therefore I use the array_search which tells where something is, then
                after that I want to insert something.
                >
                /S

                Erm, allright.

                But could you rephrase your problem, because now I can only guess.
                What is it you need excactly?

                Regards,
                Erwin Moller

                Comment

                • Sonnich

                  #9
                  Re: Inserting into an array...

                  On Mar 23, 4:26 pm, Erwin Moller
                  <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
                  Sonnich wrote:
                  On Mar 23, 3:45 pm, Erwin Moller
                  <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
                  Sonnich wrote:
                  Hi
                  >
                  if I have an array of [a, b, d] and want to insert c, what is the way
                  then?
                  >
                  Sonnich
                  >
                  $testArr = array('a','b',' d');
                  // one of the 1000 ways to manipulate an array in PHP:
                  $testArr[] = 'c';
                  // [] without a number or string means just add at the end
                  >
                  // results in an array 'a','b','d','c'
                  >
                  If you want to sort it, use sort() or usort() if you need anything fancy.
                  >
                  Regards,
                  Erwin Moller
                  >
                  I am inserting but cannot sort it, as I have 3 arrays linked together,
                  and the codes are not as ligical as here.
                  IT is a tree (like e.g. of folders) with another array with e.g.
                  properties or anything.
                  >
                  Therefore I use the array_search which tells where something is, then
                  after that I want to insert something.
                  >
                  /S
                  >
                  Erm, allright.
                  >
                  But could you rephrase your problem, because now I can only guess.
                  What is it you need excactly?
                  >
                  Regards,
                  Erwin Moller- Hide quoted text -
                  >
                  - Show quoted text -
                  like in delphi, tstringlist.ins ert(id, text)

                  say, array1 and array2, both have 45 items.

                  using
                  $i=array_search ("xx", array1)+1;
                  array1[$i]="xx";
                  array2[$i]="yy";

                  array1 and array2 should now have 46 items, where "xx" was inserted at
                  (after) $i;

                  like (a,b,d) insert c -(a,b,c,d)
                  $i=1 as array[1] = b, therefore d should move ahaed (no 3) and I
                  should get c as no. 2





                  Comment

                  • Erwin Moller

                    #10
                    Re: Inserting into an array...

                    Sonnich wrote:
                    On Mar 23, 4:26 pm, Erwin Moller
                    <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
                    >Sonnich wrote:
                    On Mar 23, 3:45 pm, Erwin Moller
                    <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
                    >Sonnich wrote:
                    Hi
                    >>
                    if I have an array of [a, b, d] and want to insert c, what is the
                    way then?
                    >>
                    Sonnich
                    >>
                    >$testArr = array('a','b',' d');
                    >// one of the 1000 ways to manipulate an array in PHP:
                    >$testArr[] = 'c';
                    >// [] without a number or string means just add at the end
                    >>
                    >// results in an array 'a','b','d','c'
                    >>
                    >If you want to sort it, use sort() or usort() if you need anything
                    >fancy.
                    >>
                    >Regards,
                    >Erwin Moller
                    >>
                    I am inserting but cannot sort it, as I have 3 arrays linked together,
                    and the codes are not as ligical as here.
                    IT is a tree (like e.g. of folders) with another array with e.g.
                    properties or anything.
                    >>
                    Therefore I use the array_search which tells where something is, then
                    after that I want to insert something.
                    >>
                    /S
                    >>
                    >Erm, allright.
                    >>
                    >But could you rephrase your problem, because now I can only guess.
                    >What is it you need excactly?
                    >>
                    >Regards,
                    >Erwin Moller- Hide quoted text -
                    >>
                    >- Show quoted text -
                    >
                    like in delphi, tstringlist.ins ert(id, text)
                    >
                    say, array1 and array2, both have 45 items.
                    >
                    using
                    $i=array_search ("xx", array1)+1;
                    array1[$i]="xx";
                    array2[$i]="yy";
                    >
                    array1 and array2 should now have 46 items, where "xx" was inserted at
                    (after) $i;
                    >
                    like (a,b,d) insert c -(a,b,c,d)
                    $i=1 as array[1] = b, therefore d should move ahaed (no 3) and I
                    should get c as no. 2
                    Aha,

                    I am not aware of a function that works excactly like that, but go to
                    www.php.net, and look up the functions:
                    array_splice()
                    and
                    array_slice()

                    They will help you accomplish what you need.

                    Good luck.

                    Regards,
                    Erwin Moller

                    Comment

                    • Sonnich

                      #11
                      Re: Inserting into an array...

                      On Mar 23, 5:10 pm, Erwin Moller
                      <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
                      Sonnich wrote:
                      On Mar 23, 4:26 pm, Erwin Moller
                      <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
                      Sonnich wrote:
                      On Mar 23, 3:45 pm, Erwin Moller
                      <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
                      Sonnich wrote:
                      Hi
                      >
                      if I have an array of [a, b, d] and want to insert c, what is the
                      way then?
                      >
                      Sonnich
                      >
                      $testArr = array('a','b',' d');
                      // one of the 1000 ways to manipulate an array in PHP:
                      $testArr[] = 'c';
                      // [] without a number or string means just add at the end
                      >
                      // results in an array 'a','b','d','c'
                      >
                      If you want to sort it, use sort() or usort() if you need anything
                      fancy.
                      >
                      Regards,
                      Erwin Moller
                      >
                      I am inserting but cannot sort it, as I have 3 arrays linked together,
                      and the codes are not as ligical as here.
                      IT is a tree (like e.g. of folders) with another array with e.g.
                      properties or anything.
                      >
                      Therefore I use the array_search which tells where something is, then
                      after that I want to insert something.
                      >
                      /S
                      >
                      Erm, allright.
                      >
                      But could you rephrase your problem, because now I can only guess.
                      What is it you need excactly?
                      >
                      Regards,
                      Erwin Moller- Hide quoted text -
                      >
                      - Show quoted text -
                      >
                      like in delphi, tstringlist.ins ert(id, text)
                      >
                      say, array1 and array2, both have 45 items.
                      >
                      using
                      $i=array_search ("xx", array1)+1;
                      array1[$i]="xx";
                      array2[$i]="yy";
                      >
                      array1 and array2 should now have 46 items, where "xx" was inserted at
                      (after) $i;
                      >
                      like (a,b,d) insert c -(a,b,c,d)
                      $i=1 as array[1] = b, therefore d should move ahaed (no 3) and I
                      should get c as no. 2
                      >
                      Aha,
                      >
                      I am not aware of a function that works excactly like that, but go towww.php.net, and look up the functions:
                      array_splice()
                      and
                      array_slice()
                      >
                      They will help you accomplish what you need.
                      This is it:
                      $code1 = array_merge(arr ay_slice($code1 , 0, $key+1), array($new),
                      array_slice($co de1, $key+1));

                      Comment

                      Working...