note 82230 modified in function.implode by felipe

From: Date: Thu, 03 Apr 2008 01:35:55 +0000
Subject: note 82230 modified in function.implode by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137875@lists.php.net to get a copy of this message
I'm new to PHP, coming from Perl and I already had a problem once when I wanted to join some strings. In perl you can use join to join a list (which can be an array and/or a list of strings and/or hashes), where PHP does this only with arrays. That annoyed me. I could not find any code which did this, so I created my own join, based on Perl's join function. It is called p_join(). <?php /* is_assoc_array and is_sequential_array are STOLEN from: * http://nl3.php.net/manual/en/function.is-array.php#73505 */ function is_assoc_array($var) { return (array_merge($var) !== $var || !is_numeric(implode(array_keys($var)))); } function is_sequential_array($var) { return (array_merge($var) === $var && is_numeric(implode(array_keys($var)))); } function _p_join_assoc($sep, $hash) { $result = ""; foreach ($hash as $k => $v) { $result .= $sep . $k . $sep . $v ; } return $result; } function _p_join_array($sep, $array) { # Turn off notices and return to the old loglevel once we're done. # This is because we stringify an array in some cases.. $old_error_reporting = error_reporting(E_ALL ^ E_NOTICE); $result = join($sep, $array); error_reporting($old_error_reporting); return $result; } function p_join() { $args = func_get_args(); if (count($args) > 1) { $result = ""; $sep = array_shift($args); foreach ($args as $val) { if (is_array($val)) { if (is_assoc_array($val)) { $result .= _p_join_assoc($sep, $val); } else { $result .= $sep . _p_join_array($sep, $val); } continue; } $result .= $sep . $val; } # $result will always start with a $sep, so remove it.. return substr_replace($result, "" ,0, strlen($sep)); } trigger_error(sprintf("%s requires at least 2 parameters", __FUNCTION__), E_USER_WARNING); return null; } /* And some example code */ print p_join(",", "string", array("Key" => "Value", "Key 2" => "Value 2"), array("Hello", array("in", "hello", "world"), "array"), "string 2") . "\n"; /* Watch out, prints de indices when it you supply a mixed array */ print p_join(",", array(1,2,"Green" => "Apple" , array(1,2))) . "\n"; print "Warning.. "; print p_join(","); ?> --was-- I'm new to PHP, coming from Perl and I already had a problem once when I wanted to join some strings. In perl you can use join to join a list (which can be an array and/or a list of strings and/or hashes), where PHP does this only with arrays. That annoyed me. I could not find any code which did this, so I created my own join, based on Perl's join function. It is called p_join(). <?php /* is_assoc_array and is_sequential_array are STOLEN from: * http://nl3.php.net/manual/en/function.is-array.php#73505 */ function is_assoc_array($var) { return (array_merge($var) !== $var || !is_numeric(implode(array_keys($var)))); } function is_sequential_array($var) { return (array_merge($var) === $var && is_numeric(implode(array_keys($var)))); } function _p_join_assoc($sep, $hash) { $result = ""; foreach ($hash as $k => $v) { $result .= $sep . $k . $sep . $v ; } return $result; } function _p_join_array($sep, $array) { # Turn off notices and return to the old loglevel once we're done. # This is because we stringify an array in some cases.. $old_error_reporting = error_reporting(E_ALL ^ E_NOTICE); $result = join($sep, $array); error_reporting($old_error_reporting); return $result; } function p_join() { $args = func_get_args(); if (count($args) > 1) { $result = ""; $sep = array_shift($args); $val = array_shift($args); if (is_array($val)) { if (is_assoc_array($val)) { $result .= _p_join_assoc($sep, $val); $result = preg_replace("/^$sep/", "", $result); } else { $result = _p_join_array($sep, $val); } } else { $result = $val; } foreach ($args as $val) { if (is_array($val)) { if (is_assoc_array($val)) { $result .= _p_join_assoc($sep, $val); } else { $result .= $sep . _p_join_array($sep, $val); } continue; } $result .= $sep . $val; } return $result; } trigger_error(sprintf("%s requires at least 2 parameters", __FUNCTION__), E_USER_WARNING); return null; } /* And some example code */ print p_join(",", "string", array("Key" => "Value", "Key 2" => "Value 2"), array("Hello", array("in", "hello", "world"), "array"), "string 2") . "\n"; /* Watch out, prints de indices when it you supply a mixed array */ print p_join(",", array(1,2,"Green" => "Apple" , array(1,2))) . "\n"; print "Warning.. "; print p_join(","); ?> http://php.net/manual/en/function.implode.php

« previous php.notes (#137875) next »