How to sort this array on specific key?

Hi,

Got some code that loops through an existing array, builds a new array as it goes and strips out some sort-related data. when the loop is finished, I want to sort the resultant array on the sort-related data that resides in $dibble[‘sort’] … I’m struggling to understand how to do it. Here’s the loop that build and strips:

	
foreach($initial as $item) 
	{
		$dibble['alink'][]=$item->link;
		$dibble['aname'][]=$item->name;
		$dibble['aid'][]=$item->ID;
		# strip out block text ... if there is any
		$c_start=strpos($item->text,"[menusort]")+10;
		if ($c_start > 10)
			{
				$c_end = strpos($item->text,"[/menusort]");
				$c_len=$c_end - $c_start;
				$dibble['sort'][]=substr($item->text, $c_start,$c_len);		
			}
		else
			{
				$dibble['sort'][]='99999';		
			}								
	}

I think USort() is the function you’re after.

But the way you’ve organised that array, it’s going to be a real pain to sort it.

Thanks Jake,

that was quick!!

How would you suggest I arrange the array to make sorting easier?

Can anyone tell me how to build the above array so it’s easier to sort?

Thanks.

You’re going to need to post some before and (ideal) after representations of your data structures.

You can create a new array item with associative keys on that array item storing your information. To do that, you could move the assignments to the end of the loop, so that you can place the sort key in another variable, and then add the array item at the end of things.


foreach($initial as $item) {
    # strip out block text ... if there is any
    $sort = '99999';
    $c_start=strpos($item->text, "[menusort]") + 10;
    if ($c_start > 10) {
        $c_end = strpos($item->text, "[/menusort]");
        $c_len = $c_end - $c_start;
        $sort = substr($item->text, $c_start, $c_len);		
    }								
    $dibble[] = array(
        'alink' => $item->link,
        'aname' => $item->name,
        'aid' => $item->ID,
        'sort' => $sort
    );
}

Now you’re ready to sort the $dibble array.

Been away from this stuff for a while and I’m having a real, proper, brainfart meltdown on this array !!!

anyone take pity on me and tell me how to write the foreach loop to run through the $dibble array?

Thanks in advance!

:wink:

As a normal array loop?


foreach ($dibble as $item) {
    ...
}

I’m not sure I can say it any differently than paul wilkins code above … each occurrence of $dibble holds an array containing four data items … I simply want to get a ‘foreach’ to loop over each item and get access to any/all of the data items.

You could access each data item as $item[‘alink’] or $item[‘aname’]

I’ve tried that but get nothing …

I’ve done a print_r on the $dibble array and here’s what I get:

Array ( [6] => Array ( [alink] => 7 [aname] => Training [aid] => corporate [asort] => 99999 ) [7] => Array ( [alink] => 24 [aname] => Work For Us [aid] => corporate [asort] => 99999 ) [8] => Array ( [alink] => 5 [aname] => Young People [aid] => yp [asort] => 300 ) [5] => Array ( [alink] => 4 [aname] => Housing [aid] => housing [asort] => 200 ) [4] => Array ( [alink] => 17 [aname] => Fundraising [aid] => corporate [asort] => 99999 ) [1] => Array ( [alink] => 3 [aname] => Adult [aid] => adult [asort] => 100 ) [2] => Array ( [alink] => 31 [aname] => Blogs [aid] => [asort] => 99999 ) [3] => Array ( [alink] => 18 [aname] => Events [aid] => corporate [asort] => 99999 ) [0] => Array ( [alink] => 6 [aname] => About Us [aid] => corporate [asort] => 99999 ) )

Here’s the code I used to try to loop:

foreach($dibble as $item){

			echo "Name = $item->aname";
}

Here’s the output I got:

Name = Name = Name = Name = Name = Name = Name = Name = Name =

You should see before your last post that I deleted that bad advice and replaced it with better advice.

The $item->aname syntax is trying to access the aname property of an object in $item. However, $item isn’t an object and aname isn’t a property.

The array syntax would be $item["aname"] (or within your double-quoted string "{$item["aname"]}", or "$item[aname]").

Thanks guys, that’s fixed it!