473,698 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("function s", "events and groups");
$m[3] = array("activiti es", "things to do", "exclusive activities");
$m[4] = array("enquirie s", "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_curre nt(&$array, $key){
reset($array);
while(current($ array)){
if(key($array) == $key){
break;
}
next($array);
}
}

TIA

Nicolaas
Jan 28 '06 #1
3 1274
windandwaves wrote:
Hi Folk

I have the following array:
$m = array();
$m[0] = array("lodgings ", "rooms");
$m[1] = array("location ", "there and away");
$m[2] = array("function s", "events and groups");
$m[3] = array("activiti es", "things to do", "exclusive activities");
$m[4] = array("enquirie s", "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_curre nt(&$array, $key){
reset($array);
while(current($ array)){
if(key($array) == $key){
break;
}
next($array);
}
}

TIA

Nicolaas

function find_word($arra y, $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-

Jan 28 '06 #2
Al
David Haynes wrote:
function find_word($arra y, $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("function s", "events and groups");
$m[3] = array("activiti es", "things to do", "exclusive activities");
$m[4] = array("enquirie s", "tariffs");
$m[5] = array("download ", "media info");

echo ext_array_searc h("download", $m);
function ext_array_searc h($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($need le, $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.

Jan 28 '06 #3
Al wrote:
David Haynes wrote:
function find_word($arra y, $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("function s", "events and groups");
$m[3] = array("activiti es", "things to do", "exclusive activities");
$m[4] = array("enquirie s", "tariffs");
$m[5] = array("download ", "media info");

echo ext_array_searc h("download", $m);
function ext_array_searc h($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($need le, $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
Jan 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2003
by: Joshua Beall | last post by:
Hi All, I cannot turn off magic quotes, since I am just leasing space from a hosting company, and they determine the magic_quotes settings. I realize in retrospect that using a .htaccess file to turn magic quotes would probably be better, and I am going to switch to that solution, but I am still trying to figure out what is causing my current problem: I am using the following code to automatically strip out any slashes that were...
19
2844
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type > >pointer-to-array-of-size-N-of-type-T (which is fine) and not having type > >array-of-size-N-of-type-T (with some exceptions, which is curious). > > So far > >the consensus seems to be that while everyone is aware of this no one knows
5
29245
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir); pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") + ld.GetFiles("*.master.resx"); but of course there is no + operation allowed on the FileInfo arrays returned by the GetFiles method.
1
8703
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections Framework are said to have an element type. http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
0
1069
by: silvrique | last post by:
I am totally lost. I have an xml script example as follows: <?xml version="1.0" encoding="utf8" ?> <starter> <second> <name>Silver</name> </second> </starter>
41
4956
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
3
2015
by: c++dummy | last post by:
I got this project for my class and I'm totally lost as to how to copy the 1d array with the bone name into a 2d array using this supposed strncpy function I'm supposed to create. I believe the teacher wants us to make the function and not use the library function. Here's the info: Write a C++ program that
1
4016
by: hello12 | last post by:
hello, i have 2 upper triangular matrices A and B.. the values are stored in efficient format in the text file. I wanted to use those values for matrix multiplication and display the result C on the screen. I am supposed to use 1d arrays. SO, basically i have 2 --> 1d arrays which i am supposed to multiply to get C.C must not store zeroes(i.e it must also be in efficient format). I am having trouble in the multiplication part... i am...
1
1205
by: Bill Cunningham | last post by:
In a previous post I mentioned a beautifully working simple function called out with this prototype: int out (int n,char *buffer); Great. But how would I handle the command line? I have arrays and sub-arrays char *argv and argc. int main (int argc, char *argv) { /* In one of these two parameters I am going to have to take a
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.