Help Understanding Arrays

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

    Help Understanding Arrays

    I don't quite understand this. $result is the result of a mysql_query.

    $requests = array();
    while ($request = mysql_fetch_arr ay($result))
    {
    print("<p>");
    print_r($reques t);
    array_push($req uests, $request);
    }

    while ($request = each($requests) )
    {
    print("<p>");
    print_r($reques t);
    }

    My thinking is that the while..each loop does the opposite of
    array_push. However, this doesn't seem to be the case.

    $request in the first loop looks like a single associative array.
    $request in the second loop is a multidimensiona l associative array.

    Can someone explain to me what is happening here?

    --Bruce

  • Pedro Graca

    #2
    Re: Help Understanding Arrays

    Bruce wrote:[color=blue]
    > while ($request = each($requests) )
    > {
    > print("<p>");
    > print_r($reques t);
    > }
    >
    > My thinking is that the while..each loop does the opposite of
    > array_push. However, this doesn't seem to be the case.
    >
    > $request in the first loop looks like a single associative array.
    > $request in the second loop is a multidimensiona l associative array.
    >
    > Can someone explain to me what is happening here?[/color]

    You're misusing the each() function.

    <quote src="http://www.php.net/each">
    Returns the current key and value pair from the array array and
    advances the array cursor. This pair is returned in a four-element
    array, with the keys 0, 1, key, and value. Elements 0 and key contain
    the key name of the array element, and 1 and value contain the data.
    </quote>


    Try this:

    while (list($request_ k, $request_v) = each($requests) )
    {
    print("<p>");
    print_r($reques t_v);
    }


    or, with foreach() (which I like much more then list() and each()):

    foreach ($requests as $request)
    {
    print("<p>");
    print_r($requet s);
    }


    --
    USENET would be a better place if everybody read: | to mail me: simply |
    http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
    http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
    http://www.expita.com/nomime.html | and *NO* attachments. |

    Comment

    • Michael Fesser

      #3
      Re: Help Understanding Arrays

      .oO(Bruce)
      [color=blue]
      >[...]
      >
      >My thinking is that the while..each loop does the opposite of
      >array_push. However, this doesn't seem to be the case.[/color]

      Correct. The opposite of array_push() is array_pop().
      [color=blue]
      >$request in the first loop looks like a single associative array.[/color]

      Correct.
      [color=blue]
      >$request in the second loop is a multidimensiona l associative array.[/color]

      You should have a look at the manual. each() returns a four-element
      array which contains the key and the value of the current element from
      the given array.

      Return the current key and value pair from an array and advance the array cursor


      Micha

      Comment

      Working...