473,666 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort array without index

Hi guys I'd like to sort a multidimensiona l array that hasn't numerical
index, in fact it is like:

Array
(
[mobile] => Array
(
[name_str] => mobile
[relationCount_i nt] => 1
)

[video] => Array
(
[name_str] => video
[relationCount_i nt] => 2
)

[flash] => Array
(
[name_str] => flash
[relationCount_i nt] => 1
)

[tool] => Array
(
[name_str] => tool
[relationCount_i nt] => 1
)

)

And I'd like to sort, for instance for the value of
"relationCount_ int", so the new order should be something like:

Array
(
[video] => Array
(
[name_str] => video
[relationCount_i nt] => 2
)
[mobile] => Array
(
[name_str] => mobile
[relationCount_i nt] => 1
)
[flash] => Array
(
[name_str] => flash
[relationCount_i nt] => 1
)

[tool] => Array
(
[name_str] => tool
[relationCount_i nt] => 1
)

)

Any idea? I tried some script I found on php.net but didn't work. The
could should run inside a class method.

Thanks a lot, chr

Feb 6 '06 #1
4 2545
ch********@late ral.net wrote:
Hi guys I'd like to sort a multidimensiona l array that hasn't numerical
index, in fact it is like:

Array
(
[mobile] => Array
(
[name_str] => mobile
[relationCount_i nt] => 1
)

[video] => Array
(
[name_str] => video
[relationCount_i nt] => 2
)

[flash] => Array
(
[name_str] => flash
[relationCount_i nt] => 1
)

[tool] => Array
(
[name_str] => tool
[relationCount_i nt] => 1
)

)

And I'd like to sort, for instance for the value of
"relationCount_ int", so the new order should be something like:

Array
(
[video] => Array
(
[name_str] => video
[relationCount_i nt] => 2
)
[mobile] => Array
(
[name_str] => mobile
[relationCount_i nt] => 1
)
[flash] => Array
(
[name_str] => flash
[relationCount_i nt] => 1
)

[tool] => Array
(
[name_str] => tool
[relationCount_i nt] => 1
)

)

Any idea? I tried some script I found on php.net but didn't work. The
could should run inside a class method.

Thanks a lot, chr

Hi

Yes, it is easy if you use uksort().

-----------------------------------
uksort

(PHP 3 >= 3.0.4, PHP 4, PHP 5)
uksort -- Sort an array by keys using a user-defined comparison function
-----------------------------------

Read more here:
http://nl2.php.net/manual/en/function.uksort.php

Regards,
Erwin Moller
Feb 7 '06 #2
Hi Erwin, I tried in many ways, using also usort but it doesn't work.
This is my last test:

- iniside the method of the class -
usort($arr, array ($this,"cmp"));

function cmp($a, $b)
{
if ($a['relationCount_ int'] == $b['relationCount_ int']) {
return 0;
}
return ($a['relationCount_ int'] > $b['relationCount_ int']) ? -1 : 1;
}

debugging it is comparing the right values, but the order doesn't
change. Could be the fact that the array has as index a string instead
of number? Cheers, chr

Feb 7 '06 #3
Actually, I tried the sorting outside my class and it works properly:

$arr = array();
$arr['del.icio.us'] = array( "relationCount_ int" => 1, "name_str" =>
"del.icio.u s");
$arr['php'] = array( "relationCount_ int" => 1, "name_str" => "php");
$arr['framework'] = array( "relationCount_ int" => 2, "name_str" =>
"framework" );
$arr['javascript'] = array( "relationCount_ int" => 3, "name_str" =>
"javascript ");
$arr['google'] = array( "relationCount_ int" => 1, "name_str" =>
"google");
$arr['xml'] = array( "relationCount_ int" => 1, "name_str" => "xml");
$arr['RIA'] = array( "relationCount_ int" => 1, "name_str" => "RIA");
$arr['library'] = array( "relationCount_ int" => 1, "name_str" =>
"library");
$arr['design'] = array( "relationCount_ int" => 1, "name_str" =>
"design");
$arr['development'] = array( "relationCount_ int" => 2, "name_str" =>
"developmen t");
$arr['webdesign'] = array( "relationCount_ int" => 1, "name_str" =>
"webdesign" );
$arr['web'] = array( "relationCount_ int" => 1, "name_str" => "web");

function cmp($a, $b)
{
//print_r($a['relationCount_ int']." ==
".$b['relationCount_ int']."<br>");
if ($a['relationCount_ int'] == $b['relationCount_ int']) {
return 0;
}
return ($a['relationCount_ int'] > $b['relationCount_ int']) ? -1 : 1;
}

usort($arr, "cmp");
print "<pre>";
print_r($arr);
print "</pre>";

I have problem on modifying the array of my class, I tried even to
remove the elements but I can't:

foreach($this->relatedtags_ar r as $relatedtag){
unset($this->relatedtags_ar r[$relatedtag['name_str']]);
}

Although the count() becomes 0, the elements are still there. How could
modify that array, and how could I do inside my class? Any idea? Thanks
a lot, chr

Feb 7 '06 #4
Le Tue, 07 Feb 2006 21:10:57 +0100, <ch********@lat eral.net> a écrit:
Actually, I tried the sorting outside my class and it works properly: .... usort($arr, "cmp");

If you want to keep the key associations, you must use uasort() instead of
usort().
I have problem on modifying the array of my class, I tried even to
remove the elements but I can't:

foreach($this->relatedtags_ar r as $relatedtag){
unset($this->relatedtags_ar r[$relatedtag['name_str']]);


Why do you replicate your key in the value ? Seems very complex without
need. You can keep only the key and sort on it, event with a user compare
function (with uksort). But maybe the best solution would be the opposite
method : removing the string keys and keeping the value field...

You can compare on any field with the following code inside your class :

var $field;
var $a; // Array

function sort_array($fie ld)
{
$this->field=$field ;
uasort($a,$a,$t his->cmp);
}

function cmp($a1,$a2)
{
return strcmp($a1[$this->field],$a2[$this->field]);
}
//-- End of class

And you sort your array with something like :

$object->sort_array('fi eld_name');
Feb 7 '06 #5

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

Similar topics

21
3198
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
5
2834
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the values 28 142 3 17 225. I can sort this array in ascending order and it will return 3 17 28 142 225. But I want a method that will return the sort order, not the array in sorted
48
4456
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
0
8448
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8640
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
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 we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.