Can someone tell me why the constant length isn't defined? I'm using glob to find and put all .txt files into an array then using for to loop through them echoing them out.
$filew = glob("/xampp/htdocs/new/*.txt");
for($n=0; $n<$filew.length; $n++) {
$filew['$n'] = substr($filew['$n'], 18, -4);
echo $filew['n'] . " ";
}
The error message indicates the problem lines in the second line, someone told me to put array content into quote marks but it hasn't worked. Thank you
PHP arrays don't have a length property. You should use count instead
$filew = glob("/xampp/htdocs/new/*.txt");
for($n = 0; $n < count($filew); $n++) {
$filew[$n] = substr($filew[$n], 18, -4);
echo $filew[$n] . " ";
}
You also have some errors in the way you are accessing your array. Using a single quote will cause the variable inside to not be rendered. Using $filew['$n'] will not give you a numeric value for $n. It will give a literal string with the value $n. Remove the single quotes should be just fine.
Related
I have the main array called $quizzes which contains the collection of $Quiz.
Each $Quiz has the following fields: $Quiz['correct'] gives me the number of correct questions.
I can get the number of correct questions for 12th quiz using $quizzes[12]['correct']
However, since these quizzes are not displayed in order, I decided to define a new array:
$listoftests = array('$quizzes[30]','$quizzes[51]');
In my head, $listoftests[0]['correct'] should be equal to $quizzes[30]['correct'] but it's giving me
Warning: Illegal string offset 'correct' in
/demo.php
on line 14 $
when I try to echo $listoftests[0]['correct'];
In this $listoftests = array('$quizzes[30]','$quizzes[51]'); These are considered as two strings $quizzes[30] and $quizzes[51]. You should remove single quotes ' and try again.
Change this:
$listoftests = array('$quizzes[30]','$quizzes[51]');
To:
$listoftests = array($quizzes[30],$quizzes[51]);
By doing this
$listoftests = array('$quizzes[30]','$quizzes[51]');
you created array of 2 strings. That's it. Not an array of arrays.
You should remove quotes. And you can also use isset() to check if array item exists.
Remove the single quote and it shell work fine $listoftests = array($quizzes[30],$quizzes[51]);
#GRS you can do it in two way:
//case one where the element will be of string type
<?php
$quizzes[30] = 3;
$quizzes[51] = 32;
$listoftests = array("$quizzes[30]","$quizzes[51]");
var_dump($listoftests);
//or
//case two where the element will be of integer type
<?php
$quizzes[30] = 3;
$quizzes[51] = 32;
$listoftests = array($quizzes[30],$quizzes[51]);
var_dump($listoftests);
This is my php function it must return "5" but it returns nothing.
<?php
function get_second($num){
$second = $num[1]; //must return second number of the variable.
return $second;
}
$numbers=456789;
echo get_second($numbers);
?>
When I tried out this code, this returns nothing (NULL, empty).
But I tried out this function below, worked perfectly.
<?php
function get_second($num){
$second = $num[1]; //must return second number of the variable.
return $second;
}
$numbers=$_POST['number_input'];//that includes numbers
echo get_second($numbers);
?>
This code returns me second number of the post data. What I must do to work my First function? What is the difference between first $numbers variable and second $numbers variable?
Here the problem has to be better defined: how to get the second digit from a number. Your initial approach was correct in logic, but incorrect in the assumption that a number is a order set of characters. Only strings are ordered set of characters. Once you transform the integer 45678 to the string 45678 you can easily intercept the second character by using substr or even directly the string - because in PHP strings can be treated as arrays of characters.
#RamRaider solution is better than other have suggested but is overkill to use preg_split. Other solutions ask you to modify the type of the variable which is not done by adding quotes, but is done by casting to string, which is simpler and faster than a regular expression and you maintain your original variable in original form and your original function definition.
function get_second($num){
$second = $num[1]; //must return second number of the variable.
return $second;
}
$numbers = 456789;
// casting to string
echo get_second((string)$numbers);
// or transform to string by concatenation to a string
echo get_second($numbers ."");
// even quoting works
echo get_second("$numbers");
// using strval
echo get_second(strval($numbers));
// using settype
echo get_second(settype($numbers, "string"));
Try this: (add quotes to your integer variable)
<?php
function get_second($num){
$second = $num[1]; //must return second number of the variable.
return $second;
}
$numbers="456789";
echo get_second($numbers);
?>
If you want to get Character by its number then you can use substr()
function get_second($num)
{
return substr($num,1,1);
}
$numbers="456789";
echo get_second($numbers);
you are declaring a number in the $number variable.
if you want to view the second element than you have to use string.
try
$numbers= "456789";
it will output 5.
You could use preg_split to force an array from which you can choose any number by index, like:
$number=12358397;
function get_number($num,$i){
$num=preg_split( '#^\d$#', $num );
return $num[0][$i];
}
echo ' [1] > > > ' . get_number($number,1);
I'm following a tutorial and this piece of code came up:
for ($i=0; $i < $inlen ; ++$i) {
if (isset($this->morse[$in{$i}])) {
$out .= $this->morse[$in{$i}];
}
return $out;
}
$in is the key we're looking for in the morse array, but what does the $i in curly brackets represent in regards to the key=>value?
Thank you.
In this case, this is not part of a variable variable but rather String access (like getCharAt()). See the manual for more information.
$in{$i}
Becomes the character at $ith position of the string $in.
${'in'.$i}
Would be the variable variable in$i ($in0, $in1, ...)
This also makes sense as $inlen supposedly is strlen($in) and so the loop goes through each character of $in, one at a time.
I'm confused as to how PHP determines whether a variable is a string or an array. It seems to depend on the operators being used.
Here's an example:
<?php
$z1 = "abc";
$out = "";
for ($i = 0; $i < strlen($z1); $i++)
{
// $out[$i] = $z1[$i];
$out = $out.$z1[$i];
}
print $out;
?>
In the above version $out becomes a string (print $z1 shows "abc"). However, if I use the first line $out[$i] = $z1[$i];, $out becomes an array.
Can someone please clarify why this happens, and if its possible to access a string's characters with square brackets without converting the output to an array?
The definition of a string in PHP is considered a set of data writen in linear format (i.e: $var = "username=SmokeyBear05,B-day=01/01/1980";)
An array however is a set of data broken down into several parts. A sort of list format if you will. As an example I've written the data string from before, into an array format...
Array(['username']=>"SmokeyBear05", ['B-day']=>"01/01/1980")
Now strings are generally defined as such: $var="Your String";
Arrays however can be written in three different formats:
$var1 = array('data1','data2','data3');
$var2 = array('part A'=>'data1','part B'=>'data2','part C'=>'data3');
The output of var1 starts the index value at 0. The output of var2 however, sets a custom index value. Now the third way to write an array (least common format) is as such:
$var[0]="data1";
$var[1]="data2";
$var[2]="data3";
This takes more work, but allows you to set the index.
Most web developers working with PHP will set data from an external source as a string to deliver it to another PHP script, and then break it down into an array using the explode() function.
When you define variable $out = "", for loop doesn't understand this variable as string value. If you set $out[$i] value, by default, it was treated as an array.
If you want to get the output result as string value, you can define $out = "a" to make sure it's a string variable.
I know there are a lot of similar questions on here, and I think I've read them all. My problem is that I am attempting to loop through a list of arrays and grab one value from each. The arrays have been set up by a third party, and I don't have the access to configure how I am receiving them. Here's what I have so far:
for ($i = 0; $i < $length; $i++) {
// Both of these work and return the value I need
echo $post->related_credits_0_related_show[0];
echo "{$post->related_credits_0_related_show[0]}"
// But none of these do, and I need to loop through a handful of arrays
echo "{$post->related_credits_{$i}_related_show[0]}";
echo "{$post->related_credits_${i}_related_show[0]}";
echo "{$post->related_credits_{${i}}_related_show[0]}";
echo "{$post->related_credits_".$i."_related_show[0]}";
}
I've tried many (many!) more combinations that I won't include. I've also tried converting $i to a string. Been knocking my head against this for awhile.
Thanks ahead of time for any help.
You need to use variable variables here. The basic usage is as follows:
$var = 'Hello there!';
$foo = 'var';
echo $$foo;
^^--- note the double $-sign
This will output:
Hello there!
Instead of $$foo, you can also write the following:
echo ${"$foo"};
If the variable name is more complex, you can can also do:
echo ${"some_stuff_{$foo}_more_stuff"};
In this case, the string that denotes the variable name contains a variable, and that variable is also wrapped inside curly braces ({}). This is done in order to avoid problems with constants, array indexes etc. But if your use-case doesn't involve any of those, you don't have to worry about it.
For your specific problem, you can use the following:
for ($i=0; $i < $length; $i++) {
$post->{"related_credits_{$i}_related_show"}[0];
}
Or, if you prefer concatenation:
for ($i=0; $i < $length; $i++) {
$res = $post->{'related_credits_'.$i.'_related_show'}[0];
}
See the documentation on Variable Variables for more information.
You can use this:
$varname = "related_credits_$i_related_show";
$array = $post->$varname;
echo $array[0];
A shorter form would be:
$post->{"related_credits_{$i}_related_show"}[0];
Here you find all about so called "variable variables" : http://www.php.net/manual/en/language.variables.variable.php