• 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

    • Meta says you'll soon be getting ads on WhatsApp by David Uzondu No, the headline is not clickbait. WhatsApp is getting ads. For years, the messaging app has been the golden child, bought by Meta for $19 billion back in 2014 and left mostly untouched. That long, ad-free holiday is officially over. Your private chats with friends and family are safe for now, as the ads will not appear there. Instead, they will be sandwiched between Status updates, just like you already see them in Instagram Stories. Apart from ads in Statuses, Meta is also letting people pay to promote their Channels. This means businesses and creators can pay to get their broadcast channels discovered by more users. A few will even be able to charge for subscriptions to their channels for exclusive content. The company says it will not take a fee from these subscriptions at first, but that is probably not going to last forever. Meta is a business, after all, not a charity. Meta has tried to calm everyone down by saying your personal chats and calls will remain encrypted and untouched. The company claims it only uses general information like your country and language, plus the channels you follow, to figure out which ads to show you. However, if you have linked your WhatsApp to Meta's Account Center, then your ad preferences from Facebook and Instagram will follow you. This entire plan certainly excited Wall Street. After the news broke, Meta's stock climbed 2.8% in pre-market trading, as investors salivated over the prospect of finally monetizing WhatsApp's 2 billion+ users. Ads in messaging apps are not new at all. Take Telegram, for example. The app shows sponsored messages in large public channels, but it also gives users a way out. For a monthly fee, you can get Telegram Premium, and all those ads disappear (plus a bunch of advanced paid features). Maybe, in the future, WhatsApp will offer a similar premium service for people willing to pay to escape the ads.
    • To say that it's build in America, they can build it somewhere else and just put the back cover on the smartphone in America et put it in a box. On some country they put just the last screw to finish the product
    • "The T1 phone will be designed and built in America." I was looking for this information and wasn't disappointed! 👍🏻
  • 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
      639
    2. 2
      ATLien_0
      281
    3. 3
      +FloatingFatMan
      172
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      129
  • Tell a friend

    Love Neowin? Tell a friend!