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.
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
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];
I'm creating a program which goes through data in a array and filter outs any repeats within it and then echos out anything which isn't a repeated piece of data
for ($i = 0; $i < count($urlArray); $i++) {
for ($j = 0; $j < count($urlArray); $j++) {
if($i != $j)
{
if($urlArray[$i] !== $urlArray[$j])
echo $urlArray[$i];
}
}
}
I'm fairly certain there's something wrong but I can't quite spot it, any help with this would be great.
I dont get, how your array is structured, but whats about just array_unique()
$urlArray = array_unique($urlArray);
Or in your case (because you want to echo it
foreach (array_unique($urlArray) as $url) echo $url;
Update:
Sorry, just mixed up two functions :) Of course its array_unique() and not array_filter().
You could use built in function array_unique() to remove duplicate values in array
$result = array_unique($urlArray);
print_r($result);
Im trying to spit out each letter of the alphabet from an array on a single line, A-Z.
This is what my code looks like so far:
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
while ($alphabet) {
echo $alphabet;
$alphabet;
}
Im kinda stuck at this part and not quite sure what else to write to make this work. Any suggestions?
Use range and array_walk:
function e($s) { echo $s; }
array_walk(range('A', 'Z'), 'e');
Working example: http://codepad.org/pedjOlY9
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
foreach($alphabet as $letter) {
echo $letter;
}
I am not sure why you need the array...
This is why we have the ASCII code.
You can do is like this:
for ($i = 65; $i <=90; $i++)
{
echo chr($i) . PHP_EOL;
}
chr() displays the character in the ASCII map - check it here: http://www.danshort.com/ASCIImap/. If you want to do lowercase - just use strtolower() or the numbers between 97-122 instead. PHP_EOL is a built in constant that outputs end of line. You can change it with ."" if you are doing HTML.
I think range is a little bit longer to be done, but it still works.
This might be help full for you
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$c = sizeof($alphabet);
for($i= 0; $i < $c ; $i++) {
echo $alphabet[$i];
}
and you can use count($alphabet) instead of sizeof() built-in function