Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: array_combine - Manual
[go: Go Back, main page]

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

search for in the

array_count_values" width="11" height="7"/> <array_chunk
Last updated: Sun, 25 Nov 2007

view this page in

array_combine

(PHP 5)

array_combine — 一方の配列をキーとして、もう一方の配列を値として、ひとつの配列を生成する

説明

array array_combine ( array $keys , array $values )

keys 配列の値をキーとして、また values 配列の値を対応する値として生成した 配列 を作成します。

パラメータ

keys

キーとして使用する配列。

values

値として使用する配列。

返り値

作成した配列を返します。 互いの配列の要素の数が合致しない場合や空の配列である場合に FALSE を返します。

エラー / 例外

keys および values のいずれかが空であったり要素数が一致しなかったりした場合は E_WARNING が発生します。

Example#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_count_values" width="11" height="7"/> <array_chunk
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
array_combine
Khaly
04-Oct-2007 06:11
This is the function for PHP4 :

<?php

function array_combine($arr1,$arr2) {
  
$out = array();
   foreach(
$arr1 as $key1 => $value1)    {
   
$out[$value1] = $arr2[$key1];
   }
   return
$out
}

?>
neoyahuu at yahoo dot com
20-Mar-2007 01:36
Some tips for merging same values in an array

<?php
$array1
= array(1,2,3,4,5,6,7,8,9,10,11,12);
$array2 = array(1,2,3,13);

$merged = array_merge($array1,$array2);

// output normal array_merge
echo '<pre>After array_merge :
'
;
print_r($merged);
echo
'</pre>';

// do double flip for merging values in an array
$merged = array_flip($merged);
$merged = array_flip($merged);

// Output after
echo '<pre>After Double Flip :
'
;
print_r($merged);
echo
'</pre>';
?>

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
)
ifeghali at interveritas dot net
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
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_count_values" width="11" height="7"/> <array_chunk
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites