How php converts a two-dimensional joined array into a string
This example describes how php converts a two-dimensional joined array to a string. We will share this with you for your reference. The details are as follows:
Requirement
The project encountered the problem of converting two-dimensional joined arrays to strings. For more information, read the relevant materials, write the following program, and filter duplicate keywords.
For example, the two-dimensional array of php is as follows:
$name = array( "self" => "wangzhengyi", "student" => array( "chenshan", "xiaolingang" ), "unkmow" => "chaikun", "teacher" => array( "huangwei", "fanwenqing" ));
The final string format to be obtained is:
wangzhengyi,chenshan,xiaolingang,chaikun,huangwei,fanwenqing
Ideas
Method
Use static keywords and recursive ideas to traverse Arrays
Custom Function Code (php)
Function arrToStr ($ array) {// defines the array static $ r_arr = array () that stores all strings; if (is_array ($ array )) {foreach ($ array as $ key = >$ value) {if (is_array ($ value) {// recursively traverse arrToStr ($ value );} else {$ r_arr [] = $ value ;}} else if (is_string ($ array) {$ r_arr [] = $ array ;} // array deduplication $ r_arr = array_unique ($ r_arr); $ string = implode (",", $ r_arr); return $ string ;}
Complete sample code:
<? Php $ name = array ("self" => "wangzhengyi", "student" => array ("chenshan", "xiaolingang"), "unkmow" => "chaikun ", "teacher" => array ("huangwei", "fanwenqing"); function arrToStr ($ array) {// defines the array static $ r_arr = array () that stores all strings (); if (is_array ($ array) {foreach ($ array as $ key => $ value) {if (is_array ($ value )) {// recursively traverse arrToStr ($ value);} else {$ r_arr [] = $ value ;}} else if (is_string ($ arra Y) {$ r_arr [] = $ array;} // array deduplication $ r_arr = array_unique ($ r_arr); $ string = implode (",", $ r_arr ); return $ string;} echo arrToStr ($ name);?>
Running result:
wangzhengyi,chenshan,xiaolingang,chaikun,huangwei,fanwenqing