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-24-2009, 03:15 AM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default Array merge on similar key => value

I have three arrays I'm working with based on queries pulled from a database. All of them have one key => value pair in common, and I'm looking for the most elegant way to merge these arrays.

array_merge() of course won't work, and I'm curious if there's a better solution than iterating through and merging it 'by hand' so to speak within a loop.

For example, the arrays all follow this paradigm;

Code:
Array
(
    [0] => Array
        (
            [timestamp] => 2009-10-23
            [key_one] => value_one
        )

....

Array
(
    [0] => Array
        (
            [timestamp] => 2009-10-23
            [another_key] => another_value

        )
The desired result, is of course;

Code:
Array
(
    [0] => Array
        (
            [timestamp] => 2009-10-23
            [key_one] => value_one
            [another_key] => another_value
        )
delayedinsanity is offline  
Reply With Quote
Old 10-24-2009, 03:23 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

you could use a combination of intersect, diff, and merge like this:

PHP Code:
<?php
$arr1 
= array(array("timestamp" => "2009-10-23""key_one" => 45));
$arr2 = array(array("timestamp" => "2009-10-23""another_key" => 1234));
print_r($arr1);
echo 
"<br /><br />";
print_r($arr2);
echo 
"<br /><br />";


$identical array_intersect($arr1[0], $arr2[0]);
$diff array_merge(array_diff($arr1[0], $arr2[0]), array_diff($arr2[0], $arr1[0]));
$merge[0] = array_merge($identical$diff);
print_r($merge);
echo 
"<br /><br />";
?>
this solution just takes into account only 2 arrays, but it can be modified to do for multiple.
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
delayedinsanity (10-24-2009)
Old 10-24-2009, 05:46 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Thanks Tony, I wound up just looping through it directly; The above method won't work on the array for me as a whole due to the fact that I'd still have to loop through and merge each key seperately, and trying to run the intersect and diff on the top level of the array doesn't work.

Were I to loop through I'm sure something along the lines of,

PHP Code:
$count count$arr );
for ( 
$i 0$i <= $count$i++ ) {
    
$arr[$i] = array_mergearray_diff$arr[$i], $arr_two[$i] ), array_diff$arr_two[$i..... 
Would do the trick, but it becomes redundant by the time I wind up using a loop. Array-fu!

I'm a huge fan of array_s(p)?lice, array_search, et al, but there's some I've never had the chance to use yet and it was nice to play with them a little. :)
delayedinsanity 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
feedback on my class please frostyboy33 Advanced PHP Programming 7 10-22-2012 09:12 AM
PHP Group Array "teams" and add up points godsdead General 3 07-15-2009 08:38 PM
How to search array benton General 3 03-29-2009 12:31 AM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


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