• 0

[PHP] Array indexes as string value


Question

Hi guys,

I wonder why I'm not able to use strings as array index to get a value from the array?

Is there any other way of doing it (without evals please)?

Example:


$arr = array(
"foo" => array(
"bar" => "abc",
"baz" => "CBA"
)
);

$string = "['foo']['bar']";
echo $arr['foo']['bar']; // OK: abc
echo $arr{$string}; // Fail: Undefined index: ['foo']['bar']
[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1053231-php-array-indexes-as-string-value/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

You can use strings as array indices, but you're also trying to use the strings as a substitute for the language.


<?php
$array = array(
'foo' => array('bar' => 'boz')
);
$foo = 'foo'; $bar = 'bar';
echo $array[$foo][$bar]; //boz
[/CODE]

You could capture the array keys though, but I'd guess that if you expand upon what you're trying to do there will be a better solution. :)

  • 0

What Anthony said. You can't replace the syntax with your own strings.

It's like saying:

$function_end = ()

some_function$function_end;

Just doesn't make sense to me. Do it the proper way.

Or maybe you're doing something wrong in the first place, nothing should make you want to replace the syntax itself.

What are you trying to do?

  • 0

I think this is what you're trying to do...


<?php
$array = array (
"foo" => array(
"bar" => "abc",
"baz" => "CBA"
)
);

$name = "['foo']['bar']";
var_dump ($array . $name);
?>
[/CODE]

With the code $array[$string] you are basically trying to find $array[['foo']['bar']]; which is invalid, whereas by printing it as $array . $name, it becomes $array['foo']['bar'];

Bad explanation, but I hope you understand :)

[b]Edit[/b]

Damn! I just noticed that code doesn't even work. The code I provided now echos Array['foo']['bar'];

[b]Edit #2[/b]

On further research, and asking other PHP programmers, we don't think it's possible and aren't really sure why you would want to do it this way. If you can give us an example of how your template system works, we may be able to help you with another system to grab the template data?

  • 0
  On 06/02/2012 at 17:55, Jayzee said:

Templating engine.

Instead of reinventing the wheel why don't you use something like http://twig.sensiolabs.org/ ?

One of the most widely used, arguably the best templating engine for professional developers, you can't go wrong with twig.

  • 0

Yes, I looked into every templating engine available for PHP (incl. Twig) and found them bloated with bunch of extras I don't need, therefore I decided to create my own templating engine (I have already developed file caching engine which works just great and which templating engine now uses). I have analyzed source of most popular templating engines and created my own mini-templating engine based on best practices learned by analyzing source and reading articles (improving your skills as a developer is also a big +). I no longer use method array-replace method I asked about earlier :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Popular Now

  • Posts

    • There is a lot of truth to that, but also, a lot of those issues were due to 3rd parties being slow to support Vista. That doesn't explain all users, but a lot of them. Win7 didn't have better support for XP-era hardware and software, it simply didn't have to deal with it as much because Vista has already blazed the way and forced that stuff to be updated or replaced.
    • MIght as well go back to good old SMS. Or switch to other services.
    • Nintendo 64 games were $60-$70 Playstation 1 were $50-$60 28 years ago. Heck I remember paying $70 for Final Fantasy IV (2) in 1991 for SNES. Salarys have gone up, both for the consumer's salary and the developer's salary. So I think it's pretty reasonable for $80games today. Not that I pay for new games, I don't have the time to play games as much as before. So I tend to buy them 2 years after release.
    • I hear you on that, and if that is happening, then its bad. Could you give an example please? People make these comments all the time but rarely provide an example.
    • Firefox gets new way to pin and unpin tabs by Taras Buria Mozilla Firefox recently received a long-anticipated feature: vertical tabs. It arrived in March 2025 as part of the Firefox 136 update. Now, Mozilla is introducing another welcome improvement to tab management in its browser, making it easier for users to pin and unpin tabs in Firefox. With the latest Firefox Nightly update, Mozilla implemented a new way to pin or unpin tabs: you can simply drag the current page to another pinned tab to pin or drag it out to unpin it. As simple as that. The new logic works with both vertical and horizontal tab views, and it eliminates the need to use context menus. The only prerequisite is that you need to have at least one pinned tab for the dragging to work. Otherwise, you will simply move the tab across the strip. It is nice to see Firefox implementing this small convenience for customers. However, some argue that there is still room for improvement. Dragging tabs is cool, but a dedicated shortcut could be an even better and quicker way to pin or unpin your tabs. That said, other mainstream browsers lack this feature as well. The improved tab pinning is now available in Firefox Nightly, the least stable update channel. Mozilla uses it to test early changes and big platform changes, which could be buggy, unstable, or feature-breaking. Therefore, it is probably not a very good idea to use Nightly builds as your main browser. Still, you can run Firefox Nightly side-by-side with other channels for testing purposes. If you are curious, get it from the official website using this link.
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      636
    2. 2
      ATLien_0
      278
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      128
  • Tell a friend

    Love Neowin? Tell a friend!