Php how to convert an array into a string _ PHP Tutorial

Source: Internet
Author: User
Php converts an array to a string. The problem array is converted into a string. the solution is an array problem. now I get a two-dimensional array variable: $ postanswer $ _ POST [answer {$ showarr [cs_id]}]; replication generation problems
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 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

)



)
Now I want to group their previous IDCs. The group members are separated by 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 );


?? 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?

[]
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 );

?>



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 );

?>



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

)


How to solve the problem of converting the arrays array into strings: an array problem. I now get a two-dimensional array variable: $ postanswer = $ _ POST ["answer {$ showarr ['CS _ id']}"]; copy generation...

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.