Folks,
I know I could do this with a foreach loop but it looks dirty. I'm
wondering if I can do this via array_walk() or asort() and would appreciate
some help..
I have an array - an example follows
$tmpArray[0]="one";
$tmpArray[1]="two";
$tmpArray[2]="three";
$tmpArray[3]="apples";
$tmpArray[4]="ref=1";
The elements can be written up in any order, thus the last element "ref=1"
could have been anywhere in the array, its just for this example that I have
placed it as the last element.
I need to read these elements, but I need to give precedence to first
reading the elements that contain an equals sign before the others... It
does *not* have to be alphabetically sorted - so the following result would
be acceptable:
$tmpArray[0]="ref=1";
$tmpArray[1]="one";
$tmpArray[2]="two";
$tmpArray[3]="three";
$tmpArray[4]="apples";
Can anyone suggest/tell me a method that might be environmentally friendly
to do this? I think array_walk might help but I've never worked out how to
use it...
Thanks
randelld 5 2391
On Mon, 08 Sep 2003 21:03:41 GMT, "Randell D."
<yo**************************@yahoo.com> wrote: Folks,
I know I could do this with a foreach loop but it looks dirty. I'm wondering if I can do this via array_walk() or asort() and would appreciate some help..
I have an array - an example follows
$tmpArray[0]="one"; $tmpArray[1]="two"; $tmpArray[2]="three"; $tmpArray[3]="apples"; $tmpArray[4]="ref=1";
The elements can be written up in any order, thus the last element "ref=1" could have been anywhere in the array, its just for this example that I have placed it as the last element.
I need to read these elements, but I need to give precedence to first reading the elements that contain an equals sign before the others... It does *not* have to be alphabetically sorted - so the following result would be acceptable:
$tmpArray[0]="ref=1"; $tmpArray[1]="one"; $tmpArray[2]="two"; $tmpArray[3]="three"; $tmpArray[4]="apples";
Can anyone suggest/tell me a method that might be environmentally friendly to do this? I think array_walk might help but I've never worked out how to use it...
<pre>
<?php
$tmpArray[0]="one";
$tmpArray[1]="two";
$tmpArray[2]="three";
$tmpArray[3]="apples";
$tmpArray[4]="ref=1";
function equalsSort($a,$b) {
if (strstr($a,'=')) return -1;
if (strstr($b,'=')) return 1;
return strcmp($a,$b);
}
usort($tmpArray, 'equalsSort');
var_dump($tmpArray);
?>
</pre>
array(5) {
[0]=>
string(5) "ref=1"
[1]=>
string(6) "apples"
[2]=>
string(3) "one"
[3]=>
string(5) "three"
[4]=>
string(3) "two"
}
--
Andy Hassall (an**@andyh.co.uk) icq(5747695) ( http://www.andyh.co.uk)
Space: disk usage analysis tool ( http://www.andyhsoftware.co.uk/space)
Randell D. <yo**************************@yahoo.com> wrote: Folks,
I know I could do this with a foreach loop but it looks dirty. I'm wondering if I can do this via array_walk() or asort() and would appreciate some help..
You'll need usort. http://nl.php.net/manual/en/function.usort.php
<?php
$list = array('one', 'two', '=three', 'four', '=five');
print_array ($list);
usort($list, "is_prec");
print_array ($list);
function is_prec ($a, $b) {
if (substr($a,0,1) == '=' )
{
return -1;
}
elseif (substr($a,0,1) <> '=')
{
return 1;
}
else return 0;
}
function print_array($ar)
{
print "<pre>"; print_r($ar);print"</pre>";
}
?> I have an array - an example follows
$tmpArray[0]="one"; $tmpArray[1]="two"; $tmpArray[2]="three"; $tmpArray[3]="apples"; $tmpArray[4]="ref=1";
The elements can be written up in any order, thus the last element "ref=1" could have been anywhere in the array, its just for this example that I have placed it as the last element.
I need to read these elements, but I need to give precedence to first reading the elements that contain an equals sign before the others... It does *not* have to be alphabetically sorted - so the following result would be acceptable:
$tmpArray[0]="ref=1"; $tmpArray[1]="one"; $tmpArray[2]="two"; $tmpArray[3]="three"; $tmpArray[4]="apples";
Can anyone suggest/tell me a method that might be environmentally friendly to do this? I think array_walk might help but I've never worked out how to use it...
Thanks randelld
Goodluck.
--
Maarten van der Peet
"Maarten van der Peet" <Ma******************@haalditvetweg.xs4all.nl> wrote
in message
news:1g0zrl6.1j3vurcbn178N%Ma******************@ha alditvetweg.xs4all.nl... Randell D. <yo**************************@yahoo.com> wrote:
Folks,
I know I could do this with a foreach loop but it looks dirty. I'm wondering if I can do this via array_walk() or asort() and would
appreciate some help..
You'll need usort.
http://nl.php.net/manual/en/function.usort.php
<?php
$list = array('one', 'two', '=three', 'four', '=five'); print_array ($list); usort($list, "is_prec"); print_array ($list);
function is_prec ($a, $b) { if (substr($a,0,1) == '=' ) { return -1; } elseif (substr($a,0,1) <> '=') { return 1; } else return 0; }
function print_array($ar) { print "<pre>"; print_r($ar);print"</pre>";
} ?> I have an array - an example follows
$tmpArray[0]="one"; $tmpArray[1]="two"; $tmpArray[2]="three"; $tmpArray[3]="apples"; $tmpArray[4]="ref=1";
The elements can be written up in any order, thus the last element
"ref=1" could have been anywhere in the array, its just for this example that I
have placed it as the last element.
I need to read these elements, but I need to give precedence to first reading the elements that contain an equals sign before the others... It does *not* have to be alphabetically sorted - so the following result
would be acceptable:
$tmpArray[0]="ref=1"; $tmpArray[1]="one"; $tmpArray[2]="two"; $tmpArray[3]="three"; $tmpArray[4]="apples";
Can anyone suggest/tell me a method that might be environmentally
friendly to do this? I think array_walk might help but I've never worked out how
to use it...
Thanks randelld
Goodluck.
-- Maarten van der Peet
I don't think you read my entire post properly... I do not have any element
that begins with an = sign, instead, I have *some* elements that *have* an
equals sign somewhere. The array you created in your example is not the
same as the array in my question.
Thanks for trying though.
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:uv********************************@4ax.com... On Mon, 08 Sep 2003 21:03:41 GMT, "Randell D." <yo**************************@yahoo.com> wrote:
Folks,
I know I could do this with a foreach loop but it looks dirty. I'm wondering if I can do this via array_walk() or asort() and would
appreciatesome help..
I have an array - an example follows
$tmpArray[0]="one"; $tmpArray[1]="two"; $tmpArray[2]="three"; $tmpArray[3]="apples"; $tmpArray[4]="ref=1";
The elements can be written up in any order, thus the last element
"ref=1"could have been anywhere in the array, its just for this example that I
haveplaced it as the last element.
I need to read these elements, but I need to give precedence to first reading the elements that contain an equals sign before the others... It does *not* have to be alphabetically sorted - so the following result
wouldbe acceptable:
$tmpArray[0]="ref=1"; $tmpArray[1]="one"; $tmpArray[2]="two"; $tmpArray[3]="three"; $tmpArray[4]="apples";
Can anyone suggest/tell me a method that might be environmentally
friendlyto do this? I think array_walk might help but I've never worked out how
touse it...
<pre> <?php $tmpArray[0]="one"; $tmpArray[1]="two"; $tmpArray[2]="three"; $tmpArray[3]="apples"; $tmpArray[4]="ref=1";
function equalsSort($a,$b) { if (strstr($a,'=')) return -1; if (strstr($b,'=')) return 1; return strcmp($a,$b); }
usort($tmpArray, 'equalsSort');
var_dump($tmpArray); ?> </pre>
array(5) { [0]=> string(5) "ref=1" [1]=> string(6) "apples" [2]=> string(3) "one" [3]=> string(5) "three" [4]=> string(3) "two" }
-- Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk) Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Cheers - that works perfectly... and looks tidy too...
Randell D. <yo**************************@yahoo.com> wrote:
<snip> I don't think you read my entire post properly... I do not have any element that begins with an = sign, instead, I have *some* elements that *have* an equals sign somewhere. The array you created in your example is not the same as the array in my question.
Yes you're right. I must have had a black out. :-)
But the understanding and using of 'usort' is still valid.
Thanks for trying though.
You're welkom.
--
Maarten van der Peet This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: gambler |
last post by:
let's say you have:
var games = new Array();
games = new GAME(gameNum, rotNum1, rotNum2, ... );
(
so a sparsley populate array which enables me to locate a game usin
the game number...
|
by: Xah Lee |
last post by:
Sort a List
Xah Lee, 200510
In this page, we show how to sort a list in Python & Perl and also
discuss some math of sort.
To sort a list in Python, use the “sort” method. For example:
...
|
by: KraftDiner |
last post by:
I have two lists.
I want to sort by a value in the first list and have the second list
sorted as well... Any suggestions on how I should/could do this?
|
by: yeti349 |
last post by:
Hi, I'm using the following code to retrieve data from an xml file and
populate a javascript array. The data is then displayed in html table
form. I would like to then be able to sort by each...
|
by: xahlee |
last post by:
Last year, i've posted a tutorial and commentary about Python and
Perl's sort function. (http://xahlee.org/perl-python/sort_list.html)
In that article, i discussed a technique known among...
|
by: jason735 |
last post by:
Hi,
I've got the following problem. I have to sort x*y elements which are
in one file. I can use only an array for x elements and floor tmp
files which can be read only forward.
Thanks for...
|
by: At_sea_with_C |
last post by:
Hello all,
I have written an ascending sort routine for floats. This seems to do
the job, but for elements over 10,000, it gets awfully slow. A lot of
useless comparisions with previously sorted...
|
by: Ganon11 |
last post by:
So far, the Articles sections are filled with slow sorting algorithms. Bubble Sort and Selection Sort, while very easy to comprehend, are relatively slow algorithms at Θ(n^2) running time. Here, I...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |