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
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation associative arrays

    I've stored template variables in an array with indexes 0,1,2.... using preg_match_all() function. Later I build string ( $template_vars ): 'TITLE' => "{TITLE}", 'DESCRIPTION' => "{DESCRIPTION}", 'KEYWORDS' => "{KEYWORDS}", 'PAGE_HEADER' => "{PAGE_HEADER}", 'MENU_HEADER' => "{MENU_HEADER}", 'BAR_LEFT' => "{BAR_LEFT}", 'PAGE_CONTENT' => "{PAGE_CONTENT}", 'BAR_RIGHT' => "{BAR_RIGHT}", 'FOOTER' => "{FOOTER}" from that array.
    Then I created, another array as $template_vars=array($template_vars_string);

    I exprected to have an associative array now. But it's an array with only one element $template_vars[0]

    How can I better deal with associative arrays?, please help. Thanx.

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    thats because your putting a variable into it.

    if you use this code it should display them all

    PHP Code:
    var_dump($template_vars[0]); 
    to make it an associative array try
    PHP Code:
    //this
    $template_vars=$template_vars_string;

    //instead of
    $template_vars=array($template_vars_string); 

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but that's ( both above ) not working.
    Also I realized code get ugly when I tried to construct $template_vars_string
    So, I've now two arrays $template_vars_keys and $template_vars_ I wanna create a third array $template_vars with associative nature containing keys from first array and values from second array. Please help me.

    http://www.mediasworks.com/

  • #4
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I explain more:
    I've first array
    Code:
    0 => TITLE
    1 => DESCRIPTION
    2 => KEYWORDS
    3 => PAGE_HEADER
    4 => MENU_HEADER
    5 => BAR_LEFT
    6 => PAGE_CONTENT
    7 => BAR_RIGHT
    8 => FOOTER
    I've second array
    Code:
    0 => {TITLE}
    1 => {DESCRIPTION}
    2 => {KEYWORDS}
    3 => {PAGE_HEADER}
    4 => {MENU_HEADER}
    5 => {BAR_LEFT}
    6 => {PAGE_CONTENT}
    7 => {BAR_RIGHT}
    8 => {FOOTER}
    I want third array using two above:
    Code:
    TITLE => {TITLE}
    DESCRIPTION => {DESCRIPTION}
    KEYWORDS => {KEYWORDS}
    PAGE_HEADER => {PAGE_HEADER}
    MENU_HEADER => {MENU_HEADER}
    BAR_LEFT => {BAR_LEFT}
    PAGE_CONTENT => {PAGE_CONTENT}
    BAR_RIGHT => {BAR_RIGHT}
    FOOTER => {FOOTER}
    I could've used array_combine() but we've not installed PHP 5.

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    function array_combine($keys$values)
        {
        if(
    count($keys) === count($values))
            {
            
    $new = array();
            
    $x count($keys);
            for(
    $i=1;$i<$x;$i++)
                {
                
    $new[$keys[$i]] = $valuess[$i];
                }        
            
            return 
    $new;
            }
        return 
    false;
        } 


  •  

    Posting Permissions

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