How to implode array with key and value without foreach in PHP
You could use http_build_query, like this: <?php $a=array(“item1″=>”object1”, “item2″=>”object2″); echo http_build_query($a,”,’, ‘); ?> Output: item1=object1, item2=object2 Demo
You could use http_build_query, like this: <?php $a=array(“item1″=>”object1”, “item2″=>”object2″); echo http_build_query($a,”,’, ‘); ?> Output: item1=object1, item2=object2 Demo
An elegant way to prefix array values (PHP 5.3+): $prefixed_array = preg_filter(‘/^/’, ‘prefix_’, $array); Additionally, this is more than three times faster than a foreach.
In this way you are wrapping the entire set in one span, you have to add the closing/opening tag to the implode: $value = “<span>”.implode(‘</span>,<span>’, $values).”</span>”;
$array = array(‘lastname’, ’email’, ‘phone’); echo “‘” . implode(“‘,'”, $array) . “‘”;
I’m assuming that you have this array: $array = array ( array (’01’,’03’,’02’,’15’), array (’05’,’04’,’06’,’10’), array (’07’,’09’,’08’,’11’), array (’12’,’14’,’13’,’16’) ); In which case, you can do this: $tmpArr = array(); foreach ($array as $sub) { $tmpArr[] = implode(‘,’, $sub); } $result = implode(‘|’, $tmpArr); echo $result; See it working
Simplest way, when you have only one item in inner arrays: $values = array_map(‘array_pop’, $array); $imploded = implode(‘,’, $values); EDIT: It’s for version before 5.5.0. If you’re above that, see better answer below 🙂
Use boost::algorithm::join(..): #include <boost/algorithm/string/join.hpp> … std::string joinedString = boost::algorithm::join(elems, delim); See also this question.
Use implode implode(“|”,$type);
Quite simple: $input = array( array( ‘tag_name’ => ‘google’ ), array( ‘tag_name’ => ‘technology’ ) ); echo implode(‘, ‘, array_map(function ($entry) { return $entry[‘tag_name’]; }, $input)); http://3v4l.org/ltBZ0 and new in php v5.5.0, array_column: echo implode(‘, ‘, array_column($input, ‘tag_name’));