Reply to Re: single array to multidimensional

Your name:

Reply:


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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация