JSHINER Posted February 25, 2009 Share Posted February 25, 2009 Array ( [bread] => 19 [butter] => 19 [coke] => 4 [orange] => 7 ) How can I get this into an array that looks like this? $terms[] = array('term' => $term, 'counter' => $counter); So 'term' would be "bread" and 'counter' would be "19" and so on. Quote Link to comment https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/ Share on other sites More sharing options...
samshel Posted February 25, 2009 Share Posted February 25, 2009 $origArr = array("bread"=>19, "butter"=>19, "coke"=>4, "orange"=> 7); $newArr = array(); foreach($origArr as $key=>$val) { $item = array(); $item["term"] = $key; $item["counter"] = $val; $newArr[] = $item; } not tested. Quote Link to comment https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/#findComment-771279 Share on other sites More sharing options...
JSHINER Posted February 25, 2009 Author Share Posted February 25, 2009 Works perfect. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/#findComment-771289 Share on other sites More sharing options...
JSHINER Posted February 25, 2009 Author Share Posted February 25, 2009 one more thing... can you sort the array by "counter" ? Quote Link to comment https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/#findComment-771293 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 array The manual with different array functions. sort should give you what you want. Quote Link to comment https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/#findComment-771295 Share on other sites More sharing options...
JSHINER Posted February 25, 2009 Author Share Posted February 25, 2009 Tried going through that page - none of the sorts seem to be working. foreach($interestCount as $key=>$val) { $item = array(); $item["term"] = $key; $item["counter"] = $val; if ($val> $maximum) $maximum = $val; $terms[] = $item; } How can I sort that so it sorts by "counter" highest to lowest. Quote Link to comment https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/#findComment-771321 Share on other sites More sharing options...
samshel Posted February 25, 2009 Share Posted February 25, 2009 sort your original first on counter, then do this foreach. Quote Link to comment https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/#findComment-771329 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 <?php function sortCounter($a, $b, $key="counter") { return ($a[$key] < $b[$key]); } $items[0]["counter"] = 18; $items[1]["counter"] = 19; $items[2]["counter"] = 1; usort($items, "sortCounter"); print_r($items); die(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/#findComment-771330 Share on other sites More sharing options...
sasa Posted February 25, 2009 Share Posted February 25, 2009 try <?php $origArr = array("bread"=>19, "butter"=>19, "coke"=>4, "orange"=> 7); $newArr = array(); foreach($origArr as $key=>$val) { $item = array(); $item["counter"] = $val; $item["term"] = $key; $newArr[] = $item; } rsort($newArr); print_r($newArr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/#findComment-771333 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.