array_combine

(PHP 5)

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

说明

array array_combine ( array keys, array values )

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

如果两个数组的单元数不同或者数组为空时返回 FALSE

例子 1. 简单的 array_combine() 例子

<?php
$a
= array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

上例将输出:

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

参见 array_merge()array_walk()array_values()


add a note add a note User Contributed Notes
MSpreij, c/o GMail
10-Sep-2006 04:47
And another one..
<?php
if (! function_exists('array_combine')) {
  function
array_combine($keys, $values) {
   foreach(
$keys as $key) $out[$key] = array_shift($values);
   return
$out;
  }
}
?>

If there are less values then keys, the extra values will be NULL (that's what array_shift returns).
glashio.at.gmail.com
07-Dec-2005 06:00
I encounterd a "Fatal Error" on PHP4 while i developed on PHP5 using : array_combine($a, $b);

__TOP_OF_SCRIPT__

if (!function_exists('array_combine')) {
   function array_combine($a, $b) {
       $c = array();
       if (is_array($a) && is_array($b))
           while (list(, $va) = each($a))
               if (list(, $vb) = each($b))
                   $c[$va] = $vb;
               else
                   break 1;
       return $c;
   }
}
m at momech dot dk
01-Nov-2005 11:55
An 3-liner way to get the effect of array_combine in PHP4:
<?php
$keysValues
= array();
foreach(
$csvFieldnames as $indexnum => $key)
  
$keyValues[$key] = $values[$indexnum];
?>

/ Mogens
Ivo van Sandick
02-Sep-2005 04:04
Such a useful function, and only since version 5! Why an emulation for earlier PHP versions has not been posted long ago, is a mystery:

<?php
function array_combine_emulated( $keys, $vals ) {
 
$keys = array_values( (array) $keys );
 
$vals = array_values( (array) $vals );
 
$n = max( count( $keys ), count( $vals ) );
 
$r = array();
 for(
$i=0; $i<$n; $i++ ) {
 
$r[ $keys[ $i ] ] = $vals[ $i ];
 }
 return
$r;
}
?>
ifeghali at interveritas dot net
27-Feb-2005 02:53
Use that code to group an array by its first element.

<?

function groupbyfirst($array)
{
   foreach (
$array as $row)
   {
      
$firstkey = array_keys($row);
      
$firstkey = $firstkey[0];
      
$key = $row[$firstkey];
       unset(
$row[$firstkey]);
      
$newarray[$key][] = $row;
   }
   return
$newarray;
}

?>

Example:

<?

$array
=
Array(
  
0 => Array('color' => 'red','name' => 'apple', 'quantity' => '3'),
  
1 => Array('color' => 'green','name' => 'pear', 'quantity' => '2'),
  
2 => Array('color' => 'yellow','name' => 'corn', 'quantity' => '3'),
  
3 => Array('color' => 'blue','name' => 'grape', 'quantity' => '4'),
  
4 => Array('color' => 'yellow','name' => 'banana', 'quantity' => '13'),
);

$output = groupbyfirst($array);
print_r($output);

?>

will return:

Array
(
 [red] => Array ( [0] => Array ( [name] => apple [quantity] => 3 ) )
 [green] => Array ( [0] => Array ( [name] => pear [quantity] => 2 ) )
 [yellow] => Array ( [0] => Array ( [name] => corn [quantity] => 3 ), [1] => Array ( [name] => banana [quantity] => 13 ) )
 [blue] => Array ( [0] => Array ( [name] => grape [quantity] => 4 ))
)

Or you can use mysql recordset:

<?
while ($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
  
$firstkey = array_keys($row);
  
$firstkey = $firstkey[0];
  
$key = $row[$firstkey];
   unset(
$row[$firstkey]);
  
$newarray[$key][] = $row;
}
?>
aidan at php dot net
21-May-2004 10:15
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat