Jump to content

Array does not take value


lawless

Recommended Posts

Hello there :)

 

I'm looking for an solution to an very specific problem.

In order to make my template system working, I need to replace certain strings with different values. For example %%title%% will become $value['title'].

 

Now when trying this manually with strtr() or str_replace() everything works fine. This is kinda much to code, so I'm running an array through the strtr() function.

 

The problem is, that my array does not take the value : $value['title'] (string)

$array1['%%title%%'] = "{$value['title']}";

// same as

$array1 = array(
		"%%title%%" => "{$value['title']}");

I tried it with brackets, without, with escaping (backslash), with reference and I don't know why this does not work.

 

The $value variable is another array which contains entries from the database (already running inside an foreach loop where this code is situated at). Without the array, I can echo out the content without problems.

 

After I add this value to my array, var_dump() gives me:

Array
(
    [%%title%%] => 
)

It SHOULD give me:

Array
(
    [%%title%%] => $value['title']
)

That is what i need to run the string replacement function.

 

And yes, I tried it with different keys. This key does work properly as intended (function does take it as normal string).

 

So the problem only can be situated at why my array does not take this string. Can I mark this as string somehow I do not know of?

 

 

Link to comment
Share on other sites

It SHOULD give me:

Just to clarify, do you want it to be the value contained in the $value['title'] variable, or do you want it to be literally '$value['title']'?

 

If the former, var_dump $value to make sure it contains that key and that it has a value to start with. If the later then you need to either escape the $ or use single quotes around the string.

$array1['%%title%%'] = "\$value['title']";
Link to comment
Share on other sites

Well I had the insane idea, that I must

- Create an array which contains the wildcards as KEYS and the variable (as string) as VALUES

- run through an foreach loop to replace those (string) VALUES with the correct VALUES

- run through another foreach loop to replace the actual template wildcards with the (now) correct VALUES

 

This was one logical approach I do not wish anyone to think about :P

 

For the solution I did the following:

- Create an array with the acutal VALUES that should be entered in the string

- run the strtr() function

 

This is so much easier then whatever twisted thought I had in mind before. The problems with the first approach were (a) that is was too complicated and (b) catching the string to replace with the value. For that I would have had to run another foreach loop to rush through the array and replace the placeholder values with the correct values anyways. So why not directly inserting the correct value at the first time :)

 

 

Thank you very much for your help. I guess sometimes you've gotta take a small break from overthinking an solution.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.