473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Can't find built-in PHP function which I know exists...

Daz
Hi Everyone. I know this is a stupid question, but I have been sifting
through php.net and I am about ready to pull my hair out over it...

I have seen a function similar to in_array() and array_search() which
returns the 0-based index number of the specified element if it exists.

Please could someone point me in the right direction?

Many thanks!

Daz.

Oct 21 '06 #1
6 1312
Hi Everyone. I know this is a stupid question, but I have been sifting
through php.net and I am about ready to pull my hair out over it...

I have seen a function similar to in_array() and array_search() which
returns the 0-based index number of the specified element if it exists.

Please could someone point me in the right direction?
do you mean reset($array)?
Oct 21 '06 #2
Daz wrote:
I have seen a function similar to in_array() and array_search() which
returns the 0-based index number of the specified element if it exists.

Please could someone point me in the right direction?
I don't think there's such a function built-in into PHP;
but you can build your own ...

<?php
function array_function($needle, $array) {
$n = 0;
foreach ($array as $value) {
if ($needle == $value) return $n;
++$n;
}
}

$data = array('name'=>'forty-two', 'dob'=>'2006-10-21', 'eye-color'=>'brown');
$zero_index = (int)array_function('brown', $data);

### if ($zero_index != 2) {
### // error!
### }
?>

What you would use it for is beyond my imagination :)

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 21 '06 #3
Pedro Graca wrote:
<?php
function array_function($needle, $array) {
$n = 0;
foreach ($array as $value) {
if ($needle == $value) return $n;
++$n;
}
return false;
}

$data = array('name'=>'forty-two', 'dob'=>'2006-10-21', 'eye-color'=>'brown');
$zero_index = (int)array_function('brown', $data);

### if ($zero_index != 2) {
### // error!
### }
?>
--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

I don't check the dodgeit address (very often).
If you *really* need to mail me,
use the address in the Reply-To header.
Oct 21 '06 #4
On 21 Oct 2006 11:40:15 -0700, "Daz" <cu********@gmail.comwrote:
>Hi Everyone. I know this is a stupid question, but I have been sifting
through php.net and I am about ready to pull my hair out over it...

I have seen a function similar to in_array() and array_search() which
returns the 0-based index number of the specified element if it exists.
Isn't that what array_search already does on numerically-indexed arrays?

Or do you want the index of the key for the element, in the array of keys for
the array? In which case it sounds like two array_search() calls:

<?php
$a = array(
'fish' ='fingers',
'dog' ='hot dog',
'cow' ='steak',
);

print array_search(array_search('hot dog', $a), array_keys($a));
?>

Output: 1

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 21 '06 #5
Daz

Pedro Graca wrote:
Pedro Graca wrote:
<?php
function array_function($needle, $array) {
$n = 0;
foreach ($array as $value) {
if ($needle == $value) return $n;
++$n;
}

return false;
}

$data = array('name'=>'forty-two', 'dob'=>'2006-10-21', 'eye-color'=>'brown');
$zero_index = (int)array_function('brown', $data);

### if ($zero_index != 2) {
### // error!
### }
?>
Ok, I am happy yo build my own, I could have just swore that I saw a
function that deoes it.

Thanks for your input.

Oct 22 '06 #6
Daz

Andy Hassall wrote:
On 21 Oct 2006 11:40:15 -0700, "Daz" <cu********@gmail.comwrote:
Hi Everyone. I know this is a stupid question, but I have been sifting
through php.net and I am about ready to pull my hair out over it...

I have seen a function similar to in_array() and array_search() which
returns the 0-based index number of the specified element if it exists.

Isn't that what array_search already does on numerically-indexed arrays?

Or do you want the index of the key for the element, in the array of keys for
the array? In which case it sounds like two array_search() calls:

<?php
$a = array(
'fish' ='fingers',
'dog' ='hot dog',
'cow' ='steak',
);

print array_search(array_search('hot dog', $a), array_keys($a));
?>

Output: 1
Hi Andy. Now that achieves exactly what I am looking for without
iterating through the whole array as such.

Many thanks.

Daz

Oct 22 '06 #7

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

Similar topics

1
by: JimmyT | last post by:
I just configured and installed 2.3.4 and noticed there is no datetime module. I noticed there is a datetimemodule.c file that did not get built (ie no object file). Is there something I need to...
1
by: mfjacobs | last post by:
Hello, I was hoping someone here can help me with a problem I am having with the PyXML extension ( ver 0.8.4) and Python 2.4 Thsi is a brand new build of Python. Build with default .configre...
1
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
2
by: Ken Overton | last post by:
I've built a service that can be easily installed by simply copying it into place and running InstallUtil.exe -install <service.exe>. But it'd be great if a Setup project could do that because...
10
by: Rada Chirkova | last post by:
Hi, at NC State University, my students and I are working on a project called "self-organizing databases," please see description below. I would like to use an open-source database system for...
3
by: ng | last post by:
I have the strangest behavior with this form. I guess the form has had one of it's events deleted, but damned if I can find which one! The specific message is just that: "Microsoft Access...
7
by: Matthew Louden | last post by:
I created an ASP.NET web application. I added a TextBox and a RequiredFieldValidator control, and set the ControlToValidate property of RequiredFieldValidator control to be TextBox1. After I built...
9
by: yevvi | last post by:
Hi, We have a product with bunch of dlls which are now built with Visual Studio 2003. We want to switch the build to use VS2005. I have read that in VS2005 runtime libraries come as...
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
3
by: drewj840 | last post by:
I built a Windows service that sweeps a set of folders every 60 seconds and puts the files into a SQL Server database. I am creating a second service that will delete this set of folders and recreate...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.