lost with arrays 
January 28th, 2006, 01:35 AM
| | | |
Hi Folk
I have the following array:
$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");
If I have a string from the array, for example, download, and I want to find
out what number it occures in (i.e. 0 - 5, for download the answer being 5)
then how do I find out? I looked at the array functions, but I am not sure
what to use (arrays are new to me).
Also, I saw this function (which may actually help), what is the & doing on
the first line?
function array_set_current(&$array, $key){
reset($array);
while(current($array)){
if(key($array) == $key){
break;
}
next($array);
}
}
TIA
Nicolaas | 
January 28th, 2006, 01:55 AM
| | | | re: lost with arrays
windandwaves wrote:[color=blue]
> Hi Folk
>
> I have the following array:
> $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");
>
> If I have a string from the array, for example, download, and I want to find
> out what number it occures in (i.e. 0 - 5, for download the answer being 5)
> then how do I find out? I looked at the array functions, but I am not sure
> what to use (arrays are new to me).
>
> Also, I saw this function (which may actually help), what is the & doing on
> the first line?
>
> function array_set_current(&$array, $key){
> reset($array);
> while(current($array)){
> if(key($array) == $key){
> break;
> }
> next($array);
> }
> }
>
> TIA
>
> Nicolaas
>
>[/color]
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;
}
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.
-david- | 
January 28th, 2006, 05:35 AM
| | | | re: lost with arrays
David Haynes wrote:[color=blue]
> 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;
> }[/color]
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;
}
?>
[color=blue]
> 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.[/color]
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. | 
January 28th, 2006, 09:35 AM
| | | | re: lost with arrays
Al wrote:[color=blue]
> David Haynes wrote:[color=green]
>> 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;
>> }[/color]
>
> 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;
> }
>[color=green]
>>[/color]
>[color=green]
>> 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.[/color]
>
> 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.[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|