array sorting
Moderator: General Moderators
array sorting
i list of a column's values by while loop, $row[column],
i wanna to list from $n, for example, in normal i get val1, val2, val3, val4.... but what should i do if i want in the way, val3, val4,val1,val2 or val2, val3, val4, val1...
thanks in advance.
i wanna to list from $n, for example, in normal i get val1, val2, val3, val4.... but what should i do if i want in the way, val3, val4,val1,val2 or val2, val3, val4, val1...
thanks in advance.
Your wording was very hard to understand but if I do understand what you're asking you can do this by creating another temporary array which you will use for re-ordering and then you can move the contents of that array back into the original.
Last edited by nigma on Sat Feb 12, 2005 7:52 pm, edited 1 time in total.
true, it is because of my poor english..
if i edit the question again;
i list of a column's values that i get database table by while loop,
in the way
this work simply/orderly and outputs the values, and now what i wanna do is to line up values in different ways like val3, val4,val1,val2 or val2, val3, val4, val1... instead of val1, val2, val3, val4...
and, nigma, your suggestion makes no sense to me for now, thanks anyway i appreciate your concern as well
if i edit the question again;
i list of a column's values that i get database table by while loop,
in the way
Code: Select all
while ($row=mysql_fetch_array($query)){ print $rowїcolumn_name]; }and, nigma, your suggestion makes no sense to me for now, thanks anyway i appreciate your concern as well
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
no, not random
(if i cant get a solution i will try so)
normal listing in this way; value1, value2, value3, value4
i want to list firstly in the way; value1, value2, value3, value4
and then value2, value3, value4, value1
and then value3, value4, value1, value2
and then value4, value1, value2, value3
... (sowwy)
how, sounds nonsense?

(if i cant get a solution i will try so)
normal listing in this way; value1, value2, value3, value4
i want to list firstly in the way; value1, value2, value3, value4
and then value2, value3, value4, value1
and then value3, value4, value1, value2
and then value4, value1, value2, value3
... (sowwy)
how, sounds nonsense?
Last edited by MrKnight on Tue Feb 01, 2005 12:10 pm, edited 1 time in total.
Code: Select all
while (list($varone, $vartwo, $varthree)=mysql_fetch_array($result)) {
echo ("2- $vartwo 1- $varone 3-$varthree");
// keep putting echo () with the variables in a different order
}sorry guys,
, for making trouble...
at last, at least, i got the event (thanks to feyd for indicating the way)
namely;
but values was in the database table and this piece of code needed some extras, and here it is; how i retrieved the values from database.
works...
meanwhile, there is else way to gather all rows in an array?[/quote]
at last, at least, i got the event (thanks to feyd for indicating the way)
namely;
Code: Select all
$array = array("value 1", "value 2", "value 3", "value 4");
for ($i=1;$i<=count($array);$i++){
foreach ($array as $value) print $value.", ";
$deleted = array_shift($array);
$added = array_push($array,$deleted);
print "<br>";
}
// outputs
value 1, value 2, value 3, value 4,
value 2, value 3, value 4, value 1,
value 3, value 4, value 1, value 2,
value 4, value 1, value 2, value 3,Code: Select all
// connect to the db and send query
$array = array();
while ($row=mysql_fetch_assoc($query)) array_push($array,$rowїcolumn_name]);
for ($i=1;$i<=count($array);$i++){
foreach ($array as $value) print $value.", ";
$deleted = array_shift($array);
$added = array_push($array,$deleted);
print "<br>";
}
// outputs
value 1, value 2, value 3, value 4,
value 2, value 3, value 4, value 1,
value 3, value 4, value 1, value 2,
value 4, value 1, value 2, value 3,meanwhile, there is else way to gather all rows in an array?[/quote]