note 93778 deleted from function.explode by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:49:16 +0000 |
| Subject: | note 93778 deleted from function.explode by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215329@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
Note to the previous example: we can do the whole string->array conversion using explode()
exclusively.
<?php
// converts pure string into a trimmed keyed array
function string_2_array( $string, $delimiter = ',', $kv = '=>')
{
if ($element = explode( $delimiter, $string ))
{
// create parts
foreach ( $element as $key_value )
{
// key -> value pair or single value
$atom = explode( $kv, $key_value );
if( trim($atom[1]) )
{
$key_arr[trim($atom[0])] = trim($atom[1]);
}
else
{
$key_arr[] = trim($atom[0]);
}
}
}
else
{
$key_arr = false;
}
return $key_arr;
}
?>