search for in the  
<array_chunkarray_count_values>
Last updated: Thu, 19 May 2005

array_combine

(PHP 5)

array_combine --  Creates an array by using one array for keys and another for its values

Description

array array_combine ( array keys, array values )

Returns an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

Returns FALSE if the number of elements for each array isn't equal or if the arrays are empty.

Example 1. A simple array_combine() example

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

print_r($c);
?>

The above example will output:

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

See also array_merge(), array_walk(), and array_values().



User Contributed Notes
array_combine
ungu at terong dot com
10-May-2005 01:52
Sorry for the mistype in previous submission, here is the correct one completed with a sample.
Simpler version of array_combine if you do not use PHP 5

<?
if(!function_exists('array_combine')) function array_combine($a, $b) {
  
$c = array();
  
$at = array_values($a);
  
$bt = array_values($b);
   foreach(
$at as $key=>$aval) $c[$aval] = $bt[$key];
   return
$c;
}

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

print_r($c);
?>
ungu at terong dot com
10-May-2005 01:39
Simpler version of array_combine if you do not use PHP 5

<?
if (!function_exists('array_combine')) {
   function
array_combine($a, $b) {
      
$c = array();
      
$at = array_values($a);
      
$bt = array_values($b);
       foreach(
$at as $key=>$aval) $c[$aval] = $b[$key];
       return
$c;
   }
}
?>
dreptack at op dot pl
22-Apr-2005 06:15
For those of you want to use this function but have php version < 5.0
<?php
if (!function_exists('array_combine')) {
  
//{{{ array_combine
   /**
   * Creates an array by using one array for keys and another for its values
   * @param    array    $keys
   * @param    array    $values
   * @return    Returns an array by using the values from the keys array as keys and the values from the values array as the corresponding values.<br />Returns FALSE if the number of elements for each array isn't equal or if the arrays are empty.
   * @author    <dreptack [at] op [dot] pl>
   */
  
function array_combine($keys, $values)
   {
       if (!
is_array($keys) || !is_array($values) || !$keys || !$values || count($keys) != count($values)) {
           return
false;
       }
      
reset($keys);
      
reset($values);
      
$retarr = array();
       while ((
$keyarr = each($keys)) && ($valarr = each($values))) {
          
$retarr[$keyarr['value']] = $valarr['value'];
       }
       return
$retarr;
   }
//}}}
} //endif function_exists
?>
Enjoy:-)
ifeghali at interveritas dot net
26-Feb-2005 12: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
20-May-2004 09: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

<array_chunkarray_count_values>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT