在PHP中使用类似的值组合键

时间:2013-07-09 17:06:03

标签: php

我正在尝试在键具有相似值时组合键,并期望将结果作为字符串。实际上我不知道如何实现这一目标,尽管我进行了一些测试。

$array = array(
  'a' => 65,
  'b' => 31,
  'c' => 100,
  'd' => 31,
  'e' => 31,
  'f' => 31,
  'h' => 23,
  'i' => 23,
  'j' => 23,
  'k' => 23,
  'l' => 48,
  'm' => 48,
);

$results = array();
foreach ($array as $k => $v) {
  // This is my attempt among others to no luck.
  // Can not use array_key_exists because values are unpredictable.
  $similars[$v] = $k;
  $results[$v] = implode(", ", array_unique($similars)) . ' : ' . $v;
}

var_dump(implode("\n ", $results));

您可以查看输出: http://codepad.org/ECekF3dq

我快到了,但显然是错的:(

不期望:

string(72) "a : 65
 a, f, c : 31
 a, b, c : 100
 a, f, c, k : 23
 a, f, c, k, m : 48"

预期:

a : 65
b, d, e, f : 31
c : 100
h, i, j, k : 23
l, m : 48

具有相同值的那些应折叠为一行。

感谢任何提示。

5 个答案:

答案 0 :(得分:2)

您希望将$similars放入数组数组中。这样,您可以使用该值保持所有键。

$results = array();
$similars = array();

foreach ($array as $k => $v) {
  if(!isset($similars[$v])){
    $similars[$v] = array($k);
  }
  else{
    $similars[$v][] = $k;
  }

  $results[$v] = implode(", ", $similars[$v]) . ' : ' . $v;
}

var_dump(implode("\n ", $results));

答案 1 :(得分:1)

可能有一种更优雅的方式,但我可能会使用两个简单的foreach循环:

function parseArray($data)
{
    $tmp = array();
    foreach ($data as $key => $item) {
        // Check if we already created this key
        if (isset($tmp[$item])) {
            // Append if so
            $tmp[$item] .= ', '.$key;
        } else {
            // Init if not
            $tmp[$item] = $key;
        }
    }

    // Now we stringify the tmp array.

    $result = '';
    foreach ($tmp as $key => $value) {
        $result .= "$key : $value\n";
    }

    return $result;
}

答案 2 :(得分:1)

$tmp = array();
foreach ($array as $k => $v) $tmp[$v][] = $k;
foreach ($tmp as $k => $v) echo implode(', ', $v) . ' : ' . $k . "\n<br />";  

答案 3 :(得分:1)

我认为扭转关键和价值观可能是有益的。将整数转换为键,将字符转换为数组值。这将使您能够保持所需的一对多关系:

$array = array(
  'a' => 65,
  'b' => 31,
  'c' => 100,
  'd' => 31,
  'e' => 31,
  'f' => 31,
  'h' => 23,
  'i' => 23,
  'j' => 23,
  'k' => 23,
  'l' => 48,
  'm' => 48,
);

$results = array();

foreach ($array as $k => $v) {
    $results[$v][]=$k;
}


foreach ($results as $k=>$v) {
    echo implode(',',$v) . " : " . $k . "\r\n";
}

多维数组$ results equals:

array(5) {
  [65]=>
  array(1) {
    [0]=>
    string(1) "a"
  }
  [31]=>
  array(4) {
    [0]=>
    string(1) "b"
    [1]=>
    string(1) "d"
    [2]=>
    string(1) "e"
    [3]=>
    string(1) "f"
  }
  [100]=>
  array(1) {
    [0]=>
    string(1) "c"
  }
  [23]=>
  array(4) {
    [0]=>
    string(1) "h"
    [1]=>
    string(1) "i"
    [2]=>
    string(1) "j"
    [3]=>
    string(1) "k"
  }
  [48]=>
  array(2) {
    [0]=>
    string(1) "l"
    [1]=>
    string(1) "m"
  }
}

最终输出:

a : 65
b,d,e,f : 31
c : 100
h,i,j,k : 23
l,m : 48

答案 4 :(得分:1)

试试这个

$array = array('a' => 65,'b' => 31,'c' => 100,'d' => 31,'e' => 31,'f' => 31,'h' => 23,
               'i' => 23,'j' => 23,'k' => 23,'l' => 48,'m' => 48);
$similars = array();
foreach ($array as $k => $v) {
    // This is my attempt among others to no luck.
    // Can not use array_key_exists because values are unpredictable.
    $key = array_search($v, $similars);
    if (isset($similars[$key])) {
        unset($similars[$key]);
        $similars[$key.','.$k] = $v;
    }
    else
        $similars[$k] = $v;
}

print_r($similars);
//display as expected
foreach($similars as $k => $v)
  echo "</br>".$k.':'.$v;