Jump to content

simple array_multisort problem


darkminos

Recommended Posts

Hi,

 

I am trying to put values into an array and then sort it by 'cost'

 


...
  		$results_array = array('pic' => $pic, 'link' => $link, 'title' => $title, 'cost' => $cost);	
                   
  foreach ($results_array as $key => $row) {
	    $sort_cost[$key]  = $row['cost'];
	}
	array_multisort($sort_cost, SORT_ASC, $results_array);
	echo $results_array['cost']."<br>";
  
...

 

this is what I get:

 

19.31
5.99
17.99
10.59
9.79
16.99
19.99
11.99
10.95
19.99

 

any ideas why it doesn't sort it  :shrug: ? Thanks

Link to comment
https://forums.phpfreaks.com/topic/264514-simple-array_multisort-problem/
Share on other sites

In order for that code to generate that output, it must be inside another loop.

 

I'm not 100% sure but I don't think you're using the function correctly. do a print_r($sort_cost); after the multi sort, it is sorted?

 

this is what it prints

 

Array ( [title] => [link] => [pic] => [cost] => ) Array ( [pic] => [link] => [title] => [cost] => ) Array ( [title] => [link] => [pic] => [cost] => ) Array ( [pic] => [link] => [title] => [cost] => ) Array ( [title] => [link] => [pic] => [cost] => ) Array ( [pic] => [link] => [title] => [cost] => ) Array ( [title] => [link] => [pic] => [cost] => ) Array ( [pic] => [link] => [title] => [cost] => ) Array ( [title] => [link] => [pic] => [cost] => ) Array ( [pic] => [link] => [title] => [cost] => 

 

I'm not sure what is happening here, but I know it's not what I need...  below is the loop I believe you were referring to?

 


  foreach($resp->searchResult->item as $item) {
    $pic   = $item->galleryURL;
    $link  = $item->viewItemURL;
    $title = $item->title;
    $cost  = $item->sellingStatus->currentPrice;	 
  
  		$results_array[] = array('pic' => $pic, 'link' => $link, 'title' => $title, 'cost' => $cost);	
                   

  foreach ($results_array as $key => $row) {
	    $sort_cost[$key]  = $row['cost'];
	}

	array_multisort($sort_cost, SORT_ASC, $results_array);
  	print_r($sort_cost)."<br>";

}





}

Here's how it's supposed to be used. Not tested, because I can't.

 

<?php

foreach ($resp->searchResult->item as $item) {
$pic = $item->galleryURL;
$link = $item->viewItemURL;
$title = $item->title;
$cost = $item->sellingStatus->currentPrice;

$results_array[] = array('pic' => $pic, 'link' => $link, 'title' => $title, 'cost' => $cost);
$sort_cost[] = $cost;
}

array_multisort($sort_cost, SORT_ASC, $results_array);

print_r($results_array);

?>

 

Here's a working example

 

<?php

$results_array = array(
array( 'id'=>1, 'cost'=>'19.31' ),
array( 'id'=>2, 'cost'=>'5.99' ),
array( 'id'=>3, 'cost'=>'17.99' ),
array( 'id'=>4, 'cost'=>'10.59' )
);

foreach( $results_array as $item ) {
$sort_cost[] = $item['cost'];
}

array_multisort($sort_cost, SORT_ASC, $results_array);

print_r($results_array);

?>

 

output

 

Array
(
    [0] => Array
        (
            [id] => 2
            [cost] => 5.99
        )

    [1] => Array
        (
            [id] => 4
            [cost] => 10.59
        )

    [2] => Array
        (
            [id] => 3
            [cost] => 17.99
        )

    [3] => Array
        (
            [id] => 1
            [cost] => 19.31
        )

)

I always find a user sort function easier than array_multisort

 

<?php
foreach ($resp->searchResult->item as $item) {
    $pic = $item->galleryURL;
    $link = $item->viewItemURL;
    $title = $item->title;
    $cost = $item->sellingStatus->currentPrice;

    $results_array[] = array('pic' => $pic, 'link' => $link, 'title' => $title, 'cost' => $cost);
    
}

usort($results_array, 'costsort');

        /***
        * cost sort callback function
        */
function costsort($a, $b)
{
    return $a['cost'] - $b['cost'];
}
?>

I always find a user sort function easier than array_multisort

 

<?php
foreach ($resp->searchResult->item as $item) {
    $pic = $item->galleryURL;
    $link = $item->viewItemURL;
    $title = $item->title;
    $cost = $item->sellingStatus->currentPrice;

    $results_array[] = array('pic' => $pic, 'link' => $link, 'title' => $title, 'cost' => $cost);
    
}

usort($results_array, 'costsort');

        /***
        * cost sort callback function
        */
function costsort($a, $b)
{
    return $a['cost'] - $b['cost'];
}
?>

 

Agreed, for a single search term, this is way better. For more than one, I'd imagine a UDF would get quite complex. Please correct me if I'm wrong.

A simple set of nested if reflecting the sort hierarchy. So if the above were to be sorted on cost ASC / title ASC / link DESC

 

<?php
function multisort ($a, $b)
{
   
    $x1 = ($a['cost'] - $b['cost'];           // numeric ASC
    $x2 = strcmp($a['title'], $b['title']);   // string ASC
    $x3 = strcmp($b['link'], $a['link']);     // string DESC (swap a and b)
    
    if ($x1==0) {                             // simply nest the hierarchy of tests
        if ($x2==0) {
            return $x3;
        }
        else return $x2;
    }
    else return $x1;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.