简体   繁体   English

使用array_search VS将数据存储在数组中作为两个索引

[英]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. 首先,我使用array_search获取对象的ID。

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 通常,对象不会改变,并且不会有超过5-10种类型, 我需要存储对象ID和标题

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 因此,回答您的问题array_search会更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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