Convert php array to formatted string -



Convert php array to formatted string -

lets have array this, multi-dimensional need create loop recursive.

i think i'm close can't quite see i'm wrong.

[ { "value": "rigging" }, { "value": "animation" }, { "value": "modeling" } ] function _replace_amp($post = array()) { foreach($post $key => $value) { if (is_array($value)) { $b = $this->_replace_amp($value); } else { $b .= $value . ', '; } } homecoming $b; }

the intended result should be:

"rigging, animation, modeling"

i'm getting "modeling,"

in code, need write

$b .= $this->_replace_amp($value); // note period

without period, initiating $b every time script finds new array, want append results $b.

other that, there nice implode function multidimensional arrays available:

/** * recursively implodes array optional key inclusion * * illustration of $include_keys output: key, value, key, value, key, value * * @access public * @param array $array multi-dimensional array recursively implode * @param string $glue value glues elements * @param bool $include_keys include keys before values * @param bool $trim_all trim whitespace string * @return string imploded array */ function recursive_implode(array $array, $glue = ',', $include_keys = false, $trim_all = true) { $glued_string = ''; // recursively iterates array , adds key/value glued string array_walk_recursive($array, function($value, $key) utilize ($glue, $include_keys, &$glued_string) { $include_keys , $glued_string .= $key.$glue; $glued_string .= $value.$glue; }); // removes lastly $glue string strlen($glue) > 0 , $glued_string = substr($glued_string, 0, -strlen($glue)); // trim whitespace $trim_all , $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string); homecoming (string) $glued_string; }

source: https://gist.github.com/jimmygle/2564610

php arrays string multidimensional-array

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -