Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help matching array elements

    howdy,
    I have two arrays $a & $b. they both started out with the same number of elements in each, $a consists of many duplicates example below.
    [example]
    $a =
    [0]=> string(12) "BentGear.com"
    [1]=> string(12) "BentGear.com"
    [2]=> string(12) "BentGear.com"
    [3]=> string(37) "BillBam.com - Bill Bam's Collectibles"
    [4]=> string(37) "BillBam.com - Bill Bam's Collectibles"
    [5]=> string(37) "BillBam.com - Bill Bam's Collectibles"
    [6]=> string(37) "BillBam.com - Bill Bam's Collectibles"
    [7]=> string(9) "Blair.com"
    [8]=> string(7) "Buy.com"
    [/example]
    I used array_unique($a) to get it reduced to this.
    [example]
    [0]=> string(12) "BentGear.com"
    [3]=> string(37) "BillBam.com - Bill Bam's Collectibles"
    [7]=> string(9) "Blair.com"
    [8]=> string(7) "Buy.com"
    [/example]


    array $b contains corresponding urls for $a.
    [example]
    [0]=> string(54) "http://www.shareasale.com/u.cfm?d=1719&m=7371&u=106368"
    [1]=> string(54) "http://www.shareasale.com/u.cfm?d=1718&m=7371&u=106368"
    [2]=> string(54) "http://www.shareasale.com/u.cfm?d=1717&m=7371&u=106368"
    [3]=> string(54) "http://www.shareasale.com/u.cfm?d=1716&m=7371&u=106368"
    [4]=> string(54) "http://www.shareasale.com/u.cfm?d=1671&m=7371&u=106368"
    [5]=> string(54) "http://www.shareasale.com/u.cfm?d=1057&m=7371&u=106368"
    [6]=> string(54) "http://www.shareasale.com/u.cfm?d=1283&m=5151&u=106368"
    [7]=> string(54) "http://www.shareasale.com/u.cfm?d=1282&m=5151&u=106368"
    [8]=> string(54) "http://www.shareasale.com/u.cfm?d=1281&m=5151&u=106368"
    [9]=> string(53) "http://www.shareasale.com/u.cfm?d=889&m=5151&u=106368"
    [10]=> string(53) "http://www.shareasale.com/u.cfm?d=448&m=5151&u=106368"
    [11]=> string(53) "http://www.shareasale.com/u.cfm?d=301&m=5151&u=106368"
    [12]=> string(53) "http://www.shareasale.com/u.cfm?d=294&m=5151&u=106368"
    [13]=> string(53) "http://www.shareasale.com/u.cfm?d=293&m=5151&u=106368"
    [14]=> string(53) "http://www.shareasale.com/u.cfm?d=197&m=5151&u=106368"
    [15]=> string(53) "http://www.shareasale.com/u.cfm?d=196&m=5151&u=106368"
    [16]=> string(53) "http://www.shareasale.com/u.cfm?d=113&m=5151&u=106368"
    [17]=> string(53) "http://www.shareasale.com/u.cfm?d=112&m=5151&u=106368"
    [18]=> string(52) "http://www.shareasale.com/u.cfm?d=13&m=5151&u=106368"
    [19]=> string(52) "http://www.shareasale.com/u.cfm?d=12&m=5151&u=106368"
    [20]=> string(44) "http://www.dpbolvw.net/click-1999005-4992712"
    [21]=> string(47) "http://www.anrdoezrs.net/click-1999005-10389812"
    [/example]

    what I need to know is how can I match up the elements in $a with the corresponding url in $b?

    any ideas?
    tia.

  • #2
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why are $a and $b different sizes in the first place (9/22 elements)?

  • #3
    New Coder
    Join Date
    Apr 2006
    Location
    UK
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could put it all in one array, with the strings as the keys to the array?

    PHP manual - Arrays

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts
    they both start out with 22, i just abbreviated $a for simplicity.

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I cant use one array as I need to get rid of the duplicates in $a, and the the values of $b are unique for each, so running array_unique does nothing for me when I have both values in one array.

  • #6
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $c array_unique(array_combine($b$a)); 
    Not sure if that's exactly what you want though.

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, I thought of that array_combine is only available in php 5, I am still using 4.

  • #8
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    From the php.net user comments:
    PHP Code:
    if (!function_exists('array_combine')) {
       function 
    array_combine($a$b) {
           
    $c = array();
           if (
    is_array($a) && is_array($b))
               while (list(, 
    $va) = each($a))
                   if (list(, 
    $vb) = each($b))
                       
    $c[$va] = $vb;
                   else
                       break 
    1;
           return 
    $c;
       }



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •