English 中文(简体)
如何将值合并到相似的密钥
原标题:How to merge values into similar key
  • 时间:2012-05-27 12:41:10
  •  标签:
  • php

我在找一个功能,如果有的话, 来保存或整合价值 成类似的钥匙, 但到目前为止没有运气。

"http://www.php.net/manual/en/formation.array-combine.php" rel="nofollow" >arry_combine 简单地删除了值 :

array_combine(array( a , a , b ), array(1,2,3));

<强 > 返回:

Array
(
    [a] => 2
    [b] => 3
)

<强 > 预期:

Array
(
    [a] => 1,2
    [b] => 3
)

任何提示都非常感激。

谢谢 谢谢

UPDATE: I didn t know, and didn t articulate better about the merged values (1,2), but then I had better accepted Jeroen array of values for easier breakdowns of possible array of values. 谢谢 谢谢 to everyone who has kindly helped.

最佳回答

这一点:

function array_combine_custom($arr1, $arr2) {
    $out = array();

    $arr1 = array_values($arr1);
    $arr2 = array_values($arr2);

    foreach($arr1 as $key1 => $value1) {
        $out[(string)$value1] [] = $arr2[$key1];
    }

    return $out;
}

返回时间 :

Array
(
    [a] => Array
        (
            [0] => 1
            [1] => 2
        )

    [b] => Array
        (
            [0] => 3
        )

)
问题回答
$out = array();
foreach ($arr1 as $v) {
  $out[$v] .= (strlen($out[$v]) ?  ,  :   ).array_shift($arr2);
}

$out 现在:

Array
(
  [a] => 1,2
  [b] => 3
)




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签