CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   foreach help (http://www.codingforums.com/showthread.php?t=286442)

Clawed 01-25-2013 06:23 PM

foreach help
 
Hi,

I'm coding a template system for my site with multiple themes and i need to know how to echo things like this:

PHP Code:

$replace "['data1','Hi there'],['data2','Hello there']"

I need to explode or something like that with [] as 1 array with 2 data sections.
It's like array but not.

I know how to replace in the site:
PHP Code:

str_ireplace"#data1#""value of data1" );
str_ireplace"#data2#""value of data2" ); 

Is just need to know how to make [] these in to 1 array.

Can anyone help.
Would be very helpful.

Thanks,
Clawed

Fou-Lu 01-25-2013 06:36 PM

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
        )

)


Clawed 01-25-2013 07:11 PM

Thanks, i'll try these.
Quote:

Originally Posted by Fou-Lu (Post 1308760)
How'd you end up with a string like that?

I'm building a template system & it's all MySQL powered, all data comes from MySQL tables & it echos pages with:
Code:

<div>#data1#</div>
<div>#data2#</div>

And so on.

And after row content there's things that i want it to search and replace for example:
PHP Code:

$content "<div>#data1#</div>
<div>#data2#</div>"
;

$string str_ireplace"#data1#""Hi"$content );
$string str_ireplace"#data2#""Hi2"$string );

echo 
$string

Edit;
------------------------------------------------------------------------------------

Nope not how i wanted it, i want so i can use foreach() to set a template param, example:
PHP Code:

$aParts explode('],['trim($replace']['));
foreach (
$aParts AS &$part)
{
    
$part explode("','"trim($part"'"));
    foreach (
$part as $info => $value)
    {
          
$template->setParam($info$value);
    }


So when i do:
PHP Code:

echo $template->params['data1']; 

It echos "Hi there"
And same with data2:
PHP Code:

echo $template->params['data2']; 

It echos "Hello there"

Fou-Lu 01-25-2013 08:03 PM

Well no, this wouldn't work:
PHP Code:

    foreach ($part as $info => $value)
    {
          
$template->setParam($info$value);
    } 

That indicates that you have $info as something valid other than an offset. What that would do is set param0 and param1 to the value given. What you appear to want to do is this:
PHP Code:

foreach ($aParts AS $part)
{
    
$part explode("','"trim($part"'"));
    
$template->setParam($part[0], $part[1]);


A quick example of what I think you are doing:
PHP Code:

class T
{
    private 
$itms;
    public function 
__construct()
    {
        
$this->itms = array();
    }
    public function 
setParam($p$v)
    {
        
$this->itms[$p] = $v;
    }
    public function 
getParam($p)
    {
        return @
$this->itms[$p];
    }
}

$t = new T();
$replace "['data1','Hi there'],['data2','Hello there']";  
$aParts explode('],['trim($replace']['));
foreach (
$aParts AS &$part)
{
    
$part explode("','"trim($part"'"));
    
$t->setParam($part[0], $part[1]);    
}
print_r($t); 

Which would give me:
Code:

T Object
(
    [itms:private] => Array
        (
            [data1] => Hi there
            [data2] => Hello there
        )

)

To which I can access using:
Code:

print $t->getParam('data1');
Edit:
Oh, BTW, this is what makes sense:
PHP Code:

$content "<div>#data1#</div>
<div>#data2#</div>"
;

$string str_ireplace"#data1#""Hi"$content );
$string str_ireplace"#data2#""Hi2"$string ); 

What I don't understand is how you ever ended up with such a bizarre string like:
PHP Code:

 $replace "['data1','Hi there'],['data2','Hello there']"

If that comes from a database or something else, it would be better to retain it in its array and use the str_[i]replace and give it the arrays instead.

Clawed 01-25-2013 08:14 PM

Thankyou!
Worked perfect!


All times are GMT +1. The time now is 10:46 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.