How php converts a two-dimensional joined array into a string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.