PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > sorting in PHP.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
sorting in PHP.

Réponse
 
LinkBack Outils de la discussion
Vieux 26/03/2008, 21h00   #1
amit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sorting in PHP.



Hello group,

I'm getting data string as following. One of the columns is unixtime
(10th element in each row) . How can I sort my array base on that?

"310","true","6824567491","682087491","714469","xx xxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432"
"310","false","681224591","682087491","667256827", "yyyyy","yyyy","yyyy","yyyy","yyyy","120604783 7"

Thanks.
  Réponse avec citation
Vieux 26/03/2008, 22h04   #2
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting in PHP.

..oO(amit)

>I'm getting data string as following. One of the columns is unixtime
>(10th element in each row) . How can I sort my array base on that?
>
>"310","true","6824567491","682087491","714469","x xxxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432"
>"310","false","681224591","682087491","667256827" ,"yyyyy","yyyy","yyyy","yyyy","yyyy","120604783 7"


Write your own sorting function to use with usort().

Micha
  Réponse avec citation
Vieux 26/03/2008, 22h17   #3
Alexey Kulentsov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting in PHP.

amit wrote:
> I'm getting data string as following. One of the columns is unixtime
> (10th element in each row) . How can I sort my array base on that?
>
> "310","true","6824567491","682087491","714469","xx xxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432"
> "310","false","681224591","682087491","667256827", "yyyyy","yyyy","yyyy","yyyy","yyyy","120604783 7"


bad way but illustrates usort()
--------------------------------
define('TS_REGEXP','/^(?:"[^"]*",){10}"(\d+)"$/');

// data
$d=array
('"310","true","6824567491","682087491","714469"," xxxxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432" '
,'"310","false","681224591","682087491","667256827 ","yyyyy","yyyy","yyyy","yyyy","yyyy","1206047837" '
);

// Extract and compare timestamps from two rows
function rowCompare($a,$b)
{
preg_match(TS_REGEXP,$a,$ar)reg_match(TS_REGEXP, $b,$br);
return (int)$ar[1] > (int)$br[1];
}

// sort array
usort($d,'rowCompare');
// sorted array in $d
--------------------------------


good way but it is depends where you will to use results.
--------------------------------
define('TS_REGEXP','/^(?:"[^"]*",){10}"(\d+)"$/');

// data
$d=array
('"310","true","6824567491","682087491","714469"," xxxxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432" '
,'"310","false","681224591","682087491","667256827 ","yyyyy","yyyy","yyyy","yyyy","yyyy","1206047837" '
);

// additional array
$d1=array();

// make index
foreach($d as $data)
{
preg_match(TS_REGEXP,$data,$key);
$d1[(int)$key[1]]=$data;
}
ksort($d1);
// sorted array in $d1
--------------------------------

one more index, no data copy in it:
--------------------------------
....
$ts_index=array();
foreach($d as $index=>$data)
{
preg_match($r,$data,$key);
$ts_index[(int)$key[1]]=$index;
}

ksort($ts_index);
$ts_index=array_values($ts_index);

// loop ordered by timestamp
foreach($ts_index as $index)
echo $d[$index]."\n";

--------------------------------



?>
  Réponse avec citation
Vieux 26/03/2008, 22h59   #4
amit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting in PHP.

On Mar 26, 2:04pm, Michael Fesser <neti...@gmx.de> wrote:
> .oO(amit)
>
> >I'm getting data string as following. One of the columns is unixtime
> >(10th element in each row) . How can I sort my array base on that?

>
> >"310","true","6824567491","682087491","714469","x xxxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432"
> >"310","false","681224591","682087491","667256827" ,"yyyyy","yyyy","yyyy","yyyy","yyyy","12060478 37"

>
> Write your own sorting function to use with usort().
>
> Micha



Hi Micha,

