Ali Demirci
Ali Demirci

Reputation: 5442

how to merge keys with same value?

code;

    $all_tags = array();
  while ($row = mysql_fetch_assoc($result)) {
    $all_tags = array_merge($all_tags, explode(' ', $row['title']));
}
$all_tags = array_count_values($all_tags);
echo "<pre>";
arsort($all_tags);

foreach ($all_tags as $key => $val) {
  echo "$key = $val\n";
}

output;

fuups! = 7
401 = 5
Authorization = 5
Required = 5
, = 3
izle = 3
Error = 2
Server = 2
Internal = 2
500 = 2
Full = 1
MegaSinema.net = 1
Sinema = 1
Bad = 1
Request = 1
Film = 1
400 = 1

all i wanna do is merge 'keys' with same integer 'value'. example;

401 = 5
Authorization = 5
Required = 5

to

401 Authorization Required = 5

i don't know how could i do it. i tried a bunch of ways but i never let it work. thank you.

Upvotes: 1

Views: 1094

Answers (3)

Shai Mishali
Shai Mishali

Reputation: 9382

I misunderstood you in the beginning. I think you could just save the objects in an array and implode them if needed.

$out = array();
foreach($array as $key=>$value)
    if(array_key_exists($value, $out))
         $out[$value][] = $key;
    else
         $out[$value] = array($key);

// Then you could do
echo implode(" ", $out[5]); // Should output "401 Authorization Required"

Working example at http://codepad.org/MgLKXA75

Another option is to directly append it and trim the "extra" space at the end.

$out = array();
foreach($array as $key=>$value)
    if(array_key_exists($value, $out))
         $out[$value] .= $key . ' ';
    else
         $out[$value] = $key . ' ';

// Then you could do
echo trim($out[5]); // Should output "401 Authorization Required"

Upvotes: 1

max_
max_

Reputation: 24481

Try this

<?php
    $data = array('fuups!' => '7','401' => '5','Authorization' => '5','Required' => '5',',' => '3','izle' => '3','Error' => '2','Server' => '2','Internal' => '2','500' => '2','Full' => '1','MegaSinema.net' => '1','Sinema' => '1','Bad' => '1','Request' => '1','Film' => '1','400' => '1');
    $rows = array();
    $values = array_values($data);
    asort($values);
    $highestVal = $values[0];
    for ($i = 0; $i <= $highestVal; $i++) {
        foreach ($data as $key => $value) {
            if ($i == $value) {
                $rows[$i] = $rows[$i] . " {$key}";
            }
        }
    }

?>

Working Example XD http://codepad.org/x9uHs1sp

EDIT----

To echo all keys, just replace var_dump($rows) with:

foreach ($rows as $key) {
    echo "{$key}<br />";
}

Upvotes: 1

Bruce Li
Bruce Li

Reputation: 515

I'm not familiar with PHP, but I think you can try to create a hash table to handle this.

For example, if you insert above 3 items into the hash table and use '5' as the key, the first item can be inserted correctly, and the other two should throw exceptions. You can catch the exception since it's excepted and append the values to the value of the first item. So the item in you hash table should be like:

key: 5 value: 401 Authorization Required

But you should make sure about the orders in the value.

Just my 2 cents, good luck!

Update: If throwing exception is not acceptable, you can look-up a key in the hash table first, if the key doesn't exist then insert the key and value, if it already exists then append the value to existing item value.

Upvotes: 0

Related Questions