looping two arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stefano.troiani@gmail.com

    looping two arrays

    Hi,

    i have two arrays and I would like to loop both of them one inside the
    other.
    The first one comes from a readdir and present an array with the
    document's names, the second one is an array with the names i want to
    appear for that documents as a link.
    My need is to sort both of them so that i got something like this:
    <a href="first element of array 1" first element of array 2</a>
    <a href="second element of array 1" second element of array 2</a>
    ............... ............... ............

    but i don't know how to do it.

    Thanks for any help,

    stefano

  • Mateusz Markowski

    #2
    Re: looping two arrays

    stefano.troiani @gmail.com napisal(a):
    Hi,
    >
    i have two arrays and I would like to loop both of them one inside the
    other.
    The first one comes from a readdir and present an array with the
    document's names, the second one is an array with the names i want to
    appear for that documents as a link.
    My need is to sort both of them so that i got something like this:
    <a href="first element of array 1" first element of array 2</a>
    <a href="second element of array 1" second element of array 2</a>
    ............... ............... ...........
    >
    but i don't know how to do it.
    $array1 = array('first', 'second');
    $array2 = array('one', 'two');
    for ($i=0; $i<count($array 1); $i++) {
    echo "<a href=\"".$array 1[$i]."\" >".$array2[$i]."</a>";
    }

    Comment

    • Erwin Moller

      #3
      Re: looping two arrays

      stefano.troiani @gmail.com wrote:
      Hi,
      >
      i have two arrays and I would like to loop both of them one inside the
      other.
      The first one comes from a readdir and present an array with the
      document's names, the second one is an array with the names i want to
      appear for that documents as a link.
      My need is to sort both of them so that i got something like this:
      <a href="first element of array 1" first element of array 2</a>
      <a href="second element of array 1" second element of array 2</a>
      ............... ............... ...........
      >
      but i don't know how to do it.
      >
      Thanks for any help,
      >
      stefano
      Hi Stefano,

      That is very straightforward .
      I assume the arrays are both the same length.
      (if not your setup makes little sense.)

      <?
      // both arrays are filled by your code
      $arrHyperlink = array(.....);
      $arrHyperlinkTe xt = array(.....);

      for ($i=0;$i<count( $arrHyperlink); $i++){
      ?>
      <a href="<?= $arrHyperlink[$i] ?>"><?= $arrHyperlinkTe xt[$i] ?></a>
      <?
      }
      ?>

      Simple he? Or did I miss someting? (Happens the whole day to me. :P)

      Regards,
      Erwin Moller

      Comment

      • friglob

        #4
        Re: looping two arrays

        stefano.troiani @gmail.com wrote:
        Hi,
        >
        i have two arrays and I would like to loop both of them one inside the
        other.
        The first one comes from a readdir and present an array with the
        document's names, the second one is an array with the names i want to
        appear for that documents as a link.
        My need is to sort both of them so that i got something like this:
        <a href="first element of array 1" first element of array 2</a>
        <a href="second element of array 1" second element of array 2</a>
        ............... ............... ...........
        >
        but i don't know how to do it.
        >
        Thanks for any help,
        >
        stefano
        >

        foreach( paths_array AS $key =$path ){

        // if document in path exists
        if( file_exists($pa th) ){

        // if there is a name for this document, set defined name else set name as "no name"
        ( $names_array[$key] ) ? $name = $names_array[$key] : $name = "no name";

        // print link with document path and document name
        print "<a href=\"".$path. "\">$name</a>";

        }

        }

        Comment

        • Rik

          #5
          Re: looping two arrays

          David Gillen wrote:
          No over head of $i, $i < comparison, and $i increment.
          foreach is a far too underused language construct. The other
          solutions posted are perfectly workable, I just have a strong
          preference for foreach when working with arrays.
          Indeed, and it works with named keys, also a handy feature..
          --
          Rik Wasmus


          Comment

          • Rik

            #6
            Re: looping two arrays

            stefano.troiani @gmail.com wrote:
            Hi,
            >
            i have two arrays and I would like to loop both of them one inside the
            other.
            The first one comes from a readdir and present an array with the
            document's names, the second one is an array with the names i want to
            appear for that documents as a link.
            My need is to sort both of them so that i got something like this:
            <a href="first element of array 1" first element of array 2</a>
            <a href="second element of array 1" second element of array 2</a>
            ............... ............... ...........
            >
            How exactly are you linking the document names and descriptions?
            If link them purelay on the numerical indexes in both arrays, with one to
            one relations, try:

            $combined = array_map(null, $files,$descipt ions);
            foreach($combin ed as $link){
            vprintf('<a href="%s">%s</a>',$link);
            }
            --
            Grtz,

            Rik Wasmus


            Comment

            • stefano.troiani@gmail.com

              #7
              Re: looping two arrays


              Rik wrote:
              stefano.troiani @gmail.com wrote:
              Hi,

              i have two arrays and I would like to loop both of them one inside the
              other.
              The first one comes from a readdir and present an array with the
              document's names, the second one is an array with the names i want to
              appear for that documents as a link.
              My need is to sort both of them so that i got something like this:
              <a href="first element of array 1" first element of array 2</a>
              <a href="second element of array 1" second element of array 2</a>
              ............... ............... ...........
              >
              How exactly are you linking the document names and descriptions?
              If link them purelay on the numerical indexes in both arrays, with one to
              one relations, try:
              >
              $combined = array_map(null, $files,$descipt ions);
              foreach($combin ed as $link){
              vprintf('<a href="%s">%s</a>',$link);
              }
              --
              Grtz,
              >
              Rik Wasmus

              Thanks!!! this is what I was looking for :)

              stefano

              Comment

              Working...