I did and for some reasons (I'm trying to undersetand why) I get this:
(sort base on the last value which is unitime). It is not sorted in
fact only the places are changed.


Before sort:
Array
(
[0] => 31042035360,send,
629956827,629956827,682087491,xxxx,xxx,xxxx,xxxx,x xxx, 1206043792
[1] => 31042035360,send,
629956827,629956827,682087491,www,wwww,wwww,wwwww, wwwww, 1206045678
[2] => 31042035360,send,
629956827,629956827,1070753141,ddddd,ddddd,ddddd,d dd,dddd, 1206044017
)

After sort:
Array
(

[0] => 31042035360,send,
629956827,629956827,1070753141,ddd,dddd,ddddd,dddd d,dddd, 1206044017
[1] => 31042035360,send,
629956827,629956827,682087491,wwww,wwwww,wwwwww,ww ww,wwww,1206045678
[2] => 31042035360,send,
629956827,629956827,682087491,xxx,xxx,xxxx,xxxx,xx xx, 1206043792
)

Thanks.
  Réponse avec citation
Vieux 26/03/2008, 23h01   #5
amit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting in PHP.

On Mar 26, 2:17pm, Alexey Kulentsov <a...@inbox.ru> wrote:
> amit wrote:
> > I'm getting data string as following. One of the columns is unixtime
> > (10th element in each row) . How can I sort my array base on that?

>
> > "310","true","6824567491","682087491","714469","xx xxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432"
> > "310","false","681224591","682087491","667256827", "yyyyy","yyyy","yyyy","yyyy","yyyy","12060478 37"

>
> bad way but illustrates usort()
> --------------------------------
> define('TS_REGEXP','/^(?:"[^"]*",){10}"(\d+)"$/');
>
> // data
> $d=array
> ('"310","true","6824567491","682087491","714469"," xxxxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432 "'
> ,'"310","false","681224591","682087491","667256827 ","yyyyy","yyyy","yyyy","yyyy","yyyy","1206047837 "'
> );
>
> // Extract and compare timestamps from two rows
> function rowCompare($a,$b)
> {
> preg_match(TS_REGEXP,$a,$ar)reg_match(TS_REGEXP, $b,$br);
> return (int)$ar[1] > (int)$br[1];
>
> }
>
> // sort array
> usort($d,'rowCompare');
> // sorted array in $d
> --------------------------------
>
> good way but it is depends where you will to use results.
> --------------------------------
> define('TS_REGEXP','/^(?:"[^"]*",){10}"(\d+)"$/');
>
> // data
> $d=array
> ('"310","true","6824567491","682087491","714469"," xxxxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432 "'
> ,'"310","false","681224591","682087491","667256827 ","yyyyy","yyyy","yyyy","yyyy","yyyy","1206047837 "'
> );
>
> // additional array
> $d1=array();
>
> // make index
> foreach($d as $data)
> {
> preg_match(TS_REGEXP,$data,$key);
> $d1[(int)$key[1]]=$data;}
>
> ksort($d1);
> // sorted array in $d1
> --------------------------------
>
> one more index, no data copy in it:
> --------------------------------
> ...
> $ts_index=array();
> foreach($d as $index=>$data)
> {
> preg_match($r,$data,$key);
> $ts_index[(int)$key[1]]=$index;
>
> }
>
> ksort($ts_index);
> $ts_index=array_values($ts_index);
>
> // loop ordered by timestamp
> foreach($ts_index as $index)
> echo $d[$index]."\n";
>
> --------------------------------
>
> ?>



Thanks indeed for you solution using regular experession but I'm
afraid to get into trouble later since I'm not familiar with RE yet.

Any other suggestions?

Regards.

  Réponse avec citation
Vieux 26/03/2008, 23h13   #6
Alexey Kulentsov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting in PHP.

amit wrote:
> Thanks indeed for you solution using regular experession but I'm
> afraid to get into trouble later since I'm not familiar with RE yet.
>
> Any other suggestions?

I highly recommend to learn regular expressions because this is very
useful tool for parsing any complex string data. And don't afraid to use
it - in the case of problems here is enough peoples in the world to
you.
  Réponse avec citation
Vieux 26/03/2008, 23h51   #7
amit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting in PHP.

On Mar 26, 3:13pm, Alexey Kulentsov <a...@inbox.ru> wrote:
> amit wrote:
> > Thanks indeed for you solution using regular experession but I'm
> > afraid to get into trouble later since I'm not familiar with RE yet.

>
> > Any other suggestions?

>
> I highly recommend to learn regular expressions because this is very
> useful tool for parsing any complex string data. And don't afraid to use
> it - in the case of problems here is enough peoples in the world to
> you.



Ok then I'll go for it. I'll keep you posted.

Thanks!
  Réponse avec citation
Vieux 27/03/2008, 00h58   #8
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting in PHP.

..oO(amit)

>Thanks indeed for you solution using regular experession but I'm
>afraid to get into trouble later since I'm not familiar with RE yet.
>
>Any other suggestions?


Here's another one:

The strings look like CSV, so you could try to split them. Currently PHP
can only read CSV data from files, but in coming PHP versions there will
be a function to parse CSV strings. For now you could use this hackish
replacement until the official str_getcsv() becomes available:

if (!function_exists('str_getcsv')) {
function str_getcsv($input, $delimiter = ',', $enclosure = '"',
$escape = '\\') {
$fh = fopen('php://memory', 'w+');
fwrite($fh, $input);
rewind($fh);
$result = fgetcsv($fh, 0, $delimiter, $enclosure);
fclose($fh);
return $result;
}
}

With this function you can easily split your data strings and should be
able to use usort() to sort them on the 10th item.

HTH
Micha
  Réponse avec citation
Vieux 27/03/2008, 10h52   #9
Robin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting in PHP.

amit wrote:
> On Mar 26, 2:04 pm, Michael Fesser <neti...@gmx.de> wrote:
>> .oO(amit)
>>
>>> I'm getting data string as following. One of the columns is unixtime
>>> (10th element in each row) . How can I sort my array base on that?
>>> "310","true","6824567491","682087491","714469","xx xxx","xxxxx","xxxxx","xxxx","xxxxx","1206047432"
>>> "310","false","681224591","682087491","667256827", "yyyyy","yyyy","yyyy","yyyy","yyyy","12060478 37"

>> Write your own sorting function to use with usort().
>>
>> Micha

>
>
> Hi Micha,
>
> I did and for some reasons (I'm trying to undersetand why) I get this:
> (sort base on the last value which is unitime). It is not sorted in
> fact only the places are changed.
>
>
> Before sort:
> Array
> (
> [0] => 31042035360,send,
> 629956827,629956827,682087491,xxxx,xxx,xxxx,xxxx,x xxx, 1206043792
> [1] => 31042035360,send,
> 629956827,629956827,682087491,www,wwww,wwww,wwwww, wwwww, 1206045678
> [2] => 31042035360,send,
> 629956827,629956827,1070753141,ddddd,ddddd,ddddd,d dd,dddd, 1206044017
> )
>
> After sort:
> Array
> (
>
> [0] => 31042035360,send,
> 629956827,629956827,1070753141,ddd,dddd,ddddd,dddd d,dddd, 1206044017
> [1] => 31042035360,send,
> 629956827,629956827,682087491,wwww,wwwww,wwwwww,ww ww,wwww,1206045678
> [2] => 31042035360,send,
> 629956827,629956827,682087491,xxx,xxx,xxxx,xxxx,xx xx, 1206043792
> )
>
> Thanks.


I'ld convert that array in to two dimensions first:

<?php
echo "Before convert:\n";
print_r($a);

$b = array();
foreach ($a as $r) {
$b[]=explode(',',$r);
}

echo "After convert:\n";
print_r($b);
?>

Then you can use usort as follows:

<?php
echo "Before Sort:\n";
print_r($b);

function datecmp($a,$b) {
return ($a[10]==$b[10]?0$a[10]<$b[10]?-1:1));
}

usort($b,datecmp);

echo "After Sort:\n";
print_r($b);
?>

Regards
Robin
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h29.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18742 seconds with 17 queries