suche nach in der

array_count_values> <array_chunk
Last updated: Fri, 18 May 2012

view this page in

array_combine

(PHP 5)

array_combineErzeugt ein Array, indem es ein Array für die Schlüssel und ein anderes für die Werte verwendet

Beschreibung

array array_combine ( array $keys , array $values )

Erzeugt ein Array, in dem die Werte des keys-Arrays als Schlüssel und die Werte aus dem values-Array als die zugehörigen Werte verwendet werden.

Parameter-Liste

keys

Array mit den zu verwendenden Schlüsseln. Für Schlüssel ungültige Werte werden in einen String umgewandelt.

values

Array mit den zu verwendenden Werten

Rückgabewerte

Gibt das kombinierte Array oder FALSE zurück, wenn die Anzahl von Elementen nicht in beiden Arrays identisch ist.

Fehler/Exceptions

Wirft einen Fehler der Stufe E_WARNING, wenn die Anzahl der Elemente in keys und values nicht übereinstimmt.

Changelog

Version Beschreibung
5.4.0 Vorherige Versionen warfen ein E_WARNING Fehler und gaben FALSE zurück, wenn zwei leere Arrays übergeben wurden.

Beispiele

Beispiel #1 Ein einfaches array_combine()-Beispiel

<?php
$a 
= array('gruen''rot''gelb');
$b = array('avokado''apfel''banane');
$c array_combine($a$b);

print_r($c);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Array
(
    [gruen]  => avokado
    [rot]    => apfel
    [gelb]   => banane
)

Siehe auch



add a note add a note User Contributed Notes
array_combine
muppis
21-Jun-2007 02: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 11: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
31-May-2007 09: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 03: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 02: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
10-Apr-2007 05: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 11: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 05: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 10: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 07: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 04: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> <array_chunk
Last updated: Fri, 18 May 2012