Jump to content

[SOLVED] Create and sort array


frost

Recommended Posts

Hi,

 

I am rubbish at arrays so please bear with me.

 

I have a set of selections that can be assigned numbers for showing:

 

<input type=text name=item1> Item 1

<input type=text name=item1> Item 2

 

The user can assign a sort order to the items, upon submitting the form I would like to pop these into a database as a sorted array.

 

So if they assigned item 1 a value of 5 and item 2 a value of 4 the array would go item2, item1

 

Any help would be really appreciated.

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/143062-solved-create-and-sort-array/
Share on other sites

Give this a shot

 

 

<input type="text" name="item[1]"> Item 1
<input type="text" name="item[2]"> Item 2
<input type="text" name="item[3]"> Item 3
<input type="text" name="item[4]"> Item 4

 

 

<?php
if(isset($_POST['item'])) {
	arsort($_POST['item'],SORT_NUMERIC);
	$sortedArray = array_reverse($_POST['item'],true);
	foreach($sortedArray as $item => $order)
		{
			echo 'Item number: '.$item.' Ordered value: '.$order.'<br />';
		}
}
?>

 

This stores the item number in a var called $item and the order by value in the var called $order.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.