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.
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);
Try just $name = $piece[$wordnumber]; and echo $name
this will output "forever"
Related
I am faced with a piece of code that does not make a lot of sense to me. I am working on a website using PHP and Smarty template, but there is one line of code regarding arrays that i do not understand how it works.
$SLang = &SLanguage::getInstance();
$q="SELECT * FROM texte where text_lang='{$SLang->lang}' ORDER BY text_id";
$texte = _sqlFetchQuery($q);
foreach($texte as $text)
{
$texteList[$text['text_alias']]['text'] = $text['text_text'];
if($text["text_category"]==3){
$philosophyList[] = $text["text_text"];
$philosophyListSeo[] = $text["text_alias"];
}
}
The output of "var_dump" on $philosophyList gets out only the "text_text" column from the database, and i do now understand the structure of how it gets there. Can someone care to explain? How does this particular line of code works? $texteList[$text['text_alias']]['text'] = $text['text_text'];
It's called a jagged array.
It's shorthand for this:
$texteList[$text['text_alias']] = array('text'=>$text['text_text']);
So it's creating a named array using whatever text is $text['text_alias'] brings out, and assigning as it's value an array with a named "text" element.
But this is irrelevant to $philosophyList[]. To understand how $philosophyList[] is working, you need to understand how foreach works; basically it takes each item in an array (in this case $texte) and assigns the value of that array item to a variable (in this case $text). This is just an easier way to do a for loop. They could just have easily done:
$philosophyList[] = $texte[$i]["text_text"];
I am trying to output array content in a for loop. When I use the literal name of the array, it works. When I try to use a variable assigned to that name it fails. I need to use a variable to allow the page to display the correct content based on user's choice.
the array, saved as an include, is 2-dimensional of the form:
$newsArray2012 = array(
array(string_a, string_b, stringc),
array(string_a, string_b, stringc),
array(string_a, string_b, stringc), etc.,
);
The for loop looks like this:
for ($row = 0; $row < 5; $row++) {
echo "<p class='portrait'><a name='".$newsArray2012[$row][1]."'></a>\n
<em>".$newsArray2012[$row][0]."</em></p>\n
<img src='/Pictures/".$newsArray2012[$row][1]."' width='72' height='80' alt='' />\n";
echo $newsArray2012[$row][2]."\n";
}
That works.
I've assigned $newsCurrent = newsArray2012. When I substitute $newsCurrent for $newsArray2012 in the for loop, I do not get any output. In testing, I found that print_r($$newsCurrent) will dump the full array and even though that's goofy, I tried the $$ version in the for loop but it does not work there.
Where have I gone wrong?
OK, since $$newsCurrent worked with print_r, but not in the for loop, I assumed that PHP had a problem with the $$. Since it allowed the $$ in print_r, I made the leap that it would accept a re-assignment as follows:
$pageBios = $$newsCurrent;
This actually worked. I placed $pageBios in the for loop and had success. I'm not happy with it because the $$ format is not right - at least I've never seen it. So while I need to move forward due to deadlines, I would really like to clean this up - so please feel free to show me a non-moronic approach. Thanks.
Right I had to make a variable as shown below.
$questionID = '$quizinfo[\'Q' . $ques_num . '_ID\']';
But now the code wont run when shown anywhere it shows like $quizinfo['Q1_ID'] Instead of getting the variable that the php got earlier that behind it.
I need to be done like that as there 15 questions in a quiz and the ids I held and I change the number every time to get the new ID number but it not getting them how do I fix it?
checking it I put in.
echo $quizinfo['Q1_ID'];
And it worked correctly so what do I need to do to make it work?
Try:
$questionID = $quizinfo['Q' . $ques_num . '_ID'];
It should work.
When you write:
$questionID = '$quizinfo[\'Q' . $ques_num . '_ID\']';
$quizinfo[…] is not interpreted. It is taken as a string.
See also:
Php variables inside strings
PHP - concatenate or directly insert variables in string
PHP: variables in strings without concatenation
PHP: Beware of Variables Inside Strings
Mixing PHP variable with string literal
Since it is just php, why do you escape it?
$questionID = $quizinfo['Q' . $ques_num . '_ID'];
should do it. Also, You have ]' which should be replaced by '] at the end.
I think you are looking to use a variable for your array index:
$index = "Q{$ques_num}_ID";
echo $quizinfo[$index];
The problem you're experiencing is that you've turned the whole expression into a string with a number inside it, and so no array look-up was being performed.
As an aside: having indices that are human-readable is nice for humans, but not necessary for a computer. Unless you have a particular need to do it this way, I'd change this so it is just numerically indexed - it'll simplify your code.
I have a long function with many if statements and such. The first thing i do in my function is this: $text = file($read_text_file);
The $text array contain all text in the site therefor I need this array to be available in my entire function and I though I would accomplice this by defining the variable first thing in the function, but now i discovered that later in a if statement, its blank. Why is this?
Put var_dump($text); on the line immediately after $text = file($read_text_file);. If the array is populated, then you will get the values of the array. If not, then my guess is that there is a problem with the file itself.
As William said in the comments above we really need to see some context of this problem in order to answer this question. There could be any number of issues happening. Are you sure that the file is being read correctly as well?
How come when I echo $p, the variable which Im trying to fetch using this loop doesnt get displayed in the path.
$name_image2="picture.jpg";
for ($i=2; $i<=$nr_of_pics; $i++){
$img='name_image'.$i;
echo $$img; gives me this: 'picture.jpg' which is correct.
but when echoing $p like this:
$p="/SV/main/temp_images/$$img"; echo $p;
I get this: SV/main/temp_images/name_image2 --> the variable 'name_image2' doesnt get called here, why?
I want it to say: SV/main/temp_images/picture.jpg
Thanks
$p = "/SV/main/temp_images/" . $$img;
Ought to fix it.
Also, I would recommend learning how to use arrays. They are a much better way to have a set of data instead of variable variables.
Try $p="/SV/main/temp_images/{${$img}}";
When PHP is parsing the string and comes to a $, it looks at the next character to see if it makes a valid variable name. If not, it moves on. In this case, that means that the second $ is correctly interpreted, but the first one has already been passed by. The answer is to enclose the inner expression in brackets, so that it will be parsed before the outer one is.