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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.