473,406 Members | 2,336 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,406 software developers and data experts.

which one is faster isset(), array_search(), in_array()

guillermobytes
Hi,

i was wondering what was the best option to store a string in an array.
let me explain: I only need one part of the elements in the array. i.e. i can use the key to store my string, or i can use the value to store the string.
I'll make it clearer with some code:
Expand|Select|Wrap|Line Numbers
  1. $string = 'someString';
  2. $array = array();
  3. //first way to save
  4. $array[$string] = true;//array('someString' => true)
  5. //second way to save
  6. $array[] = $string; //array(0 => 'someString')
so my question is: what is the most efficient option to save a string in an array if I only use one part of the elements (key or value) to store the strings.

maybe how i will use that array helps:
I will need to know afterwards, if the string is stored in the array.
Expand|Select|Wrap|Line Numbers
  1. //i get a string and i want to know if it is already in array
  2. in_array($string, $array);
  3. isset($array[$string]);
  4. ...
wich one is the best?
thanks!
Mar 30 '10 #1
3 7103
Markus
6,050 Expert 4TB
Forget about which is fastest and follow your own logic: you're storing a string in an array, i.e. you're not using the string as an array index. Therefore, store the string in the array and then use in_array(). Makes sense, no?
Mar 30 '10 #2
ok it makes sense.
thank you!
Mar 30 '10 #3
isset is the fastest. But as markus said your not storing the index so it will not work correctly.
Mar 31 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: AJ Z | last post by:
I am using in_array() to search for a value ("other"), in order to validate a form. If I pass $_POST as the array to search PHP says that it is an invalid datatype. It is an array and if I copy...
2
by: Mountain Man | last post by:
Hi, The in_array function sometimes malfunctions on my computer yielding the error message shown below. The code shown below usually works, but sometimes doesn't. This even happens with the same...
3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
3
by: Tom Barnes | last post by:
Check out this code: // Start Code ------------- function test_in_array($val) { $a = array('key' => $val); printf("in_array: %d, value:%s<BR>", in_array('key', $a), $a); } test_in_array(0);...
1
by: Frank [GOD] | last post by:
Let me set this up for y'all... I have 8 mySQL databases with over 100K records, which include a phone number field. I call these the storage tanks. They're labeled db1 - db8. Then I have 1...
6
by: Daz | last post by:
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...
27
by: Cleverbum | last post by:
I currently have a list of md5 strings and need to check if a new string is in that list hundreds of thousands of times. I've found that the fastest way to do this is to have all the md5's stored...
1
by: Johnnyboy01 | last post by:
Hi all... I am having problems with the array_search function. No matter what I do, I can't get it to find a value that I specify. Here is the code I am using to call it: $value = 'month'; $key=...
5
by: chaos | last post by:
Hi , I stuck in my project as i face a problem How to show the available time of the venue when i select the day?? Need to connect to the database to find out the available time of the venue on...
6
by: Vernon Wenberg III | last post by:
I am trying to search within an array to see if there is a match, but I can't get my head around these functions which are supposed to return TRUE or FALSE. First I get a list of values from a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.