How I can get $lorem_text as a new string $lorem_outside_text without changing $lorem_text?
for ($i = 0; $i <= 5; $i++) {
$lorem_text = "lorem\n";
}
$lorem_outside_text = $lorem_text;
echo $lorem_outside_text;
Now result is: lorem
Result should be: lorem lorem lorem lorem lorem
With this piece of code you are overwriting your existing value every time.
for ($i = 0; $i <= 5; $i++) {
$lorem_text = "lorem\n"; // this just overwrites $lorem_text value 6 times with same value
}
Instead try to concatenate it using . and also remove extra using of variables here $lorem_outside_text
$lorem_outside_text = ''; //intialize it as empty string
for ($i = 0; $i <= 5; $i++) {
$lorem_outside_text .= "lorem\n";
}
echo $lorem_outside_text;
Pretty Neat:
<?php
echo str_repeat("lorem\n", 6);
?>
DEMO: https://3v4l.org/CIsCB
You may use concatenating assignment operator, Please try the following code:
$lorem_text .= "lorem\n";
$lorem_outside_text = '';
for ($i = 0; $i <= 5; $i++) {
$lorem_outside_text .= $lorem_text;
}
echo $lorem_outside_text;
You must append the text to the $lorem_text, not overwrite it. Type .= instead of =.
Personally, I'm usually adding text fragments to an array inside a loop, and then concatenate them using implode("\n", $lorem_parts).
Just use str_repeat, that will repeat the string however many times you want.
str_repeat($lorem_text, 5);
Another option is to create an array in the loop that you later implode.
for ($i = 0; $i <= 5; $i++) {
$lorem_text[] = "lorem";
}
echo implode("\n", $lorem_text);
Implode takes an array and uses the glue to make it string.
Related
Trying to remove the comma that comes after the number 10. Tried every plausible workaround, but nothing's worked so far.
$i = 0;
while($i < 10) {
echo ++$i. ",";
}
Don't use a loop in the first place. Use implode() to insert a delimiter between array elements.
echo implode(',', range(1, 10));
You can do it using loop and rtrim() like this:
$i = 0;
$result = '';
while($i < 10) {
$result .= ++$i. ",";
}
echo rtrim($result, ',');
I've done some searching and I didn't find any posts that quite answered my question.
I have a PHP array generated, for the sake of argument, with this code:
$i = 5;
for($i = 0; $i < $j; $i++) {
$multiArray[0][$i] = $i;
$multiArray[1][$i] = $i;
}
When I try to access it with:
for($i = 0; $i < $j; $i++) {
echo "$multiArray[0][$i]";
echo "$multiArray[1][$i]";
}
I get:
Notice: Array to string conversion on line 3
Notice: Array to string conversion on line 4
...x4
When I replace echo with printf("%d", $multiArray[0][$i]) then it prints fine. Why do I have to explicitly tell PHP that I'm asking for an int when the element I'm accessing is clearly an int (and PHP knows it, via var_dump())? I'm not accessing the array, but an element within the array.
Thanks
Simple double quoted variable interpolation supports up to one nested element. In other words, "foo[0][1]" is interpreted as "{$foo[0]}[1]". That means it tries to interpret the array $foo[0] as a string at that point to interpolate it into the string.
But using quotes here at all is entirely nonsensical. You don't want string interpolation, you just want to output a variable value:
echo $multiArray[0][$i];
just try this:
for($i = 0; $i < $j; $i++) {
echo $multiArray[0][$i];
echo $multiArray[1][$i];
}
Your code is parsing the string not the array, try to remove the quotes.The [] brackets after the array are considered as a string not the as a paremeter.Use the code below
<?php
$j = 5;
for($i = 0; $i < $j; $i++) {
$multiArray[0][$i] = $i;
$multiArray[1][$i] = $i;
}
for($i = 0; $i < $j; $i++) {
echo $multiArray[1][$i];
}
Hope this helps you
My goal is to make ant assoc array from the values of for loop.
//$from_time value is 6 and $to_time value is 23
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = $i;
}
echo json_encode($working_time_array);
The output I get on AJAX success, and when I console.log it, I get result as such :
["6",7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
Preferred result is
["6","7","8","9","10"]... etc
The only difference between the two results is one result set contains integers and the other contains strings. If you want those values to be strings just cast them when assigning them to the array:
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string) $i;
}
This really shouldn't be necessary unless your client side is expecting strings only.
You would need to cast $i to a string before pushing it to the array.
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string)$i;
}
why would you convert int to string?
for your goal this should work
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = "$i";
}
echo json_encode($working_time_array);
I am trying this code:
for ($x = 0; $x < $numCol; $x++) {
for ($i = 0; $i < $numRows; $i++) {
$arr.$x[] = $todas[$i][$x]."\n"; //problem here
}
}
echo $arr0[0];
echo $arr1[0];
...
But i get this warning: Cannot use a scalar value as an array
and the echos do nothing. Why ? and what is the solution ?
Here's what you think you want to do. Replace your //problem here line with:
${'arr' . $x}[] = $todas[$x][$i]."\n";
But I would strongly recommend against doing that. Just use your bidimensional array.
I think you meant: ${'arr'.$x}[] instead of $arr.$x[].
$arr.$x[]
Will concatenate the string representation of $arr and $x together so you end up with something like 'Array0'[] = ... instead of $arr0[]
When you write $arr.$x[], it is equal to $arr[$x][]
Try replacing your echos by
echo $arr[0][0];
echo $arr[1][0];
What's the fastest way to populate an array with the numbers 1-100 in PHP? I want to avoid doing something like this:
$numbers = '';
for($var i = 0; i <= 100; $i++) {
$numbers = $i . ',';
}
$numberArray = $numbers.split(',');
It seems long and tedious, is there a faster way?
The range function:
$var = range(0, 100);
range() would work well, but even with the loop, I'm not sure why you need to compose a string and split it - what's wrong with simply:
$numberArray = array();
for ($i = 0; $i < 100; $i++)
$numberArray[] = $i;