darkminos Posted June 20, 2012 Share Posted June 20, 2012 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 ? Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 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? Quote Link to comment Share on other sites More sharing options...
darkminos Posted June 20, 2012 Author Share Posted June 20, 2012 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>"; } } Quote Link to comment Share on other sites More sharing options...
xyph Posted June 20, 2012 Share Posted June 20, 2012 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 ) ) Quote Link to comment Share on other sites More sharing options...
darkminos Posted June 20, 2012 Author Share Posted June 20, 2012 Thank you for that, I would work perfectly if not the fact that $cost is being returned as a SimpleXMLElement Object :-\ and looks something like this... SimpleXMLElement Object ( [@attributes] => Array ( [currencyId] => GBP ) [0] => 19.99 ) Quote Link to comment Share on other sites More sharing options...
xyph Posted June 21, 2012 Share Posted June 21, 2012 Cast it as a string. SimpleXML supports this $cost = (string) $item->sellingStatus->currentPrice; Quote Link to comment Share on other sites More sharing options...
Barand Posted June 21, 2012 Share Posted June 21, 2012 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']; } ?> Quote Link to comment Share on other sites More sharing options...
darkminos Posted June 21, 2012 Author Share Posted June 21, 2012 Thank you everyone for the answers, I did work out that I have to cast (string) yesterday but couldn't log on the forum to inform everyone that I am all sorted now. Thanks again, very helpful replies indeed! Quote Link to comment Share on other sites More sharing options...
xyph Posted June 21, 2012 Share Posted June 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 21, 2012 Share Posted June 21, 2012 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; } ?> Quote Link to comment Share on other sites More sharing options...
darkminos Posted June 23, 2012 Author Share Posted June 23, 2012 On a different note, how quick is array_multisort compared with other sort methods? I am writing a script to get results from multiple sources, combine them, and sort. There will be on average about 5k-8k results to be sorted Quote Link to comment Share on other sites More sharing options...
Barand Posted June 23, 2012 Share Posted June 23, 2012 Run a benchmark test Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.