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).
array_combine
(PHP 5)
array_combine — 一方の配列をキーとして、もう一方の配列を値として、ひとつの配列を生成する
説明
array array_combine ( array keys, array values )keys 配列の値をキーとして、また values 配列の値を対応する値として生成した 配列を返します。
互いの配列の要素の数が合致しない場合や空の配列である場合に FALSE を返します。
例 215. 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() も参照ください。
array_combine
MSpreij, c/o GMail
10-Sep-2006 05:47
10-Sep-2006 05:47
glashio.at.gmail.com
07-Dec-2005 07:00
07-Dec-2005 07: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
02-Nov-2005 12:55
02-Nov-2005 12: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 05:04
02-Sep-2005 05: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 03:53
27-Feb-2005 03: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 11:15
21-May-2004 11: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