You are here: Re: lost with arrays « PHP Programming Language « IT news, forums, messages
Re: lost with arrays

Posted by windandwaves on 09/29/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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация