Connecting Tech Pros Worldwide Help | Site Map

lost with arrays

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 28th, 2006, 12:35 AM
windandwaves
Guest
 
Posts: n/a
Default lost with arrays

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



  #2  
Old January 28th, 2006, 12:55 AM
David Haynes
Guest
 
Posts: n/a
Default 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-

  #3  
Old January 28th, 2006, 04:35 AM
Al
Guest
 
Posts: n/a
Default 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.

  #4  
Old January 28th, 2006, 08:35 AM
windandwaves
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,989 network members.