Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: foreach help

  1. #1
    New Coder
    Join Date
    Nov 2012
    Location
    United Kingdom
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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
            )
    
    )
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Nov 2012
    Location
    United Kingdom
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thanks, i'll try these.
    Quote Originally Posted by Fou-Lu View Post
    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"
    Last edited by Clawed; 01-25-2013 at 07:49 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    Last edited by Fou-Lu; 01-25-2013 at 08:05 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Clawed (01-25-2013)

  • #5
    New Coder
    Join Date
    Nov 2012
    Location
    United Kingdom
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thankyou!
    Worked perfect!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •