百科狗-知识改变命运!
--

array_combine() - 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 - php 数组函数

乐乐1年前 (2023-11-21)阅读数 21#技术干货
文章标签数组

array_combine()

(PHP 5, PHP 7)

创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值

说明

array_combine(array $keys,array $values): array

返回一个array,用来自$keys数组的值作为键名,来自$values数组的值作为相应的值。

参数

$keys

将被作为新数组的键。非法的值将会被转换为字符串类型(string)。

$values

将被作为Array的值。

返回值

返回合并的array,如果两个数组的单元数不同则返回FALSE

错误/异常

array_combine() - 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 - php 数组函数

如果作为$keys的数组和作为$values的数组的元素个数不一样,将会抛出一个警告错误(E_WARNING)。

更新日志

版本说明
5.4.0(修复)早期版本中如果是空数组就报E_WARNING的错并且返回FALSE

范例

一个array_combine()简单的例子

以上例程会输出:

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)

参见

  • array_merge() 合并一个或多个数组
  • array_walk() 使用用户自定义函数对数组中的每个元素做回调处理
  • array_values() 返回数组中所有的值
If two keys are the same, the second one prevails. 
Example:

Returns:
Array
(
  [a] => 2
  [b] => 3
)
But if you need to keep all values, you can use the function below:

Returns:
Array
(
  [a] => Array
    (
      [0] => 1
      [1] => 2
    )
  [b] => 3
)
Further to loreiorg's script 
in order to preserve duplicate keys when combining arrays.
I have modified the script to use a closure instead of create_function
Reason: see security issue flagged up in the documentation concerning create_function 
array_combine() has a strange bug/misfeature (as of PHP 5.3.2): There is no logical reason for  throwing a warning and returning FALSE, instead of returning  (see http://bugs.php.net/bug.php?id=34857). Here is a quick workaround: 
I recently had to flip an array and group the elements by value, this snippet will do that:

Example:

Returns:
Array
(
  [US] => Array
    (
      [0] => username1
      [1] => user2
    )
  [GB] => Array
    (
      [0] => newuser
    )
)
I needed to read CSV files into associative arrays with column headers as keys. Then I ran into a problem when you have empty columns at the end of a row because array_combine returns false if both arrays don't have the same number of elements. This function based on quecoder at gmail's combine_arr() below allowed me to pad either array or not when parsing my CSVs to arrays.
$a is the array of header columns and $b is an array of the current row retrieved with fgetcsv() 
I was looking for a function that could combine an array to multiple one, for my MySQL GROUP_CONCAT() query, so I made this function. 
Some tips for merging same values in an array

Output ::
After array_merge :
Array
(
  [0] => 1
  [1] => 2
  [2] => 3
  [3] => 4
  [4] => 5
  [5] => 6
  [6] => 7
  [7] => 8
  [8] => 9
  [9] => 10
  [10] => 11
  [11] => 12
  [12] => 1
  [13] => 2
  [14] => 3
  [15] => 13
)
After Double Flip : 
Array
(
  [12] => 1
  [13] => 2
  [14] => 3
  [3] => 4
  [4] => 5
  [5] => 6
  [6] => 7
  [7] => 8
  [8] => 9
  [9] => 10
  [10] => 11
  [11] => 12
  [15] => 13
)
Khaly's PHP4 code below does not work correctly in all cases. Consider when your array consists of floats:

What can you do? In my case, I was just zipping up some select-box options, so I converted everything in my floats to strings.
Function eliminates the error Throws E_WARNING if the number of elements in keys and values ​​does not match.
function arrCombine($arr1 = array(),$arr2 = array()){
      if(is_array($arr1) && is_array($arr2)):
        $cntArr1 = count($arr1);
        $cntArr2 = count($arr2);
        $difference = max($cntArr1,$cntArr2) - min($cntArr1,$cntArr2);
        if($cntArr1 > $cntArr2):
          for ($i=1;$i string 'smith' (length=5)
 5 => int 3
 6 => int 3
 7 => int 3
 8 => int 3
var_dump( arrCombine($array2,$array) );
array (size=6)
 'john' => int 1
 'smith' => int 4
 3 => int 5
 4 => int 6
 5 => int 7
 6 => int 8
You can create an array hashset from a flat array, storing both keys and values, with array_combine(). This works with duplicate values in the array too.

This outputs the following:
array(4) {
  ["one"]=>
    string(3) "one"
  ["two"]=>
    string(3) "two"
  ["three"]=>
    string(5) "three"
  ["four"]=>
    string(4) "four"
}
Results: Array ( [one] => green [two] => blue )
NOT: Array ( [one] => red [one] => green [two] => blue )
array_combine() returns NULL instrad of FALSE, when non-array parameters are given (issuing warning). 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)