Skip to content

Instantly share code, notes, and snippets.

@lsauer
Created September 6, 2011 07:35
Show Gist options
  • Save lsauer/1196863 to your computer and use it in GitHub Desktop.
Save lsauer/1196863 to your computer and use it in GitHub Desktop.
PHP: Array_merge by value, or array merge by values and keys
/**
L. Sauer,2011
license: public domain
http://xhr2.blogspot.com/2011/09/php-arraymerge-by-value-or-array-merge.html
*/
function array_merge_values_explode(){
$args = func_get_args();
$elem = array();
$delim = strlen(end($args))<=2 ? array_pop($args) : ',';
for($i=0; $i<count($args); $i++){
$elem += array_flip(explode($delim, $args[$i]));
}
//sort and reorder keys
$elem = array_combine( range( 0,(-1+count($elem))), array_keys($elem) ); //combine( arr $keys, arr $values)
return $elem;
}
function array_merge_values($arr1, $arr2){
$merge = array_merge(
array_flip($arr1),
array_flip($arr2)
);
return
array_combine(
range( 0,count($merge)-1, array_keys($merge) )
); //array_flip would eliminate same-valued(->indexed) pairs
}
//Example
$httpvar_loadwsdl = "kegg,pathexplorer,metabol";
defined('RPC_DEFAULT_CACHED_SERVICE')
|| define('RPC_DEFAULT_CACHED_SERVICE', 'pubchem,kegg');
//notice the defaults are intentionally rightmost as they take precedence
print_r(
array_merge_values_explode($httpvar_loadwsdl, RPC_DEFAULT_CACHED_SERVICE)
);
Array
(
[0] => kegg
[1] => pathexplorer
[2] => metabol
[3] => pubchem
)
/**
L. Sauer,2011
license: public domain
http://xhr2.blogspot.com/2011/09/php-arraymerge-by-value-or-array-merge.html
*/
function array_merge_values_explode(){
$args = func_get_args();
$elem = array();
$delim = strlen(end($args))<=2 ? array_pop($args) : ',';
for($i=0; $i<count($args); $i++){
$elem += array_flip(explode($delim, $args[$i]));
}
//sort and reorder keys
$elem = array_combine( range( 0,(-1+count($elem))), array_keys($elem) ); //combine( arr $keys, arr $values)
return $elem;
}
function array_merge_values($arr1, $arr2){
$merge = array_merge(
array_flip($arr1),
array_flip($arr2)
);
return
array_combine(
range( 0,count($merge)-1, array_keys($merge) )
); //array_flip would eliminate same-valued(->indexed) pairs
}
//Example
$httpvar_loadwsdl = "kegg,pathexplorer,metabol";
defined('RPC_DEFAULT_CACHED_SERVICE')
|| define('RPC_DEFAULT_CACHED_SERVICE', 'pubchem,kegg');
//notice the defaults are intentionally rightmost as they take precedence
print_r(
array_merge_values_explode($httpvar_loadwsdl, RPC_DEFAULT_CACHED_SERVICE)
);
Array
(
[0] => kegg
[1] => pathexplorer
[2] => metabol
[3] => pubchem
)
/**
L. Sauer,2011
license: public domain
http://xhr2.blogspot.com/2011/09/php-arraymerge-by-value-or-array-merge.html
*/
function array_merge_values_explode(){
$args = func_get_args();
$elem = array();
$delim = strlen(end($args))<=2 ? array_pop($args) : ',';
for($i=0; $i<count($args); $i++){
$elem += array_flip(explode($delim, $args[$i]));
}
//sort and reorder keys
$elem = array_combine( range( 0,(-1+count($elem))), array_keys($elem) ); //combine( arr $keys, arr $values)
return $elem;
}
function array_merge_values($arr1, $arr2){
$merge = array_merge(
array_flip($arr1),
array_flip($arr2)
);
return
array_combine(
range( 0,count($merge)-1, array_keys($merge) )
); //array_flip would eliminate same-valued(->indexed) pairs
}
//Example
$httpvar_loadwsdl = "kegg,pathexplorer,metabol";
defined('RPC_DEFAULT_CACHED_SERVICE')
|| define('RPC_DEFAULT_CACHED_SERVICE', 'pubchem,kegg');
//notice the defaults are intentionally rightmost as they take precedence
print_r(
array_merge_values_explode($httpvar_loadwsdl, RPC_DEFAULT_CACHED_SERVICE)
);
Array
(
[0] => kegg
[1] => pathexplorer
[2] => metabol
[3] => pubchem
)
@lsauer
Copy link
Author

lsauer commented Sep 6, 2011

The union Array operator could be used as well, instead of array_merge: $Array1 + $Array2 + $Array3
A difference is, that array_merge is more convenient for implementing a well-performing generic function with an undefined number of parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment