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

array_multisort on $GLOBALS array

P: n/a
I'm trying to use array_multisort to sort by one of the dimensions of an
array stored in $GLOBALS['urls'] like this:

array_multisort($GLOBALS['urls']['desc'], SORT_STRING, SORT_DESC);

Each "row" in $GLOBALS['urls'] contains a value for 'href', 'desc', and
'info' but when I try to pass one of these "columns" to array_multisort() it
whinges about it not being an array or a sort value :-(

Can PHP not understand arrays stored in $GLOBALS or something?
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Frostillicus wrote:
I'm trying to use array_multisort to sort by one of the dimensions of an
array stored in $GLOBALS['urls'] like this:

array_multisort($GLOBALS['urls']['desc'], SORT_STRING, SORT_DESC);

Each "row" in $GLOBALS['urls'] contains a value for 'href', 'desc', and
'info' but when I try to pass one of these "columns" to array_multisort() it
whinges about it not being an array or a sort value :-(
Do a

print_r($GLOBALS['urls']);

to verify the structure of your array.
Can PHP not understand arrays stored in $GLOBALS or something?


Yes, it can. IMO it is /you/ who are not understanding it correctly :)
How do you specify the first row (however you define that) of your
array?

$GLOBALS['urls']?????
I guess it should be something like

$GLOBALS['urls'][0]
which is an array!

$GLOBALS['urls'][0] = array('href'=>'x', 'desc'=>'y', 'info'=>'z');
OTOH -- $GLOBALS['urls']['desc'] does not exist!
Anyway, for what you want, I think you're better off with usort()
http://www.php.net/usort

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2

P: n/a
Thanks, Pedro. Here's how I'm declearing my "array":

$GLOBALS['urls'] = array();
$temp['href'] = html_entity_decode($GLOBALS['href']); // turn & into &
$temp['desc'] = $GLOBALS['desc'];
$temp['info'] = $GLOBALS['info'];
$GLOBALS['urls'][] = $temp;

Then, to loop through, I'm doing this:

foreach ($GLOBALS['urls'] as $subarray) {
print "\t\t<li><a
href=\"{$subarray['href']}\">{$subarray['desc']}</a></li>\n";
}
Am I somehow creating an array of non-arrays, which is why array_multisort()
doesn't know what to do with it?

I'm no PHP expert, in fact, I'd say I'm a PHP newbie (I'm more confident
with Perl, ASP, and Java).

Thanks for any help you can provide.
Jul 17 '05 #3

P: n/a
Frostillicus wrote:
Thanks, Pedro. Here's how I'm declearing my "array":

$GLOBALS['urls'] = array();
$GLOBALS['urls'] is an empty array now.
$temp['href'] = html_entity_decode($GLOBALS['href']); // turn &amp; into &
$temp['desc'] = $GLOBALS['desc'];
$temp['info'] = $GLOBALS['info'];
if $temp didn't exist before these three lines,
$temp is an array with three indexes.
$GLOBALS['urls'][] = $temp;
add /one/ element to $urls (oops ... $GLOBALS['urls']) (accessible as
$urls[0]) and assign $temp to it.

So you have
$urls[0]['href']
$urls[0]['desc']
$urls[0]['info']
Then, to loop through, I'm doing this:

foreach ($GLOBALS['urls'] as $subarray) {
print "\t\t<li><a
href=\"{$subarray['href']}\">{$subarray['desc']}</a></li>\n";
}
$subarray['href'] and $subarray['desc'] do not exist!
You may want to say this instead:

foreach ($GLOBALS['urls'][0] as $subarray) {
or
foreach ($GLOBALS['urls'] as $subarray) {
print "... {$subarray[0]['href']} ...";
}
or, better yet, review how you're using the arrays :-)

Am I somehow creating an array of non-arrays,
No, that (the array of non-arrays) is what you want;
but, in fact, you are creating an array of arrays!
which is why array_multisort() doesn't know what to do with it?


array_multisort() is sorting your sub-arrays in $urls (the [0]), not the
href, info, desc stuff.
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #4

P: n/a
"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
Then, to loop through, I'm doing this:

foreach ($GLOBALS['urls'] as $subarray) {
print "\t\t<li><a
href=\"{$subarray['href']}\">{$subarray['desc']}</a></li>\n";
}


$subarray['href'] and $subarray['desc'] do not exist!
You may want to say this instead:

foreach ($GLOBALS['urls'][0] as $subarray) {
or
foreach ($GLOBALS['urls'] as $subarray) {
print "... {$subarray[0]['href']} ...";
}
or, better yet, review how you're using the arrays :-)


$subarray['desc'] does exist because I'm looping through $GLOBALS['urls']
quite successfully and retrieving the value for 'desc', 'href', etc for each
row in $GLOBALS['urls'] without problem.
Jul 17 '05 #5

P: n/a
Frostillicus wrote:
"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
> Then, to loop through, I'm doing this:
>
> foreach ($GLOBALS['urls'] as $subarray) {
> print "\t\t<li><a
> href=\"{$subarray['href']}\">{$subarray['desc']}</a></li>\n";
> }


$subarray['href'] and $subarray['desc'] do not exist!
You may want to say this instead:

foreach ($GLOBALS['urls'][0] as $subarray) {
or
foreach ($GLOBALS['urls'] as $subarray) {
print "... {$subarray[0]['href']} ...";
}
or, better yet, review how you're using the arrays :-)


$subarray['desc'] does exist because I'm looping through $GLOBALS['urls']
quite successfully and retrieving the value for 'desc', 'href', etc for each
row in $GLOBALS['urls'] without problem.

Oops, indeed they exist. Sorry, I didn't test it.
But $GLOBALS['urls']['desc'] does not exist.

========
pedro@localhost:~$ cat xx.php
<?php

$GLOBALS['urls'] = array();
$temp['href'] = '4href: something';
$temp['desc'] = '4desc: someotherthing';
$temp['info'] = '4info: yetanotherthing';
$GLOBALS['urls'][] = $temp;
$temp['href'] = '7href: something';
$temp['desc'] = '7desc: someotherthing';
$temp['info'] = '7info: yetanotherthing';
$GLOBALS['urls'][] = $temp;
$temp['href'] = '2href: something';
$temp['desc'] = '2desc: someotherthing';
$temp['info'] = '2info: yetanotherthing';
$GLOBALS['urls'][] = $temp;

echo '$GLOBALS[\'urls\']: '; print_r($GLOBALS['urls']);

?>

pedro@localhost:~$ php xx.php
$GLOBALS['urls']: Array
(
[0] => Array
(
[href] => 4href: something
[desc] => 4desc: someotherthing
[info] => 4info: yetanotherthing
)

[1] => Array
(
[href] => 7href: something
[desc] => 7desc: someotherthing
[info] => 7info: yetanotherthing
)

[2] => Array
(
[href] => 2href: something
[desc] => 2desc: someotherthing
[info] => 2info: yetanotherthing
)

)
========

So, when you do

array_multisort($GLOBALS['urls']['desc'], ...);

PHP complains.

If I were you I'd try usort instead

function compare_desc_desc($a, $b) {
if ($a['desc'] == $b['desc']) return 0;
return $a['desc'] < $b['desc'] ? 1 : -1;
}
usort($GLOBALS['urls'], 'compare_desc_desc')
HTH

Sorry again for the mess (I got myself into).
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.