array_combineCREATES a new ARRAY by combining two others, where one will be used as an index and the other as elements.
This function creates an ARRAY by using the values from the $keys array as keys and the $values from the values array as the corresponding values.
Ilegal values of $keys will be converted to STRING.
If the number of elements of $keys and $values does not match an E_WARNING will be issued and FALSE is returned.
<?php
arr array_combine ( arr $keys , arr $values )
where,
$keys = The input ARRAY used as keys
$values = The input ARRAY used as values
?>
$keys
The input ARRAY used as keys.
$values
The input ARRAY used as values.
EXERCISE
<?php
$keys01 = [ 'R', 'G', 'B', 'Y', 'C', 'M', 'K' ];
$vals01 = [ 'red', 'green', 'blue', 'yellow',
'cyan', 'magenta', 'black' ];
$arr01 = array_combine($keys01, $vals01);
echo '<pre>';
var_export($keys01);
echo '<br><br>';
var_export($vals01);
echo '<br><br>';
var_export($arr01);
echo '</pre>';
?>
RESULT
ARRAY - keys
array (
0 => 'R',
1 => 'G',
2 => 'B',
3 => 'Y',
4 => 'C',
5 => 'M',
6 => 'K',
)
ARRAY - values
array (
0 => 'red',
1 => 'green',
2 => 'blue',
3 => 'yellow',
4 => 'cyan',
5 => 'magenta',
6 => 'black',
)
ARRAY - combined
array (
'R' => 'red',
'G' => 'green',
'B' => 'blue',
'Y' => 'yellow',
'C' => 'cyan',
'M' => 'magenta',
'K' => 'black',
)
EXERCISE
<?php
$keys02 = [ 'countries', 'continents' ];
$vals02 = [ [ 'Brasil', 'Portugal', 'Japan' ] ,
[ 'South America', 'Europe', 'Asia' ] ];
$arr02 = array_combine($keys02, $vals02);
echo '<pre>';
var_export($keys02);
echo '<br><br>';
var_export($vals02);
echo '<br><br>';
var_export($arr02);
echo '</pre>';
?>
RESULT
ARRAY - keys
array (
0 => 'countries',
1 => 'continents',
)
ARRAY - values
array (
0 =>
array (
0 => 'Brasil',
1 => 'Portugal',
2 => 'Japan',
),
1 =>
array (
0 => 'South America',
1 => 'Europe',
2 => 'Asia',
),
)
ARRAY - combined
array (
'countries' =>
array (
0 => 'Brasil',
1 => 'Portugal',
2 => 'Japan',
),
'continents' =>
array (
0 => 'South America',
1 => 'Europe',
2 => 'Asia',
),
)
EXERCISE
<?php
$keys03 = [ 'K', 'L', 'M', 'N', 'O', 'P', 'Q' ];
$vals03 = [ 2, 8, 18, 32, 32, 18 ];
$arr03 = array_combine($keys03, $vals03);
echo '<pre>';
var_export($keys03);
echo '<br><br>';
var_export($vals03);
echo '<br><br>';
var_export($arr03);
echo '</pre>';
?>
RESULT
In PHP 8.0.XX
Fatal error: Is emitted ...
... the system error which causes processing interruption.
In PHP 7.4.XX
Warning: Is emitted ...
ARRAY - keys
array (
0 => 'K',
1 => 'L',
2 => 'M',
3 => 'N',
4 => 'O',
5 => 'P',
6 => 'Q',
)
ARRAY - values
array (
0 => 2,
1 => 8,
2 => 18,
3 => 32,
4 => 32,
5 => 18,
)
ARRAY - combined
false
Because the number of elements in keys and values does not match.
EXERCISE
<?php
$keys04 = [ ];
$vals04 = [ ];
$arr04 = array_combine($keys04, $vals04);
echo '<pre>';
var_export($keys04);
echo '<br><br>';
var_export($vals04);
echo '<br><br>';
var_export($arr04);
echo '</pre>';
?>
RESULT
ARRAY - keys
array (
)
ARRAY - values
array (
)
ARRAY - combined
array (
)
The ARRAYS: key and values are empty.
EXERCISE
<?php
$arrk05 = [ 1, 2, 3, 4, 5, 6, 7 ];
$arrv05 = [ "Doc", "Gumpy",
"Happy", "Sleepy", "Bashful", "Sneezy", "Dopey" ];
echo 'ARRAY - keys<br><pre>';
var_dump($arrk05);
echo '</pre><br><br>ARRAY - values<br><pre>';
var_dump($arrv05);
echo '</pre>';
$arrc05 = array_combine($arrk05, $arrv05);
echo '<br><br>ARRAY - combined<br>';
foreach($arrc05 as $k05 => $v05)
echo $k05 . ' => ' . $v05 . '<br>';
?>
RESULT
ARRAY - keys
array(7) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(6)
[6]=>
int(7)
}
ARRAY - values
array(7) {
[0]=>
string(3) "Doc"
[1]=>
string(5) "Gumpy"
[2]=>
string(5) "Happy"
[3]=>
string(6) "Sleepy"
[4]=>
string(7) "Bashful"
[5]=>
string(6) "Sneezy"
[6]=>
string(5) "Dopey"
}
ARRAY - combined
1 => Doc
2 => Gumpy
3 => Happy
4 => Sleepy
5 => Bashful
6 => Sneezy
7 => Dopey
EXERCISE
<?php
$array1 = array('green', 'red', 'yellow');
$array2 = array('1', '2', '3');
$array3 = array(0, 1, 2);
$array4 = array(TRUE, FALSE, NULL);
$a = array_combine($array1, $array1);
$b = array_combine($array1, $array2);
$c = array_combine($array1, $array3);
$d = array_combine($array1, $array4);
$e = array_combine($array2, $array1);
$f = array_combine($array2, $array2);
$g = array_combine($array2, $array3);
$h = array_combine($array2, $array4);
$i = array_combine($array3, $array1);
$j = array_combine($array3, $array2);
$k = array_combine($array3, $array3);
$l = array_combine($array3, $array4);
$m = array_combine($array4, $array1);
$n = array_combine($array4, $array2);
$o = array_combine($array4, $array3);
$p = array_combine($array4, $array4);
for($letter = "a"; $letter <= "p"; $letter++)
{
echo '<pre>';
print_r($$letter);
echo '</pre>';
}
?>
EXERCISE
<?php
echo "Testing basic functionality:<br><br>";
/* Different arrays for $keys
and $values arguments */
// array with default keys for $keys
// and $values arguments
$keys_array = array(1, 2);
$values_array = array(3,4);
echo 'The given ARRAYS:<br>';
print_r($keys_array);
echo '<br>';
print_r($values_array);
echo '<br><br>The combined ARRAY:<br>';
var_dump( array_combine($keys_array, $values_array) );
echo '<br><br>';
// associative arrays for $keys
// and $values arguments
$keys_array = array(1 => "a", 2 => 'b');
$values_array = array(3 => 'c', 4 => "d");
echo '<br>The given ARRAYS:<br>';
print_r($keys_array);
echo '<br>';
print_r($values_array);
echo '<br><br>The combined ARRAY:<br>';
var_dump( array_combine($keys_array, $values_array) );
echo '<br><br>';
// mixed array for $keys
// and $values arguments
$keys_array = array(1, 2 => "b");
$values_array = array(3 => 'c', 4);
echo '<br>The given ARRAYS:<br>';
print_r($keys_array);
echo '<br>';
print_r($values_array);
echo '<br><br>The combined ARRAY:<br>';
var_dump( array_combine($keys_array, $values_array) );
echo '<br><br>';
?>
EXERCISE
<?php
/*
* Passing different types of arrays
* to both $keys and $values arguments and testing whether
* array_combine() behaves in an expected
* way with the arguments passed to the function
*/
echo "Passing different types of arrays to
both \$keys and \$values argument:";
/* Different heredoc strings
passed as argument to arrays */
// heredoc with blank line
$blank_line = <<<EOT
EOT;
// heredoc with multiline string
$multiline_string = <<<EOT
hello world
The quick brown fox jumped over;
the lazy dog
This is a double quoted string
EOT;
// heredoc with different whitespaces
$diff_whitespaces = <<<EOT
hello\r world\t
1111\t\t != 2222\v\v
heredoc\ndouble quoted string.
with\vdifferent\fwhite\vspaces
EOT;
// heredoc with quoted strings and numeric values
$numeric_string = <<<EOT
11 < 12. 123 >22
'single quoted string'
"double quoted string"
2222 != 1111.\t 0000 = 0000\n
EOT;
// arrays passed to $keys argument
$arrays = array (
/*1*/ array(1, 2),
// with default keys and numeric values
array(1.1, 2.2),
// with default keys & float values
array(false,true),
// with default keys and boolean values
array(),
// empty array
/*5*/ array(NULL),
// with NULL
array("a\v\f","aaaa\r","b","b\tbbb","c","\[\]\!\@\#\$\%\^\&\*\(\)\{\}"),
// with double quoted strings
array('a\v\f','aaaa\r','b','b\tbbb','c','\[\]\!\@\#\$\%\^\&\*\(\)\{\}'),
// with single quoted strings
array("h1" => $blank_line, "h2" => $multiline_string,
"h3" => $diff_whitespaces, $numeric_string),
// with heredocs
// associative arrays
/*9*/ array(1 => "one", 2 => "two", 3 => "three"),
// explicit numeric keys, string values
array("one" => 1, "two" => 2, "three" => 3 ),
// string keys & numeric values
array( 1 => 10, 2 => 20, 4 => 40, 3 => 30),
// explicit numeric keys and numeric values
array( "one" => "ten", "two" => "twenty", "three" => "thirty"),
// string key/value
array("one" => 1, 2 => "two", 4 => "four"),
//mixed
// associative array, containing
// null/empty/boolean values as key/value
/*14*/ array(NULL => "NULL", null => "null",
"NULL" => NULL, "null" => null),
array(true => "true", false => "false",
"false" => false, "true" => true),
array("" => "emptyd", '' => 'emptys',
"emptyd" => "", 'emptys' => ''),
array(1 => '', 2 => "", 3 => NULL, 4 => null,
5 => false, 6 => true),
array('' => 1, "" => 2, NULL => 3,
null => 4, false => 5, true => 6),
// array with repetitive keys
/*19*/ array("One" => 1, "two" => 2, "One" => 10,
"two" => 20, "three" => 3)
);
// loop through each sub-array within
// $arrays to check the behavior of array_combine()
// same arrays are passed to both $keys and $values
$iterator = 1;
foreach($arrays as $array) {
echo "<br><br>Iteration: $iterator<br>";
var_dump( array_combine($array, $array) );
$iterator++;
}
?>
EXERCISE
<?php
/*
* Testing the behavior of array_combine()
* by passing array with
* binary values for $keys and $values argument.
*/
echo "Testing binary safe checking:<br><br>";
// array with binary values
$arr_binary = array(b"hello", b"world");
$arr_normal = array("hello", "world");
echo 'The given ARRAYS:<br>';
var_dump($arr_binary);
echo '<br><br>';
var_dump($arr_normal);
echo '<br><br><br>';
// array with binary value
// for $keys and $values argument
var_dump( array_combine($arr_binary, $arr_binary) );
echo '<br><br>';
// array with binary value
// for $values argument
var_dump( array_combine($arr_normal, $arr_binary) );
echo '<br><br>';
// array with binary value
// for $keys argument
var_dump( array_combine($arr_binary, $arr_normal) );
?>