2

I've got this problem when trying to construct a variable name (which should output a corresponding array element.)

I start with setting the variables for testing purposes, normally the piece variable would hold the first element of an array $piece.

$wordnumber = 0;
$piece[0] = "forever";

I later echo them right before my problem to see if they're still ok.

echo "$piece[0]";
echo "$wordnumber";

The output is ok.

forever0

But then comes the problem, as I'm trying to make a function that automatically handles every single array element, so I want it to construct the next corresponding variable every time. However somehow it has no value after construction.

$name = ${'piece[' . $wordnumber . ']'};
echo "$name";

outputs nothing...

I've tried a lot of different formatting, I really don't know why I'm failing so hard here. The code isn't part of any function right now btw.

Update: $name = $piece[$wordnumber] solves the problem I'm curious though why my previous formatting didn't work as expected.

Update: Question solved by André, the problem was that $piece[0] wasn't actually part of an array. So $piece was the actual variable. After storing an actual array $piece = array("Redish", "Yellow", "Green"); at start and using global $piece; in my function everything started working like a charm.

3
  • possible duplicate. PHPs variable variables syntax doesn't support access by array index. read my full my answer (with solution) here: stackoverflow.com/questions/9374292/variable-name-as-variable/… Commented Feb 27, 2012 at 18:45
  • It seems I didn't search existing questions well enough just found this formatting $myTempArr[ $key ]; on link So I changed it to $name = $piece[ $wordnumber ]; and now it works like a charm outputting forever. Commented Feb 27, 2012 at 18:56
  • My question remains though, why ${piece[0]} with my apparently somehow faulty construction didn't work. I'm the curious type I guess. Commented Feb 27, 2012 at 19:01

2 Answers 2

1

I haven’t tested the code, but I think your first approach didn’t work because your variable’s name is “piece”, not “piece[0]”. In other words, ${'piece'}[0] should work, but ${'piece[0]'} is wrong. Try add this in the very beginning of your script and PHP should display you some complaints:

error_reporting(E_ALL);
ini_set("display_errors", 1);
Sign up to request clarification or add additional context in comments.

2 Comments

Very good André! Smart thinking! And very educational for me. The problem was indeed in the variable not being an actual array! I changed it to $piece = array("Red", "Yellow", "Green", "Blue", "Indigo"); and now everything works like a charm.
Thanks for your kind words. If my answer was enough for you, please set it as your accepted answer ;]
1

Try just $name = $piece[$wordnumber]; and echo $name
this will output "forever"

2 Comments

Yes this is correct, it also works with spaces like this $piece[ $wordnumber ] btw. I'm meanwhile searching for the reason behind my first formatting's dysfunction.
the array dereferers ([]) shall not be included in a string I think. Therefore ${'parts'}[$wordnumber] works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.