Jump to content

Archived

This topic is now archived and is closed to further replies.

hvle

Array problem

Recommended Posts

hi guys,

I have 2 ways of storing record in an array, and I wanna know which one is better (efficiency).

method 1:
$book[] = array('name' => 'name1', 'email' => 'email1');
$book[] = array('name' => 'name2', 'email' => 'email2');
$book[] = array('name' => 'name3', 'email' => 'email3');

method 2:
$book['name'] = array('name1', 'name2', 'name3');
$book['email'] = array('email1', 'email2', 'email3');


thank you very much for any suggestion

Share this post


Link to post
Share on other sites
I prefer a hash (associative array). I'm not sure if there are any efficiency differences; if there are, it would depend on how many records you are storing and what you are doing with them.

Share this post


Link to post
Share on other sites
I'd go one step further, if you can, and make it:
[code]
books = array(book1 => array(name => Catch22, email =>someemail)...))
[/code]

Share this post


Link to post
Share on other sites
method 1 may seem very easy to manipulate, but I got a lot of trouble w/ sorting, and finding out duplication.

A hash method, can you explain what it is?

The # of records are reasonably small considering # of addresses in your address book, so the number is around 5 to 200.  I need to do some database operation like sorting, filtering duplication, insertion.  These operations done inside script, so no database involved.

thanks

Share this post


Link to post
Share on other sites
[quote author=Buyocat link=topic=99667.msg392609#msg392609 date=1152201958]
I'd go one step further, if you can, and make it:
[code]
array(book => array(book1 => array(name => Catch22, email =>someemail)...)))
[/code]
[/quote]
can you point out the advantage with this way cuz I can not see it.

Share this post


Link to post
Share on other sites
There are all sorts of functions of sorting/sifting through arrays.  I'll let you choose which ones suit your needs...
http://us3.php.net/array

Share this post


Link to post
Share on other sites
[url=http://www.samspublishing.com/articles/article.asp?p=31840&seqNum=6&rl=1]Sorting Multidimensional Arrays[/url]

Share this post


Link to post
Share on other sites
that's a good link http://us3.php.net/array
Thank you very much.

array_multisort, it doesn't explain much in the manual.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.