473,480 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

array_multisort() not working

<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 arrays remain unsorted. if i try:

print_r($my_array1);
sort($my_array1);
print_r($my_array1);

$my_array1 gets sorted. what am i doing wrong? both arrays
are indexed array and they have an equal amount of entries.

mike
Jul 17 '05 #1
2 2708
On Tue, 13 Jan 2004 17:06:47 +0100, "michael nieuwenhuizen" <us****@mikkie.net>
wrote:
<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 arrays remain unsorted. if i try:

print_r($my_array1);
sort($my_array1);
print_r($my_array1);

$my_array1 gets sorted. what am i doing wrong? both arrays
are indexed array and they have an equal amount of entries.


PHP version?
Can you post example data (i.e. something runnable) and output?

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #2

"Andy Hassall" <an**@andyh.co.uk> wrote

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 arrays remain unsorted. if i try:

print_r($my_array1);
sort($my_array1);
print_r($my_array1);

$my_array1 gets sorted. what am i doing wrong? both arrays
are indexed array and they have an equal amount of entries. PHP version?


4.3.2.
Can you post example data (i.e. something runnable)
not sure if it's a good idea to post this all, but here we go.

==== code =============

<?php

$file = 'dvds.xml';

$current_tag = "";
$current_item = -1;

// init arrays
$year = array(); // year
$var1 = array(); // dvd: name; cd: artist; travel: country

// start element
function startElement($parser, $tagname, $attrs) {
global $current_item, $current_tag;
$current_tag = $tagname;
switch ($tagname) {
case "ITEM":
$current_item++;
$year[$current_item] = 0;
$var1[$current_item] = "";
break;
}
}

// end element
function endElement($parser, $tagname) {
}

// character data
function characterData($parser, $data) {
global $current_item, $current_tag, $year, $var1;
switch ($current_tag) {
case "YEAR":
if ($year[$current_item] == 0) {
$year[$current_item] = $data;
}
break;
case "VAR1":
if ($var1[$current_item] == "") {
$var1[$current_item] = $data;
}
break;
}
}

// set parser variables
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

// file not found
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}

// xml error
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}

// free parser
xml_parser_free($xml_parser);

sort_dvds();
print_dvds();

// sort dvds
function sort_dvds() {
global $sort, $year, $var1;
// sort by year
array_multisort($year, SORT_ASC, $var1, SORT_ASC);
}

// print dvds
function print_dvds() {
global $current_item, $sort, $year, $var1;
// print by year
echo " <tr>\n <td class=\"pagetitle\">dvd's</td>\n </tr>\n";
$temp_year = 0;
for ($i = 1; $i <= $current_item; $i++) {
if ($year[$i] != $temp_year) {
echo " <tr>\n <td class=\"title\">$year[$i]</td>\n
</tr>\n";
$temp_year = $year[$i];
}
echo " <tr>\n <td class=\"text\">$var1[$i]</td>\n </tr>\n";
}
}

?>

==== code =============

it reads an xml file (http://mikkie.net/php/sort/dvds.xml), stores the
titles and years in two arrays ($var1 and $year) and tries to output
the list of movies by year.

maybe the script looks a bit funny, but a) it's part of a larger script
(sorting in different ways, sorting & outputting different xml files) and
b) i didn't know ANYTHING of php til monday last week.
and output?


http://mikkie.net/php/sort/data.php

mike
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2822
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains...
2
6228
by: Gary | last post by:
I am trying to use the "System.Windows.Forms.SendKeys" class for triggering the Ctrl+P key. Syntax: System.Windows.Forms.SendKeys.Send("^(P)"); This is not working ..what could be the...
5
2789
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
5
3040
by: tshad | last post by:
I have been working with setting my drop boxes to allow double clicking to select an item. It worked fine until I made some changes. I then stripped the page down to the bare essentials to find...
8
2489
by: jojobar | last post by:
Okay, I am trying to do is to test the webresource in 2.0 1. I created a new project with assembly name (and default assembly name) "Office". 2. I added the following to the AssemblyInfo.cs...
2
14809
by: Don | last post by:
I'm having problems with intellisense, autocomplete, etc. suddenly not working in certain classes of a project I'm working on. All the options are set, and it all works fine for most classes, but...
9
3180
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
0
1279
by: R | last post by:
Hi All, I have simple question can array_multisort be made locale based sort for SORT_STRING flag? or is there a work around to force strcoll when sorting strings? thanks in advance for any...
3
2315
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
7033
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
7027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6861
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...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2987
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.