0

HI,

I have a script which reads an xml feed then uses a loop to insert information from that feed into a multi-dimensional array. I need to sort the array by 'day' before outputting, day is a dimension of the array.

Below is a print_r() of the array after the xml feed has been parsed.

Array 
( 
	[0] => Array 
		( 
		        	[day] => Tuesday 
			[type] => sunny intervals 
			[max] => 18°C 
			[min] => 7°C 
		) 
) 
Array 
( 
	[1] => Array 
		( 
			[day] => Wednesday 
			[type] => sunny intervals 
			[max] => 19°C 
			[min] => 10°C 
		) 
) 
Array 
( 
	[2] => Array 
		( 
			[day] => Thursday 
			[type] => sunny intervals 
			[max] => 22°C 
			[min] => 13°C 
		)
)
3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by nileshgr
0

Seems to be in an appropriate order now. How did you want them sorted if not by the order the days come in?

0

Its in order now, but often the feed produces them in say for example Thursday Friday Wednesday, I just want to be able to check for that and then sort them properly.

0

How did you get that array structure ? I don't get it in any way. Can give a sample code to generate that array so I can think over some sorting solution ?

0

Something like this could work for you if the forecast xml has 7 days or less, which I'm assuming or how would you know what days are from what week.

$sortedarray = array();

foreach($xmlarray as $forecast)
{
	switch ($forecast['day'])
	{
		case 'Sunday':
			$sortedarray[0] = $forecast;
			break;
		case 'Monday':
			$sortedarray[1] = $forecast;
			break;
		case 'Tuesday':
			$sortedarray[2] = $forecast;
			break;
		case 'Wednesday':
			$sortedarray[3] = $forecast;
			break;
		case 'Thursday':
			$sortedarray[4] = $forecast;
			break;
		case 'Friday':
			$sortedarray[5] = $forecast;
			break;
		case 'Saturday':
			$sortedarray[6] = $forecast;
			break;
	}
}

// If first forecast is for today
$daynum = intval(date('w',now()));  // find numerical day of week

// if first forecast is for tomorrow
// $daynum = ($daynum+1)%7;

while(isset($sortedarray[$daynum])) // loop over forecasts
{
	// process forecast
	print_r($sortedarray[$daynum]); // example process
	$daynum = ($daynum+1)%7;        // move to next day index
	unset($sortedarray[$daynum]);   // added in case of 7 day forecast
}
0

I found a possible solution for this. The code is below, you can try to implement this:

<?php

$x = array(
array( 'day' => 'Friday' ),
array( 'day' => 'Monday' ),
array( 'day' => 'Sunday' ),
array( 'day' => 'Wednesday'),
array( 'day' => 'Thursday'),
array( 'day' => 'Saturday'),
array( 'day' => 'Tuesday'),
array( 'day' => 'Thursday'));

usort($x, 'cmp');

print_r($x);

function cmp($a, $b) {
  
  $d1 = date('w', strtotime($a['day']));
  $d2 = date('w', strtotime($b['day']));

  if($d1 < $d2) {
    return -1;
  } elseif($d1 == $d2) {
    return 0;
  } elseif($d1 > $d2) {
    return 1;
  }

}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.