implode

How to implode subarrays in a 2-dimensional array?

ぃ、小莉子 提交于 2019-11-27 07:27:32
问题 I want to implode values in to a comma-separated string if they are an array: I have the following array: $my_array = [ "keywords" => "test", "locationId" => [ 0 => "1", 1 => "2"], "industries" => "1" ]; To achieve this I have the following code: foreach ($my_array as &$value) is_array($value) ? $value = implode(",", $value) : $value; unset($value); The above will also change the original array. Is there a more elegant way to create a new array that does the same as the above? I mean, implode

Replace the last comma with an & sign

浪子不回头ぞ 提交于 2019-11-27 02:56:24
问题 I have searched everywhere but can't find a solution that works for me. I have the following: $bedroom_array = array($studio, $one_bed, $two_bed, $three_bed, $four_bed); For this example lets say: $studio = '1'; $one_bed = '3'; $two_bed = '3'; I then use the implode function to put a comma in between all the values: $bedroom_list = implode(", ", array_filter($bedroom_array)); echo $bedroom_list; This then outputs: 1, 2, 3 What I want to do is find the last comma in the string and replace it

How to implode array with key and value without foreach in PHP

£可爱£侵袭症+ 提交于 2019-11-26 18:54:22
问题 Without foreach , how can I turn an array like this array("item1"=>"object1", "item2"=>"object2",......."item-n"=>"object-n"); to a string like 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 to not nest the foreach? EDIT: I've changed the string EDIT2/UPDATE: This question was asked quite a while ago. At that time, I wanted to write everything in one line so I would

How to implode a vector of strings into a string (the elegant way)

橙三吉。 提交于 2019-11-26 17:32:56
I'm looking for the most elegant way to implode a vector of strings into a string. Below is the solution I'm using now: static std::string& implode(const std::vector<std::string>& elems, char delim, std::string& s) { for (std::vector<std::string>::const_iterator ii = elems.begin(); ii != elems.end(); ++ii) { s += (*ii); if ( ii + 1 != elems.end() ) { s += delim; } } return s; } static std::string implode(const std::vector<std::string>& elems, char delim) { std::string s; return implode(elems, delim, s); } Is there any others out there? Andre Holzner Use boost::algorithm::join(..) : #include

Add a prefix to each item of a PHP array

大城市里の小女人 提交于 2019-11-26 17:23:29
I have a PHP array of numbers, which I would like to prefix with a minus (-). I think through the use of explode and implode it would be possible but my knowledge of php is not possible to actually do it. Any help would be appreciated. Essentially I would like to go from this: $array = [1, 2, 3, 4, 5]; to this: $array = [-1, -2, -3, -4, -5]; Any ideas? Rohit Chopra Simple: foreach ($array as &$value) { $value *= (-1); } unset($value); Unless the array is a string: foreach ($array as &$value) { $value = '-' . $value; } unset($value); An elegant way to prefix array values (PHP 5.3+): $prefixed

php implode (101) with quotes

爱⌒轻易说出口 提交于 2019-11-26 17:18:55
Imploding a simple array would look like this $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); and that would return this lastname,email,phone great, so i might do this instead $array = array('lastname', 'email', 'phone'); $comma_separated = implode("','", $array); $comma_separated = "'".$comma_separated."'"; and now i have what I want a nice pretty csv string 'lastname','email','phone' is there a better way to do this, it feels to me like there should be an optional parameter for implode am I missing something ? No, the way that you're doing it is just

Multidimensional Array PHP Implode

独自空忆成欢 提交于 2019-11-26 15:47:40
问题 In terms of my data structure, I have an array of communications, with each communications_id itself containing three pieces of information: id, score, and content. I want to implode this array in order to get a comma separated list of ids, how do I do this? 回答1: Update for PHP 5.5 PHP 5.5 introduces array_column which is a convenient shortcut to a whole class of array_map usage; it is also applicable here. $ids = array_column($communications, 'id'); $output = implode(',', $ids); Original

How to implode a vector of strings into a string (the elegant way)

依然范特西╮ 提交于 2019-11-26 06:03:36
问题 I\'m looking for the most elegant way to implode a vector of strings into a string. Below is the solution I\'m using now: static std::string& implode(const std::vector<std::string>& elems, char delim, std::string& s) { for (std::vector<std::string>::const_iterator ii = elems.begin(); ii != elems.end(); ++ii) { s += (*ii); if ( ii + 1 != elems.end() ) { s += delim; } } return s; } static std::string implode(const std::vector<std::string>& elems, char delim) { std::string s; return implode

Add a prefix to each item of a PHP array

与世无争的帅哥 提交于 2019-11-26 05:25:27
问题 I have a PHP array of numbers, which I would like to prefix with a minus (-). I think through the use of explode and implode it would be possible but my knowledge of php is not possible to actually do it. Any help would be appreciated. Essentially I would like to go from this: $array = [1, 2, 3, 4, 5]; to this: $array = [-1, -2, -3, -4, -5]; Any ideas? 回答1: Simple: foreach ($array as &$value) { $value *= (-1); } unset($value); Unless the array is a string: foreach ($array as &$value) { $value

php implode (101) with quotes

非 Y 不嫁゛ 提交于 2019-11-26 04:39:19
问题 Imploding a simple array would look like this $array = array(\'lastname\', \'email\', \'phone\'); $comma_separated = implode(\",\", $array); and that would return this lastname,email,phone great, so i might do this instead $array = array(\'lastname\', \'email\', \'phone\'); $comma_separated = implode(\"\',\'\", $array); $comma_separated = \"\'\".$comma_separated.\"\'\"; and now i have what I want a nice pretty csv string \'lastname\',\'email\',\'phone\' is there a better way to do this, it