...

View Full Version : associative arrays



anshul
06-14-2005, 06:37 AM
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.

SeeIT Solutions
06-14-2005, 07:20 AM
thats because your putting a variable into it.

if you use this code it should display them all


var_dump($template_vars[0]);

to make it an associative array try

//this
$template_vars=$template_vars_string;

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

anshul
06-14-2005, 07:32 AM
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/

anshul
06-14-2005, 08:29 AM
OK, I explain more:
I've first array
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
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:
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.

Kurashu
06-14-2005, 07:30 PM
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;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum