473,394 Members | 1,889 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,394 software developers and data experts.

array_multisort on $GLOBALS array

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
5 2819
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ward Germonpé | last post by:
Hi, I have a table loaded in an array like so : $table $table $table $table $table $table $table $table $table ...... now I want to sort this table using the ID column.
2
by: michael nieuwenhuizen | last post by:
<newbie alert> i have 2 related arrays that i wanna sort at once. if i try: print_r($my_array1); array_multisort($my_array1, SORT_ASC, $my_array2, SORT_ASC); print_r($my_array1); both...
3
by: nate | last post by:
i'm sorting 2 arrays together. so i have a group of stats in one array and their corresponding owners in the other array. and it sorts them correctly. however, i'm wondering how i can resolve...
7
by: John | last post by:
Hi, I'm looking for the best way to deal with globals in PHP. As a 'C' software developer, I would normally avoid all globals and not have any at all, but use structs and pass everything in...
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
3
by: ojorus | last post by:
Hi! Just two short questions..: 1) In a function I want to get the value of a global variable declared outside the function. Should I use "global $variable" or $GLOBALS? Are there any...
8
by: Michael Wild | last post by:
Hi all I'm not quite new to PHP, but also not very proficient in its use, so please excuse me if my question is a FAQ. Perhaps I didn't know the right terms, but google wasn't my friend this...
1
mikejfe
by: mikejfe | last post by:
Hi all. I have a question that is probably simple to answer, but I don't even know where to get started (i.e. for searching old posts, Google, etc.) I am writing a program that reads in a 3...
3
by: Schraalhans Keukenmeester | last post by:
I'm having a hard time getting to grips with this function. I have an array shaped like this: $dircontents ='index.html' $dircontents = 'www' $dircontents = 'www' $dircontents = 'file' ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.