|
Posted by Andy Hassall on 06/22/06 19:01
On 22 Jun 2006 09:24:04 -0700, "frizzle" <phpfrizzle@gmail.com> wrote:
>I have a string, comma separated, with links and their respective URLs
>in it. Example:
>Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org
>
>etc, etc.
>
>i make an array of this with explode( ',', $string );
>
>now what i wonder is how can i turn this array into something like
>this:
>
>$array[0]['name'] = Google
>$array[0]['url'] = http://www.google.com
>$array[1]['name'] = Yahoo!
>$array[1]['url'] = http://www.yahoo.com
>$array[2]['name'] = WikiPedia
>$array[2]['url'] = http://www.wikipedia.org
<?php
$str =
"Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org";
$parts = explode(',', $str);
$array = array();
for ($i=0; $i<count($parts)/2; $i++)
{
$array[$i]['name'] = $parts[$i*2];
$array[$i]['url'] = $parts[$i*2+1];
}
print "<pre>";
var_export($array);
print "</pre>"
?>
There are undoubtably faster, more concise and cleverer ways of doing this.
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
[Back to original message]
|