Thread: foreach help
View Single Post
Old 01-25-2013, 06:36 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
How'd you end up with a string like that?
If you have a newer version of PHP and the data comes from a completely trusted source, you can use eval:
PHP Code:
$replace "['data1','Hi there'],['data2','Hello there']";  

eval(
"\$a = [$replace];");
print_r($a); 
Otherwise you can explode it and then explode each item:
PHP Code:
$aParts explode('],['trim($replace']['));
foreach (
$aParts AS &$part)
{
    
$part explode("','"trim($part"'"));        
}
print_r($aParts); 
Both of which should result in:
Code:
Array
(
    [0] => Array
        (
            [0] => data1
            [1] => Hi there
        )

    [1] => Array
        (
            [0] => data2
            [1] => Hello there
        )

)
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote