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: Thu, 31 May 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 が発生します。

例 231. 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_count_values" width="11" height="7"/> <array_chunk
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
array_combine
Hayley Watson
21-Aug-2007 02:17
Fred's code just previous isn't the simplest, given...
<?php
function array_combine($keys, $values)
{
   
$result = array();
    foreach(
array_map(null, $keys, $values) as $pair)
       
$result[$pair[0]]=$pair[1];
    return
$result;
}
?>
...if you're equally happy to avoid any sort of error checking of course.
Fred
09-Aug-2007 07:49
I read many workaround functions for users with php < 5
Here is mine (the simpliest I think) :

<?php
function array_combine($keys, $values) {
   
$result = array() ;
    while( (
$k=each($keys)) && ($v=each($values)) ) $result[$k[1]] = $v[1] ;
    return
$result ;
}
?>
royanee at yahoo dot com
07-Aug-2007 02:11
This correctly accounts for the error conditions, and actually works. The previous note appears to set all the values in the result array to the value of the last item in the values input array.
<?php
function array_combine( $keys, $values )
{
   if( !
is_array($keys) || !is_array($values) || empty($keys) || empty($values) || count($keys) != count($values) )
   {
     
trigger_error( "array_combine() expects parameters 1 and 2 to be non-empty arrays with an equal number of elements", E_USER_WARNING );
      return
false;
   }
  
$keys = array_values($keys);
  
$values = array_values($values);
  
$result = array();
   foreach(
$keys as $index => $key )
   {
     
$result[$key] = $values[$index];
   }
   return
$result;
}
?>
robertark at gmail dot com
02-Aug-2007 08:43
All of those 'array_combine' emulators did -not- work for me, so I created one of my own, out randomly, and it worked for me, maybe you can give it a shot and let me know how it works. Thanks..

function array_combine($arr1, $arr2)
{
  $buffer = array();
  foreach($arr1 as $key1 => $arr_1)
  {
    foreach($arr2 as $key2 => $arr_2)
      $buffer[$arr_1] = $arr_2;
  }
  return $buffer;
}
muppis
21-Jun-2007 09:46
I found these workarounds for PHP 4.4.x non-working with $_POST and $_GET arrays except Ivo van Sandick's one. Works well, even my client ISP denies to update to 4.4.7 at least..
toth dot janas at gmail dot com
01-Jun-2007 06:21
Under this note i send yesterday a function that in array show once the dupicated elements. Sorry, the function is wrong. I corrected the error and i write a new function is working right.
/tested/.

The test array is:

$test = array(
    0 => 1,
    1 => 2,
    2 => 1,
    3 => 3,
    4 => 4,
    5 => 2,
    6 => 5,
    7 => 4
);

the function:

function array_no_duplicate($array_tmb){
    $temp = array();
     foreach ($array_tmb as $value){
     $if_one = 0;
         foreach ($temp as $new_value) {
            if($new_value == $value)  $if_one = 1;
          }
     if($if_one == 0)  array_push($temp, $value);    
    }
    return $temp;
}

example:
var_dump(array_no_duplicate($test));

this output will show array without duplicated elements.

Good work!
toth dot janas at gmail dot com
01-Jun-2007 04:48
If you have an array and include duplicated element, and you want to that this element just show once in array, then this function is very good solution.

function array_no_duplicate($array_tmb){
    $new_array = array();
    for($i = 0; $i < count($array_tmb); $i++){
    $van = 0;
        for($j = 0; $j < count($array_tmb); $j++){
         if( $new_array[$j] == $array_tmb[$j]) $van = 1;
        }   
    if($van == 0){
         $new_array[$i] = $array_tmb[$i];
    }else continue;        
    }
    return $new_array;
}
Q1712 at online dot ms
23-Apr-2007 10:04
there was one exaple missing in my priviouse post:
<?php
print_r
( array(array()), array("array()") );
?>
prints:
Array
(
    [Array] = array()
)

@admin: please merge these 2 posts
Q1712 at online dot ms
23-Apr-2007 09:56
If a value is existing twice in the $keys-array the last one overwrites the others, wich makes the returned array shorter than the given once, as usual.
If a value of the values-array is double/float, bool or array the string representaion is used, not the integer representation as usual.

<?php
print_r
(
 
combine(
    array(
'a', 'a', 'a', 0, false, 1, true, 2, 7/3),
    array(
"first 'a'", "second 'a'", "third 'a'", "0", "false", "1", "true", "2", "7/3")
  )
);

print_r(
  array (
'a' => "first 'a'", 'a' => "second 'a'", 'a' => "third 'a'", 0 => "0", false => "false", 1 => "1", true => "true", 2 => "2", 7/3 => "7/3")
);
?>

prints:
Array
(
    [a] => third 'a'
    [0] => 0
    [] => false
    [1] => true
    [2] => 2
    [2.33333333333] => 7/3
)
Array
(
    [a] => third 'a'
    [0] => false
    [1] => true
    [2] => 7/3
)
chriswillarddesign at yahoo dot com
11-Apr-2007 12:26
Here is my take on the combine function for PHP < 5:

<?
// Combines two associate arrays by making a array with the key being $a1 and the value $a2.
   
function array_combine($a1,$a2)
    {
        for(
$i=0;$i<count($a1);$i++)
           
$ra[$a1[$i]] = $a2[$i];
        if(isset(
$ra)) return $ra; else return false;
    }

?>
Wiebe
26-Mar-2007 06:05
When you have not the use off PHP 5.

The workaround off  'Ivo van Sandick' can be much shorter.
I use my function:

<?
// combine $arr with $arr_to_key where $arr_to_key becomes key of $arr.

 
function array_comb($arr, $arr_to_key){

         
$i = 0;
          foreach(
$arr_to_key AS $value){
                 
$arr_combined[$value] = $arr[$i];
                 
$i++;
          }

          RETURN
$arr_combined;
 }

 
//example.
 
$arr_1[0] = 'apple';
 
$arr_1[1] = 'banana';
 
$arr_2[0] = 'green';
 
$arr_2[1] = 'yellow';

 
$arr_comb = array_comb($arr_1, $arr_2);

 
//$arr_comb['green'] = 'apple'
 //$arr_comb['yellow'] = 'banana'

?>
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
)
Ivo van Sandick
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
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: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites