damn - it must be the problem of long day + long night @ my desk 😉

anyway - I have an array that has 2 key=>value pairs stored in it.

I'd like to know how can I access those values when I don't know keys? Or how can I access both of them (key and value).

$aTest = array('key1'=>'val1', 'key2'=>'val2');

// how to get the key and value?
// suppose I don't know the key nor value.
echo($aTest ???????);

thank U

    this should work:

    $aTest = array('key1'=>'val1', 'key2'=>'val2');
    // this will split each key/value pair up:
    while (list($key, $value) = each($aTest)) {
      // $key contains the key
      // $value contains the value
      echo ("The key $key has a value of $value<br>\n");
    }
    

      examancers way is more efficient in php, this way is more readable when you look at your code (at least in my opinion), they both do the same thing.

      $aTest = array('key1'=>'val1', 'key2'=>'val2');
      
      foreach ( $array as $key=>$value ) 
      {
      echo ("The key $key has a value of $value<br>\n");
      }
      

      read more here:

      http://www.php.net/manual/en/control-structures.foreach.php
      http://www.php.net/manual/en/function.each.php
      http://www.php.net/manual/en/function.list.php

        Originally posted by ednark
        examancers way is more efficient in php

        Is it? Or is it just a holdover from PHP3?

          well after thinking about your post i said:

          "AHA! what a fool Weedpacket must be: didn't he see me say it was more efficient"

          so i sat down to prove me right, just rub it in your face. However, it turns out i was wrong, at least on my computer... though i can swear i remmber reading in some book that the while( list() = each() ) was faster

          i ran some quick benchmarks using PEAR's Benchmark Iterater class... feel free to run your own test using more complex data types, or just this code on a different architecture

          read more on benchmark iterater at:
          http://pear.php.net/package-info.php?pacid=53

          Specs:
          two arrays of the alphabet, one keyed on integers, one keyed on single characters.

          i ran through each array twice, one grabbing only the values, once grabbing key and value. I did this using the foreach and while techniques.

          here are the results measured in seconds of execution:

          WinXP : PHP 4.2.2

          0.000089 : foreach() val only (integer keys)

          0.000125 : while() val only (integer keys)

          0.000090 : foreach() val only (string keys)

          0.000132 : while() val only (string keys)

          0.000097 : foreach() key and val (integer keys)

          0.000142 : while() key and val (integer keys)

          0.000106 : foreach() key and val (string keys)

          0.000145 : while() key and val (string keys)

          as you can see it turns out the foreach loop wins against the while every time in every instance, though i doubt haggling over the 5th decimal place counts much in your code..

          here is the code i used to benchmark:

          <?php
          
          $int     = array( "a","b","c","d","e",
                            "f","g","h","i","j",
                            "k","l","m","n","o",
                            "p","q","r","s","t",
                            "u","v","w","x","y","z");
          $string  = array( "a"=>"A","b"=>"B","c"=>"C",
                            "d"=>"D","e"=>"E","f"=>"F",
                            "g"=>"G","h"=>"H","i"=>"I",
                            "j"=>"J","k"=>"K","l"=>"L",
                            "m"=>"M","n"=>"N","o"=>"O",
                            "p"=>"P","q"=>"Q","r"=>"R",
                            "s"=>"S","t"=>"T","u"=>"U",
                            "v"=>"V","w"=>"W","x"=>"X",
                            "y"=>"Y","z"=>"Z" );
          
          function loop_foreach_val( $array )
          {
              foreach( $array as $value ) {
                  //$string = $value.$value;
              }
          }
          
          function loop_foreach_key_val( $array )
          {
              foreach( $array as $key=>$value ) {
                  //$string = $key.$value;
              }
          }
          
          
          function loop_while_val( $array )
          {
              while( list(,$value) = each($array) ) {
                  //$string = $value.$value;
              }
          }
          
          function loop_while_key_val( $array )
          {
              while( list($key,$value) = each($array) ) {
                  //$string = $key.$value;
              }
          }
          
          echo "WinXP : PHP ". phpversion() ."<br>";
          
          require_once "Benchmark/Iterate.php";
          $benchmark = new Benchmark_Iterate;
          $runs = 10000;
          
          echo "--------------------<br>";
          
          $benchmark->run($runs, 'loop_foreach_val', $int);
          $result = $benchmark->get();
          echo "{$result['mean']} : foreach() val only (integer keys)<br>";
          
          
          $benchmark->run($runs, 'loop_while_val', $int);
          $result = $benchmark->get();
          echo "{$result['mean']} : while() val only (integer keys)<br>";
          
          echo "--------------------<br>";
          
          $benchmark->run($runs, 'loop_foreach_val', $string);
          $result = $benchmark->get();
          echo "{$result['mean']} :  foreach() val only (string keys)<br>";
          
          $benchmark->run($runs, 'loop_while_val', $string);
          $result = $benchmark->get();
          echo "{$result['mean']} :  while() val only (string keys)<br>";
          
          echo "--------------------<br>";
          
          $benchmark->run($runs, 'loop_foreach_key_val', $int);
          $result = $benchmark->get();
          echo "{$result['mean']} : foreach() key and val (integer keys)<br>";
          
          $benchmark->run($runs, 'loop_while_key_val', $int);
          $result = $benchmark->get();
          echo "{$result['mean']} : while() key and val (integer keys)<br>";
          
          echo "--------------------<br>";
          
          $benchmark->run($runs, 'loop_foreach_key_val', $string);
          $result = $benchmark->get();
          echo "{$result['mean']} :  foreach() key and val (string keys)<br>";
          
          $benchmark->run($runs, 'loop_while_key_val', $string);
          $result = $benchmark->get();
          echo "{$result['mean']} :  while() key and val (string keys)<br>";
          
          echo "--------------------<br>";
          
          ?>
          

            foreach is sexier.

            Also, if you do know the key, try

            echo $aTest['key1'];

            which will echo val1. imho variable names should not use studlyCaps, keep them lowercase.

              foreach is sexier.

              Also, if you do know the key, try

              echo $aTest['key1'];

              which will echo val1. imho variable names should not use studlyCaps, keep them lowercase.

                Write a Reply...