TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-02-2009, 01:06 PM   #1 (permalink)
The Visitor
 
Swiftaxe's Avatar
 
Join Date: Oct 2009
Location: Sweden
Posts: 3
Thanks: 1
Swiftaxe is on a distinguished road
Help Searching through multidimensional arrays

Hi,

I need advice about searching through multidimensional arrays.

My array contains product data for a webshop. This is the structure:

PHP Code:
$array = array( $category =>
    array( 
$product =>
       
$id,
       
$title,
       
$price,
       
etc... )); 
There is several categories and in each category many products with it's data. What is a good way to search such an array for a product with a certain ID? I need the keys for each dimension returned so I can extract data from that product.

Is there a way to do this without having nested foreach loops?
Tell me If I am not clear enough.

Any help is greatly appreciated!

/Swiftaxe
Swiftaxe is offline  
Reply With Quote
Old 10-02-2009, 10:59 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Swiftaxe View Post
Hi,

I need advice about searching through multidimensional arrays.

My array contains product data for a webshop. This is the structure:

PHP Code:
$array = array( $category =>
    array( 
$product =>
       
$id,
       
$title,
       
$price,
       
etc... )); 
There is several categories and in each category many products with it's data. What is a good way to search such an array for a product with a certain ID? I need the keys for each dimension returned so I can extract data from that product.

Is there a way to do this without having nested foreach loops?
Tell me If I am not clear enough.

Any help is greatly appreciated!

/Swiftaxe
Nested foreaches would be the best way, but doing this outside of a database to start with is bad. Why aren't you using a database? SQL queries could search though results literally thousands of time faster.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Swiftaxe (10-04-2009)
Old 10-04-2009, 12:03 PM   #3 (permalink)
The Visitor
 
Swiftaxe's Avatar
 
Join Date: Oct 2009
Location: Sweden
Posts: 3
Thanks: 1
Swiftaxe is on a distinguished road
Default

Thanks! I thought doing several sql-queries would be more time-consuming.

So making a dozen of these queries would be faster?: "SELECT title FROM products WHERE id=$id LIMIT 1"
Swiftaxe is offline  
Reply With Quote
Old 10-04-2009, 03:21 PM   #4 (permalink)
The Visitor
 
Swiftaxe's Avatar
 
Join Date: Oct 2009
Location: Sweden
Posts: 3
Thanks: 1
Swiftaxe is on a distinguished road
Default

Village Idiot,

I timed the two different approaches. Doing 16 sql queries was way slower than looping the arrays.
Sql query time: 0.002832 msec
Array looping time: 0.000097 msec

But I guess that if there were thousands of products the sql would be faster. I only have about 30 in my test.
Swiftaxe is offline  
Reply With Quote
Old 10-04-2009, 05:35 PM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Swiftaxe View Post
Village Idiot,

I timed the two different approaches. Doing 16 sql queries was way slower than looping the arrays.
Sql query time: 0.002832 msec
Array looping time: 0.000097 msec

But I guess that if there were thousands of products the sql would be faster. I only have about 30 in my test.
That is because of connection overheads associated with each individual query. Can you show us the query? I've never seen a need to use that many queries for one task before.
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
lol, i made a never ending loop with arrays sarmenhb Tips & Tricks 28 10-22-2012 09:36 AM
config file arrays oop memcache MJ_ General 2 01-24-2009 06:53 PM
2-d arrays -- issues of printing and deleting Dave Absolute Beginners 3 06-05-2008 11:45 AM
Arrays + Pages blayne4k Script Giveaway 0 04-20-2008 10:35 PM


All times are GMT. The time now is 12:11 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design