"object1", "item2"=>"object2",," /> "object1", "item2"=>"object2",," />

Php array join key value

Without foreach, how can I turn an array like this

array("item1"=>"object1", "item2"=>"object2",......."item-n"=>"object-n");

khổng lồ a string lượt thích this

item1="object1", item2="object2",.... item-n="object-n"

I thought about implode() already, but it doesn"t implode the key with it.

If foreach it necessary, is it possible khổng lồ not nest the foreach?

EDIT: I"ve sầu changed the string

EDIT2/UPDATE: This question was asked quite a while ago. At that time, I wanted to lớn write everything in one line so I would use ternary operators và nest built in function calls in favor of foreach. That was not a good practice! Write code that is readable, whether it is concise or not doesn"t matter that much.

In this case: putting the foreach in a function will be much more readable & modular than writing a one-liner(Even though all the answers are great!).


Bạn đang xem: Php array join key value

php arrays string implode
Share
Improve this question
Follow
edited May 28 "16 at 19:34
tom91136
asked Jul 11 "12 at 7:11
tom91136tom91136
7,735 11 11 gold badges 53 53 silver badges 72 72 bronze badges
5
How nested foreach would be necesarry? – Shubsay đắm Jul 11 "12 at 7:14
What are you attempting? Why bởi you have sầu those constraints? – Madara's Ghost Jul 11 "12 at 7:27
this was my database class for website tiện ích i'm building, i don't want it khổng lồ look messy since it's already populated with a bunch of foreach and for-loop all together – tom91136 Jul 11 "12 at 7:30
1
This selected answer is method that you asked for, however it should be noted that it is critically slower & that if you are storing this information via a database it would be vastly superior to lớn both a loop và this khổng lồ just use json_encode. Exhibit A: willem.stuursma.name/2010/11/22/… – Case Sep 7 "13 at 5:16
1
possible duplicate of Faschạy thử way lớn implode an associative array with keys – Félix Gagnon-Grenier Jul 7 "15 at 18:42
Add a bình luận  | 

11 Answers 11


Active Oldest Votes
194

and another way:

$input = array( "item1" => "object1", "item2" => "object2", "item-n" => "object-n" ); $output = implode(", ", array_map( function ($v, $k) else }, $đầu vào, array_keys($input) ));

or:

$output = implode(", ", array_map( function ($v, $k) , $input, array_keys($input) ));
Share
Improve this answer
Follow
edited Aquảng cáo 27 "16 at 8:28
community wiki
5 revs, 3 users 47%robsch

Xem thêm: Những Cung Hoàng Đạo Xui Xẻo Nhất Năm 2018, Những Cung Hoàng Đạo Nào Xui Xẻo Nhất Năm 2018

4
7
This method is what the tác giả was asking for, but it should be noted that it is critically slower & that if you are storing this information via a database it would be vastly superior to both a loop and this to lớn just use json_encode. Exhibit A: willem.stuursma.name/2010/11/22/… – Case Sep 7 "13 at 5:14
None of the liên kết here seem to lớn work anymore, they all emang lại "Hello, World!"; Should I be seeing code examples of those functions? – Félix Gagnon-Grenier Jul 7 "15 at 18:36
This method is very easy to customize according lớn own demvà. – Kabir Hossain Nov 24 "16 at 4:01
I was using this method to build select options and populate the selected option, and since both arrays must be the same kích thước you can bởi vì something lượt thích this for the second array. array_fill(0, $input đầu vào, 'selected-value-you want-to-check-against'); This will generate same kích cỡ array with single value for all the rows. – krasenslavov Dec 18 "19 at 0:08
Add a phản hồi  | 
200

You could use http_build_query, like this:

"object1", "item2"=>"object2"); emang lại http_build_query($a,"",", "); ?>

Output:

item1=object1, item2=object2

Demo


Share
Improve this answer
Follow
answered Jul 11 "12 at 7:đôi mươi
stewestewe
38.4k 13 13 gold badges 75 75 silver badges 73 73 bronze badges
8
1
is it possible lớn make the object1 into lớn 'object1' – tom91136 Jul 11 "12 at 7:23
7
Never used the other parameters of http_build_query. – Shiplu Mokaddim Jul 11 "12 at 7:38
8
Beware of the string encoding!If you are not building an URL maybe you vì chưng not want it on your array key&value – Matteo Sep 10 "14 at 12:51
9
Matteo You can wrap http_build_query in urldecode to lớn avoid it. – Afterlame Nov 19 "14 at 11:24
3
Genius solution. That was all I needed for a debug output of am assoc array. – dixus Atruyền thông quảng cáo 1 "15 at 13:43
 |  Show 3 more comments
37

I spent measurements (100000 iterations), what fasdemo way lớn glue an associative sầu array?

Objective: To obtain a line of 1,000 items, in this format: "key:value,key2:value2"

We have sầu array (for example):

$array = < "test0" => 344, "test1" => 235, "test2" => 876, ... >;

Test number one:

Use http_build_querystr_replace:

str_replace("=", ":", http_build_query($array, null, ","));

Average time lớn implode 1000 elements: 0.00012930955084904

Test number two:

Use array_map and implode:

implode(",", array_map( function ($v, $k) , $array, array_keys($array) ));

Average time to implode 1000 elements: 0.0004890081976675

Test number three:

Use array_walk & implode:

array_walk($array, function (&$v, $k) ); implode(",", $array);

Average time to lớn implode 1000 elements: 0.0003874126245348

Test number four:

Use foreach:

$str = ""; foreach($array as $key=>$item) rtrim($str, ",");

Average time khổng lồ implode 1000 elements: 0.00026632803902445

I can conclude that the best way to lớn glue the array - use http_build_query and str_replace