Home  >  Article  >  Backend Development  >  How to convert an array into a string in php_PHP tutorial

How to convert an array into a string in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:51:03864browse



Question
Problem converting array to string
Solution
Asking an array question, I now get a two-dimensional array variable: $postanswer=$_POST["answer{$showarr['cs_id']}"];
The value printed by copying the code is: Array

(

[43] => Array

(

[A] => A

[B] => B

)



[42] => Array

(

[C] => C

[D] => D

)



[41] => Array

(

[B] => B

[C] => C

)



[40] => Array

(

[B] => B

[D] => D

)



[39] => Array

(

[A] => A

[D] => D

)



[35] => Array

(

[B] => B

)



)
Copy the code. Now I want to group them with the previous ID number, and the members in the group are separated by ",". I don’t know if any heroes can help me, I would be very grateful!


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 the code?? like this?
Reference answer
Separate with t
Reference answer
Original post by yafeikf on 2009-1-3 14:28 [url=http://bbs.111cn.cn/redirect.php?goto=findpost&pid=813511&ptid=100247]Link tag [img]http://bbs. 111cn.cn/images/common/back.gif[/img][/url]


hey-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 there a loop problem in copying the code?

[ ]
Reference answer
It should work! I haven’t 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 think differently.
Let’s not talk about how to achieve it, first think about your intention.

There will be problems with this. What if there are "," in your array?
How can you restore it?

Throw away this idea and use serialize and unserialize instead.
Reference answer
Original post by liexusong on 2009-1-3 16:47 [url=http://bbs.111cn.cn/redirect.php?goto=findpost&pid=813732&ptid=100247]Link tag [img]http://bbs. 111cn.cn/images/common/back.gif[/img][/url]
It should work! I haven’t tested it!



Haha... no! By the way, to pick a mistake, the loop should be foreach instead of for, right?
Reference answer
The original post was published by hubinhust on 2009-1-3 19:13 [url=http://bbs.111cn.cn/redirect.php?goto=findpost&pid=813834&ptid=100247]Link tag [img]http://bbs. 111cn.cn/images/common/back.gif[/img][/url]
Let’s think differently.
Let’s not talk about how to achieve it, first think about your intention.

There will be problems with this. What if there are "," in your array?
How can you restore it?

Throw away this idea and use serialize and unserialize instead.
This shouldn't happen, at least here! Because I submitted the answer ABCD for the test.
Reference answer
Oh~ turns out I was 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 by liexusong on 2009-1-3 20:10 [url=http://bbs.111cn.cn/redirect.php?goto=findpost&pid=813889&ptid=100247]Link tag [img]http://bbs. 111cn.cn/images/common/back.gif[/img][/url]
Oh ~ turns out I was wrong! It should be like this!



Bamboo, it didn’t work this time, but I actually output this: Array

(

[A] => A,A

[D] => D,D,D

[B] => B,B

[C] => C

)
Copy code

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632598.htmlTechArticleProblem Convert array into string Problem solution Ask an array question, I now get a two-dimensional array variable : $postanswer=$_POST["answer{$showarr['cs_id']}"]; Copy code...
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