SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with Array Sorting

    Greetings all, wonderful forum you have here!

    I searched far and wide on this and other forums, but could not find a tutorial or example of what I am trying to do.

    I have the following routine that came with a PHP script I purchased to pull XML feeds from PPC search engines and sort the results based on effective bid price (bid price * my commission rate).

    The script only includes the following simple routine, which compares 2 feeds. I would like to query results from 4 or 5 feeds, and sort all the results based on effective bid price. Is there an easy way to do this?

    Code:
    function simpleFeedSort($ary1, $ary2) {
    	
    	$x = count($ary1) + count($ary2);
    	$aryNew = array();
    	$intAry1 = 0;
    	$intAry2 = 0;
    	
        // create an 'Item' object
        $Item['Title'] = '';
        $Item['Url'] = '';
        $Item['Uri'] = '';
        $Item['Description'] = '';
        $Item['Bid'] = (float)'0.0';
        $Item['Impression'] = '';
    
    	//PAD THE ARRAY TO AVOID ERRORS
        //add it to the array of items
    	for ($i = 0; $i <= ($x-1); $i++) {
    		$ary1[] = $Item;
    		$ary2[] = $Item;
    		}
    	
    	for ($i = 0; $i <= ($x-1); $i++) {
    		if ($ary1[$intAry1]['Bid'] > $ary2[$intAry2]['Bid']) {
    			$aryNew[$i]['Title'] = $ary1[$intAry1]['Title'];
    			$aryNew[$i]['Url'] = $ary1[$intAry1]['Url'];
    			$aryNew[$i]['Uri'] = $ary1[$intAry1]['Uri'];
    			$aryNew[$i]['Description'] = $ary1[$intAry1]['Description'];
    			$aryNew[$i]['Bid'] = $ary1[$intAry1]['Bid'];
    			$aryNew[$i]['Impression'] = $ary1[$intAry1]['Impression'];
    			$intAry1++;
    		} else {
    			$aryNew[$i]['Title'] = $ary2[$intAry2]['Title'];
    			$aryNew[$i]['Url'] = $ary2[$intAry2]['Url'];
    			$aryNew[$i]['Uri'] = $ary2[$intAry2]['Uri'];
    			$aryNew[$i]['Description'] = $ary2[$intAry2]['Description'];
    			$aryNew[$i]['Bid'] = $ary2[$intAry2]['Bid'];
    			$aryNew[$i]['Impression'] = $ary2[$intAry2]['Impression'];
    			$intAry2++;
    		}
    	}
    	$ary1 = array();
    	$ary2 = array();
    	return $aryNew;
    }
    I would appreciate any feedback, or pointers to where i can find something to suit my needs. Thanks in advance!

    neomaven

  2. #2
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use array_merge() to append multiple arrays together, then use usort() to perform the sort on the whole array.

    Something like..

    Code:
    /* append multiple arrays together... */
    $aryNew = array_merge($ary1, $ary2);
    /* Comparision function for usort, comparing to items */
    function compareBids($item1, $item2) { return $item1['Bid'] - $item2['Bid']; }
    /* Sort */
    usort($aryNew, 'compareBids');
    Havent tested the code, and not sure on the ascending/descending sort order but thats changed by reversing subtraction, but should be roughly on the right track.

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried experimenting with the examply you gave Ren, but was unable to get it to work properly. I appreciate your input nevertheless, I feel like I am getting closer.


    Any other suggestions from others perhaps?

    Thx

  4. #4
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by neomaven
    I tried experimenting with the examply you gave Ren, but was unable to get it to work properly. I appreciate your input nevertheless, I feel like I am getting closer.
    Hmm, did some testing with code below, seems fine here sorts in ascending order by bid. (Using PHP5.0.4 atleast)

    Code:
    function randItem()
    {
    	$Item = array();
        $Item['Title'] = '';
        $Item['Url'] = '';
        $Item['Uri'] = '';
        $Item['Description'] = '';
        $Item['Bid'] = (float)mt_rand(0, 100000);
        $Item['Impression'] = '';
    
        return $Item;
    }
    
    	$ary1 = array();
    	$ary2 = array();
    	$ary3 = array();
    	$ary4 = array();
    
    	for($i = 0; $i < 10; ++$i)
    	{
    		$ary1[] = randItem();
    		$ary2[] = randItem();
    		$ary3[] = randItem();
    		$ary4[] = randItem();
    	}
    
    	$aryNew = array_merge($ary1, $ary2, $ary3, $ary4);
    
    	/* Comparision function for usort, comparing to items */
    	function compareBids($item1, $item2) { return $item1['Bid'] - $item2['Bid']; }
    	/* Sort */
    	usort($aryNew, 'compareBids');
    
    	var_dump($aryNew);


Bookmarks

Posting Permissions

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