Home  >  Article  >  Backend Development  >  Function to convert multi-dimensional array into one-dimensional array using recursion_PHP tutorial

Function to convert multi-dimensional array into one-dimensional array using recursion_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:01:261042browse

Function name: array_multi2single
Function prototype: array array_multi2single(array)
Implementation function: Store the values ​​of a multi-dimensional array into a one-dimensional array without saving the Key.


function array_multi2single($array)
{
static $result_array=array();
foreach($array as $value)
{
if(is_array($value))
{
array_multi2single($value);
}
else
$result_array[]=$value;
}
Return $result_array;
}


//Function test part
$array=array("1"=>array("A","B","C ",array("D","E")),"2"=>array("F","G","H","I"));
$array=array_multi2single($array );
echo "

Test result:

";
foreach($array as $value)
{
echo "
$value echo "
";

}

?>

Welcome everyone to criticize and correct!

Author Email: fancao0515@0451.com


[The copyright of this article is jointly owned by the author and Aosuo.com. If you need to reprint, please indicate the author and source]

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316900.htmlTechArticleFunction name: array_multi2single Function prototype: array array_multi2single(array) Implementation function: Store the value of a multi-dimensional array into a In a dimensional array, the Key is not saved. ?php functio...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn