PHP: array_combine - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

array_count_values array_chunk

Last updated: Sat, 27 Jan 2007

view this page in

# 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 User Contributed Notes
array_combine
m at momech dot dk
01-Nov-2005 07: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 01: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
26-Feb-2005 10: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 07: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`
add a note
array_count_values array_chunk

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST