Cannot use a scalar value as an array - php

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];

Related

How to get string outside for loop PHP

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.

Incrementing the array value within an array using PHP

So my end result will be this (the end result will have 48 entries):
$theArray=array(
$theArray1,
$theArray2,
$theArray3,
$theArray4,
$theArray5,
$theArray6
);
I have tried a few things but I think this is the closest, but I'm still not there yet, any help appreciated.
$i = 0;
while ($i <= 48){
$theArray[]=${"theArray".$i.","}
$i++
}
$theArray[]=${"theArray".$i};
You missed ; at the end of line:
$i = 0;
while ($i <= 48){
$theArray[]=${"theArray".$i.","}; // missed ; here
$i++; // missed ; here
}
$theArray[]=${"theArray".$i};
You have to use the function compact for create an array containing variables. Try to read the documentation http://php.net/manual/en/function.compact.php
And here are some example http://www.w3schools.com/php/func_array_compact.asp
Keep it simple as below:
<?php
$i = 0;
$theArray = array();
while ($i <= 48){
array_push($theArray, '$theArray'.$i);
$i++;
}
echo '$arr = array('.implode(',', $theArray).');';
?>
Just run this on your end. Cheers!

Why does accessing an array element throw an array-as-string error in PHP?

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

how to call a numbered variable in a loop in php?

I know what i have implemented here is wrong i want it to do it correctly that is why asking help here.Don't know whether this is possible or not.
<?php
$test1="hello";
$test2="how";
$test3="are";
for($i=1;$i<=3;$i++)
{
echo $test.$i;
}
?>
When i run this i should get hello how are .i know string concatenation same thing i want to do it for variable also.Is this Possible, if possible by this i can easily access all those variable. Any help?
Try with following syntax:
echo ${'test'.$i};
I guess what you're looking for is the "array". You can use arrays in PHP like this:
$test = array('hello', 'how', 'are');
$len = count($test);
for($i=0; $i<$len; $i++) {
echo $test[$i];
}
Try this:
for($i = 1; $i < 4; $i++){
echo $test{$i};
}

Can I add $i in variable to call the same sequence variable?

<?php
$listing_weblinkurl_1z1 = "some1";
$listing_weblinkurl_1z2 = "some2";
$listing_weblinkurl_1z3 = "some3";
$listing_weblinkurl_1z4 = "some4";
for($i=1;$i<=4;$i++){
print ($listing_weblinkurl_1z.$i);
}
?>
It is not working. I know that it is wrong to add $i in variable to call it. But I want it.
This is how it would be done, using variable variables.
for ( $i = 1; $i <= 4; $i+= 1 )
{
$varname = 'listing_weblinkurl_1z' . $i;
echo $$varname;
}
However, this is not a good way of writing code. Instead, $listing_weblinkurl should probably be an array containing keys of 1z1, 1z2, etc.
for($i=1;$i<=4;$i++) {
print ( ${"listing_weblinkurl_1z$i"} );
}
As #McAden pointed out in the comments, it would probably be a better idea to use an array.

Categories