Little PHP Tip: Array Iteration that Produces both Key and Value CodeUnit 04 NOV 2009

PHP tutorial logoA PHP quick tip on something I keep on forgetting. Now while the foreach functionality is great in iterating through an array and returning the array’s values, sometimes one wishes to also capture the key against which a particular value is stored and for this one should use the overloaded form of the foreach function (usually foreach ($arr as $value) ), namely:

foreach ($arr as $key => $value)

This returns two variables in the form of $key which is the actual key name used for the array item and $value which is the actual value stored for that particular array item.

Nifty.

If you want to test it out for yourself, simply plug an array of your own making into this little blob of code: 

foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />n";
}

Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.

  • If you’re looking for a quick way to view an array (espeically if it has many children), you can also use:

    print(“”);
    print_r($arr);
    print(“”);

    Pre just helps format it. You can also use var_dump if you’re looking to see the type of data.

  • If you’re looking for a quick way to view an array (espeically if it has many children), you can also use:

    print(“”);
    print_r($arr);
    print(“”);

    Pre just helps format it. You can also use var_dump if you’re looking to see the type of data.

  • Yes of course, the <pre> tag in conjunction with print_r does give one a nice display if you just want to see what’s in an array, but you can’t exactly use those values now can you? :)

    I’m talking specifically about actually using the array values (with keys) for say, building up a SQL string.

  • Inside the empty quotes are meant to be a opening and closing pre tag. Filter stripped them out :)

  • Yes of course, the <pre> tag in conjunction with print_r does give one a nice display if you just want to see what’s in an array, but you can’t exactly use those values now can you? :)

    I’m talking specifically about actually using the array values (with keys) for say, building up a SQL string.

  • Inside the empty quotes are meant to be a opening and closing pre tag. Filter stripped them out :)

  • Yup, spotted it straight away :)

  • Yup, spotted it straight away :)

  • you’ve installed PHP in /usr/local as many people do, then your php.ini file is in /usr/local/php/lib. If you’ve installed PHP somewhere else, then you’ll find it in the corresponding lib directory. For instance, I like to put PHP in /opt/php5, so my php.ini file is in /opt/php5/lib.

  • you’ve installed PHP in /usr/local as many people do, then your php.ini file is in /usr/local/php/lib. If you’ve installed PHP somewhere else, then you’ll find it in the corresponding lib directory. For instance, I like to put PHP in /opt/php5, so my php.ini file is in /opt/php5/lib.