Jump to content

[SOLVED] Help Making an Array


JSHINER

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/146909-solved-help-making-an-array/
Share on other sites

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.

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);
?>

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.