简体   繁体   中英

using array_search VS storing data in array as both index

What is better and fast:

$objectTypes = array(
        1=>'Content',2=>'Taxonomy',3=>'Ad',4=>'Issue'       
        );

ID: $objectTypes[1];

Title: array_search('Content',$objectTypes);

VS

$objectTypes = array(
        1=>'Content',2=>'Taxonomy',3=>'Ad',4=>'Issue',
        'Content'=>1,'Taxonomy'=>2,'Ad'=>3,'Issue'=>4);

ID: $objectTypes[1];

Title: $objectTypes['Content'];

IN first I use array_search to get the id of object.

In second I just store it other way round.

Mostly the object is not going to change and it won't have more than 5-10 types I need to store the object id and title

I am just curious for use of the method vs large array

ofcourse when i use it in class, i'll use proper function to get/set

Performance is first preference, but a good coding style and optimum use of class objects/files is also important.

The second one is not recommended. There is no point in saving the same data twice and think about all the wasted time populating the unnecessary data. So to answer your question array_search will do you better

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM