Jump to content

Array uasort not doing what I want


Ninjakreborn

Recommended Posts

I am probably using it wrong but this isn't working.

 

	function user_sort($a, $b) {
		if (strtotime($a['news_date']) > strtotime($a['news_date'])) {
			return 1;	
		}
	}
	uasort($data, user_sort);

 

The data object looks like this.

Any one can help me figure out why this isn't working?  I basically just want to short the array by the "news_date" value with the newest

values showing up first.

Array

(

    [427] => Array

        (

            [news_id] => 427

            [company_id] => 16

            [news_date] => 05/13/2010

            [content] =>

            [expiration_date] => 06/30/2010

            [is_active] => 1

            [is_featured] => 0

            [is_news] => 1

            [is_portfolio] => 1

            [featured_description] =>

            [created_date] => 05/22/2010

            [short_title] =>

            [sortOrder] => 1

            [hide_news_page] => 0

        )

 

    [505] => Array

        (

            [news_id] => 505

            [company_id] => 72

            [news_date] => 05/15/2010

            [abstract] =>

            [content] =>

            [expiration_date] =>

            [is_active] => 1

            [is_featured] => 0

            [is_news] => 1

            [is_portfolio] => 1

            [featured_description] =>

            [created_date] => 0000-00-00

            [short_title] =>

            [sortOrder] => 2

            [hide_news_page] => 0

        )

 

    [439] => Array

        (

            [news_id] => 439

            [company_id] => 38

            [news_date] => 2009-09-08 00:00:00

            [content] =>

            [expiration_date] =>

            [is_active] => 1

            [is_featured] => 0

            [is_news] => 1

            [is_portfolio] => 1

            [featured_description] =>

            [created_date] => 0000-00-00

            [short_title] => testdata

            [sortOrder] => 3

            [hide_news_page] =>

        )

 

 

Link to comment
Share on other sites

I also just tried something along the lines of:

<?php
//>> GET DATA AND SET LOCAL DATA OBJECTS
	$objDataCollection	= $objData->get_news();
	function user_sort($a, $b) {
		$date1 = strtotime($a['news_date']);
		$date2 = strtotime($a['news_date']);

		if ($date1 == $date2) return 0;
		return ($date1 > $date2) ? -1 : 1;
	}
	uasort($objDataCollection, user_sort);

?>

<?php
//>> GET DATA AND SET LOCAL DATA OBJECTS
	$objDataCollection	= $objData->get_news();
	function user_sort($a, $b) {
		//$date1 = strtotime($a['news_date']);
		//$date2 = strtotime($a['news_date']);

		if ($a == $b) return 0;
		return ($a > $b) ? -1 : 1;
	}
	uasort($objDataCollection, user_sort);
?>

This almost works but not entirely...for some reason some dates are off.  Any advice?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.