Connecting Tech Pros Worldwide Help | Site Map

lost with arrays

  #1  
Old January 28th, 2006, 01:35 AM
windandwaves
Guest
 
Posts: n/a
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, 01:55 AM
David Haynes
Guest
 
Posts: n/a

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, 05:35 AM
Al
Guest
 
Posts: n/a

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, 09:35 AM
windandwaves
Guest
 
Posts: n/a

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


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with arrays onyris answers 1 July 7th, 2009 09:24 AM
Doing Calculations with arrays outstretchedarm answers 4 September 13th, 2006 06:05 PM
Problem with arrays Ramez T. Mina answers 1 November 18th, 2005 12:06 PM
Typed arrays (was: Is it ANSI or is it compiler dependent?) Canonical Latin answers 19 July 22nd, 2005 11:51 AM