Array Processing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J Huntley Palmer

    Array Processing

    How may I concatenate the index respective questions and answers into
    one string per index using ONE foreach loop? I do not want to use
    numerical indexes just the named ones as below.

    In other words, in one foreach loop, if possible, I want to have the
    results as question0+answe r0 and question1+answe r1 in 2 separate
    strings entered into an array.

    Thanks much.

    Array
    (
    [0] => Array
    (
    [0] => question0
    [1] => question1
    )

    [QUEST] => Array
    (
    [0] => question0
    [1] => question1
    )

    [ANSW] => Array
    (
    [0] => answer0
    [1] => answer1
    )

    [1] => Array
    (
    [0] => answer0
    [1] => answer1
    )

    )
  • Sjoerd

    #2
    Re: Array Processing


    J Huntley Palmer wrote:[color=blue]
    > How may I concatenate the index respective questions and answers into
    > one string per index using ONE foreach loop? I do not want to use
    > numerical indexes just the named ones as below.[/color]

    Do you mean this:

    for ($i = 0; $i < count($arr[0]); $i++) {
    $result[] = $arr[0][$i].$arr[1][$i];
    }

    Comment

    • J Huntley Palmer

      #3
      Re: Array Processing

      Sjoerd wrote:[color=blue]
      > J Huntley Palmer wrote:[color=green]
      >> How may I concatenate the index respective questions and answers into
      >> one string per index using ONE foreach loop? I do not want to use
      >> numerical indexes just the named ones as below.[/color]
      >
      > Do you mean this:
      >
      > for ($i = 0; $i < count($arr[0]); $i++) {
      > $result[] = $arr[0][$i].$arr[1][$i];
      > }
      >[/color]

      Actually I wanter to use the named indexes such as [QUEST] AND [ANSW]
      not the numerical ones.

      Array
      (
      [0] => Array
      (
      [0] => question0
      [1] => question1
      )

      [QUEST] => Array
      (
      [0] => question0
      [1] => question1
      )

      [ANSW] => Array
      (
      [0] => answer0
      [1] => answer1
      )

      [1] => Array
      (
      [0] => answer0
      [1] => answer1
      )

      )
      Thanks

      Comment

      • J Huntley Palmer

        #4
        Re: Array Processing

        Sjoerd wrote:[color=blue]
        > J Huntley Palmer wrote:[color=green]
        >> How may I concatenate the index respective questions and answers into
        >> one string per index using ONE foreach loop? I do not want to use
        >> numerical indexes just the named ones as below.[/color]
        >
        > Do you mean this:
        >
        > for ($i = 0; $i < count($arr[0]); $i++) {
        > $result[] = $arr[0][$i].$arr[1][$i];
        > }
        >[/color]


        This will also create an out of bounds error since there are named
        indexes mixed in.

        Comment

        • Oli Filth

          #5
          Re: Array Processing

          J Huntley Palmer said the following on 03/03/2006 17:43:[color=blue]
          > How may I concatenate the index respective questions and answers into
          > one string per index using ONE foreach loop? I do not want to use
          > numerical indexes just the named ones as below.
          >
          > In other words, in one foreach loop, if possible, I want to have the
          > results as question0+answe r0 and question1+answe r1 in 2 separate
          > strings entered into an array.
          >
          > Thanks much.
          >
          > Array
          > (
          > [0] => Array
          > (
          > [0] => question0
          > [1] => question1
          > )
          >
          > [QUEST] => Array
          > (
          > [0] => question0
          > [1] => question1
          > )
          >
          > [ANSW] => Array
          > (
          > [0] => answer0
          > [1] => answer1
          > )
          >
          > [1] => Array
          > (
          > [0] => answer0
          > [1] => answer1
          > )
          >
          > )[/color]

          $stuff = array("QUEST" => array(0 => "question0" , 1 => "question1" ),
          "ANSW" => array(0 => "answer0", 1 => "answer1")) ;


          $strings = null;
          foreach ($stuff["QUEST"] as $key => $value)
          {
          $strings[$key] = $value . $stuff["ANSW"][$key];
          }



          --
          Oli

          Comment

          • Janwillem Borleffs

            #6
            Re: Array Processing

            J Huntley Palmer wrote:[color=blue]
            > How may I concatenate the index respective questions and answers into
            > one string per index using ONE foreach loop? I do not want to use
            > numerical indexes just the named ones as below.
            >
            > In other words, in one foreach loop, if possible, I want to have the
            > results as question0+answe r0 and question1+answe r1 in 2 separate
            > strings entered into an array.
            >[/color]

            How do you want to establish the relationship between the questions and
            answers? With your example array, only the following would make sence:

            $results = array(
            $array['QUEST'][0] . $array['ANSWER'][0],
            $array['QUEST'][1] . $array['ANSWER'][1]
            );


            JW



            Comment

            Working...