On Sat, 2008-03-15 at 15:02 -0700, Jim Lucas wrote:
> This question should probably be directed at the PHP Internals list, but
> I thought I would start by asking here first if anybody would even use
> this feature.
>
> It has been brought to my attention that with Perl and Ruby, you can use
> Objects as the value of the key within an array. The examples that were
> shown to me make me think that this would be an awesome ability to have
> within PHP.
>
> Here is an example of how I would use such a feature.
>
> I have a table of "customers".
> I have a table customers "contact_locations".
> This table has a reference ID back to the "customers" table.
>
> Now, in my array of information that I build I have this.
>
> $res = query('Select c_id, c_first_name, c_last_name FROM customers');
>
> $customers = array();
> while ( $row = fetch_assoc($res) ) {
> $customers[$row]['contact_locations'] = array();
> $SQL = "SELECT *
> FROM contact_locations
> WHERE c_id={$row['c_id']}";
> $loc_res = query($SQL);
> while ( $loc_row = fetch_assoc($loc_res) ) {
> $customers[$row]['contact_locations'][] = $loc_row;
> }
> }
>
>
> Now, contained within one array "$customers" I have all the information
> that would be needed for displaying any type of information related to a
> customer or a customers location.
Specifically for the example above, I'd only use 2 queries to link up
the contact locations to the customer:
<?php
$sql =
'SELECT '
.' c_id, '
.' c_first_name, '
.' c_last_name '
.'FROM '
.' customers ';
$customers = array();
if( ($res = some_db_query( $sql )) )
{
while( ($row = some_db_fetch_assoc( $res )) )
{
$row['contact_locations'] = array();
$customers[$row['c_id']] = $row;
}
}
$customer_ids = implode( ',', array_keys( $customers ) );
$sql =
'SELECT '
.' * '
.'FROM '
.' contact_locations '
.'WHERE '
.' c_id IN ( '.$customer_ids.' ) ';
if( ($res = some_db_query( $sql )) )
{
while( ($row = some_db_fetch_assoc( $res )) )
{
$customers[$row['c_id']]['contact_locations'][] = $row;
}
}
?>
Imagine 200 customers in your example... your example will hit the DB
201 times. The above hits the DB twice.
> By doing having this feature, I could build the ability to do xPath
> searches within the base array. That would be nice.
>
> Anyways, what do you all think? Worth it or not?
I don't see what added advantage you get by having an object or array()
as a key. Why can't you have the contact_locations as a key in the $row?
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php