How does php convert arrays into strings?

Source: Internet
Author: User



Problem
Converting arrays into strings
Solution
For an array question, I get a two-dimensional array variable: $ postanswer =$ _ POST ["answer {$ showarr ['cs _ id']}"];
The printed value of the copied code is Array.

(

[43] => Array

(

[A] =>

[B] => B

)



[42] => Array

(

[C] => C

[D] => D

)



[41] => Array

(

[B] => B

[C] => C

)



[40] => Array

(

[B] => B

[D] => D

)



[39] => Array

(

[A] =>

[D] => D

)



[35] => Array

(

[B] => B

)



)
Copy the code. Now I want to group the previous IDCs and separate the group members with commas. I don't know which hero can help me. Thank you!


Reference answer




$ MyArr = array ();

$ ListArr = array ();

Foreach ($ _ POST ["answer {$ showarr ['cs _ id']}"] as $ key => $ item ){

Foreach ($ item as $ key2 => $ value ){

$ ListArr [] = $ key2. '-'. $ value;

}



$ MyArr [$ key] = join (',', $ listArr );

}

Var_dump ($ myArr );


Copy code ?? So?
Reference answer
Separated by t
Reference answer
Original Post published by yafeikf at [url = http://bbs.111cn.cn/redirect.php? Goto = findpost & pid = 813511 & ptid = 100247] link mark [img] http://bbs.111cn.cn/images/common/back.gif?/img=#/url]


Hey! No! Display: array (5 ){

[47] =>

String (3) "A, B"

[46] =>

String (7) "A, B, B, C"

[45] =>

String (11) "A, B, B, C, A, C"

[44] =>

String (15) "A, B, B, C, A, C, B, D"

[36] =>

String (19) "A, B, B, C, A, C, B, D, C, D"

}
Is it a loop problem to copy the code?

[]
Reference answer
Yes! I have not tested it!

$ Postanswer =$ _ POST ["answer {$ showarr ['cs _ id']}"];

$ New_array = array ();



For ($ postanswer as $ key => $ value ){

If (isset ($ new_array [$ key]) {

$ New_array [$ key]. = ','. $ value;

} Else {

$ New_array [$ key] = $ value;

}

}



Print_r ($ new_array );

?>


Copy code
Reference answer
Let's change your mind.
Not to mention how to implement it, but first think about your intention.

This will cause problems. What if there is "," in your array?
How can you restore it?

Get rid of this idea and use serialize and unserialize.
Reference answer
Original Post published by liexusong at, [url = http://bbs.111cn.cn/redirect.php? Goto = findpost & pid = 813732 & ptid = 100247] link mark [img] http://bbs.111cn.cn/images/common/back.gif?/img=#/url]
Yes! I have not tested it!



Haha... No! By the way, an error occurs. Should it be foreach rather than for loop?
Reference answer
Original Post published by hubinhust at [url = http://bbs.111cn.cn/redirect.php? Goto = findpost & pid = 813834 & ptid = 100247] link mark [img] http://bbs.111cn.cn/images/common/back.gif?/img=#/url]
Let's change your mind.
Not to mention how to implement it, but first think about your intention.

This will cause problems. What if there is "," in your array?
How can you restore it?

Get rid of this idea and use serialize and unserialize.
This should not appear, at least here! Because I submitted the test answer ABCD.
Reference answer
Oh ~ I saw it wrong! It should be like this!

$ Postanswer =$ _ POST ["answer {$ showarr ['cs _ id']}"];

$ New_array = array ();



Foreach ($ postanswer as $ element ){

Foreach ($ element as $ key => $ value ){

If (isset ($ new_array [$ key]) {

$ New_array [$ key]. = ','. $ value;

} Else {

$ New_array [$ key] = $ value;

}

}

}



Print_r ($ new_array );

?>


Copy code
Reference answer
Original Post published by liexusong at 2009-1-3 20:10 [url = http://bbs.111cn.cn/redirect.php? Goto = findpost & pid = 813889 & ptid = 100247] link mark [img] http://bbs.111cn.cn/images/common/back.gif?/img=#/url]
Oh ~ I saw it wrong! It should be like this!



We can't do it this time, but we can output this: Array

(

[A] => A,

[D] => D, D, D

[B] => B, B

[C] => C

)
Copy code

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.