473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Resorting an array?

LRW
I'm not even sure this is possible, and if it is, I'm having a hard
time focusing on a function that would make it work.

What I have is a dynamically generated form with dozens of rows from a
mySQL query.
Each row in this form has a checkbox to select what rows the user
wants sent to the processing page.
Each row also has a slew of various text fields and dropdown menus
with data from the query and editable by the user.
The user checks the boxes to send on.

Then, on the process page, an array is created based on which
checkboxes are selected.

Now at the moment the process page processes each selected row on a
for-each in the order they were listed on the previous page.

Keeping the mySQL sort order the same as it is on the 1st page, is
there a way to resort the array on the process page so that the rows
are processed in a different order, based on one of the fields in the
array?

For example, each row has the following data:

checkbox value(which is also the row
id),firstname,lastname,address,size,color

And the array could have dozens of rows. Right now it's processed in
order of checkbox value because that's how it's sorted on page one.
What if I want to change the sort in the array by lastname before
processing?

Did that make any sense?

I looked up asort() and usort() and they LOOK like something that
might work...at least I understand what they're doing, but I'm not
sure if something like that would work in this complicated (maybe?) of
an array I have going.
And to be honest, I have no idea in my array what is the key (the
value of the checkbox selected?) and what happens when you have more
than 2 items in each row of the array?

If one of the ?sort() types IS what I need to look into, someone let
me know? If there's another concept that would work better, someone
clue me in?
I'd appreciate it!
Thanks,
Liam
Jul 17 '05 #1
4 1752
LRW wrote:
I'm not even sure this is possible, and if it is, I'm having a hard
time focusing on a function that would make it work.

What I have is a dynamically generated form with dozens of rows from a
mySQL query.
Each row in this form has a checkbox to select what rows the user
wants sent to the processing page.
Each row also has a slew of various text fields and dropdown menus
with data from the query and editable by the user.
The user checks the boxes to send on.

Then, on the process page, an array is created based on which
checkboxes are selected.

Now at the moment the process page processes each selected row on a
for-each in the order they were listed on the previous page.

Keeping the mySQL sort order the same as it is on the 1st page, is
there a way to resort the array on the process page so that the rows
are processed in a different order, based on one of the fields in the
array?

For example, each row has the following data:

checkbox value(which is also the row
id),firstname,lastname,address,size,color

And the array could have dozens of rows. Right now it's processed in
order of checkbox value because that's how it's sorted on page one.
What if I want to change the sort in the array by lastname before
processing?

Did that make any sense?

I looked up asort() and usort() and they LOOK like something that
might work...at least I understand what they're doing, but I'm not
sure if something like that would work in this complicated (maybe?) of
an array I have going.
And to be honest, I have no idea in my array what is the key (the
value of the checkbox selected?) and what happens when you have more
than 2 items in each row of the array?

If one of the ?sort() types IS what I need to look into, someone let
me know? If there's another concept that would work better, someone
clue me in?
I'd appreciate it!
Thanks,
Liam


Hi Liam,

I think I understand what you want.
Only because i stubled on a similar problem recently. :-)

Your problem is that you want a bunch of associated data sorted on one of
the values of one item in those bunches of data, right?
This is how you can manage that: associative arrays (you guessed) but just a
little different than you tried. :-)

in your receiving script try this:
1) store all your rows in arrays.
eg:
loop over your Postdata{
$myRow[1]["var1"] = $_POST["var1"];
$myRow[1]["var2"] = $_POST["var2"];
$myRow[1]["var3"] = $_POST["var3"];
}

ok?
the [1] respresents a row with data. (Use 2 for second row, etc.)
the ["varX"] respresents the value for a certain variable for that row.

2) Now create a new assoc array and use for the key the value where you want
to sort on.
So suppose you need var2 for sorting:
for all your $myRow{
$allData[$var3-from-RowX] = $myRow[X]
}

3) Now you have a fresh datastructure that has keys based on var3.
You can sort it now with asort.

Hope this helps you.

Maybe there is a smarter/faster way, but I like this way because I
understand how it is working. :-)

Good luck,
Erwin Moller

Jul 17 '05 #2
Erwin Moller wrote:

topy correction:
So suppose you need var2 for sorting:
should of course be:
So suppose you need var3 for sorting:

Jul 17 '05 #3
.oO(Erwin Moller)
Your problem is that you want a bunch of associated data sorted on one of
the values of one item in those bunches of data, right?


You can use array_multisort() to create a kind of column sort function:

function csort($array, $column) {
$s = array();
foreach($array as $row) {
$s[] = $row[$column];
}
array_multisort($s, SORT_ASC, $array);
return $array;
}
Example:

$foo = array(
array('firstname' => 'foo', 'lastname' => 'bar'),
array('firstname' => 'bar', 'lastname' => 'foo')
);

print_r(csort($foo, 'firstname'));
Output:

Array
(
[0] => Array
(
[firstname] => bar
[lastname] => foo
)

[1] => Array
(
[firstname] => foo
[lastname] => bar
)

)

HTH
Micha
Jul 17 '05 #4
Michael Fesser wrote:
.oO(Erwin Moller)
Your problem is that you want a bunch of associated data sorted on one of
the values of one item in those bunches of data, right?


You can use array_multisort() to create a kind of column sort function:


<snip>

Hee Micha,

That is cool. :-)
Never ever saw that function.
I'll check it.

Thanks,

Regards,
Erwin Moller

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.