|
Posted by windandwaves on 10/18/43 11:38
Al wrote:
> David Haynes wrote:
>> function find_word($array, $word) {
>> // for every set of arrays
>> for( $i=0; $i < count($array); $i++ ) {
>> // foreach element in the array expressed as $a
>> foreach( $m[$i] as $a ) {
>> // is $a the word we are looking for?
>> if( $a == $word ) {
>> // yes, return the index into $m
>> return $i;
>> }
>> }
>> }
>> // not found in $m
>> return -1;
>> }
>
> I'd cut that down using good old in_array():
>
> <?php
>
> $m = array();
> $m[0] = array("lodgings", "rooms");
> $m[1] = array("location", "there and away");
> $m[2] = array("functions", "events and groups");
> $m[3] = array("activities", "things to do", "exclusive activities");
> $m[4] = array("enquiries", "tariffs");
> $m[5] = array("download", "media info");
>
> echo ext_array_search("download", $m);
>
>
> function ext_array_search($needle, $haystack) {
> // go through each key
> foreach ($haystack as $key => $straw) {
> // if the current key's array contains our value, return the
> key's number
> if (in_array($needle, $straw)) return $key;
> }
>
> // nothing found so return a standard error code
> return -1;
> }
>
>>
>
>> The &$array says that the array is passed by reference not by value.
>> When called this way, any changes to the contents of $array in the
>> function affect the $array in the calling process. This allows you to
>> pass back more than one value when a function returns or to modify
>> the contents of a variable in situ.
>
> Specifically here it means that the function can set the array's
> "current entry" pointer to the key you pass it as a parameter rather
> than having to return a NEW copy of the array with the pointer set.
>
> And that function just iterates through the array's keys until it gets
> to the one that you pass it, so won't actually help with what you're
> looking for.
Thank you so much guys. That is awesome. It helped me put together a menu
with these variables. It looks great now.
Thank you again
- Nicolaas
[Back to original message]
|