By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

Sort question - make elements with = sign first

P: n/a

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
Jul 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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)
Jul 16 '05 #2

P: n/a
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
Jul 16 '05 #3

P: n/a

"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.
Jul 16 '05 #4

P: n/a
"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...
Jul 16 '05 #5

P: n/a
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
Jul 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.