Calculate the array name PHP - php

I have a large number of arrays
arrawithvalues0
arrawithvalues1
arrawithvalues2
arrawithvalues3
......
arrawithvalues999
Therefore I would like to calculate the name of the array to use in my script
I am using this code:
$secondarray = array();
for($iii = 0; $iii < 1000; ++ $iii) {
$array3 = "arrawithvalues" . $iii;
for($II = 0; $II < 10; ++ $II) {
array_push ( $secondarray, $$array3[$II]);
}
}
But get errors like this
Notice: Undefined variable: a in xxxxxxxxx on line xx
Notice: Undefined variable: r in xxxxxxxxx on line xx
Notice: Undefined variable: r in xxxxxxxxx on line xx
Thanks for reading and help

Use braces, like this:
array_push ( $secondarray, {$$array3}[$II]);
Right now, your code is being interpreted as this:
array_push ( $secondarray, ${$array3[$II]});
which is why you are getting those errors.

Related

Got fatal error in PHP array

$all = array($stu_quiz_1, $stu_quiz_2, $stu_quiz_3);
$length = count($all);
$low = 10;
$lowest = 0;
for($i = 0; $i<$length; $i++){
if($all($i)<= $low){ // line 34
$lowest = all($i);
}
else{
continue;
}
return $lowest;
}
I am new at php so please help me to find it. I just want to get lowest value from this code. I have three values like $stu_quiz_1 = 20, and so on ...it shows:
Fatal error: Function name must be a string in C:\xampp\install\htdocs\just\quiz_handle.php on line 34
if($all($i)<= $low){ // line 34
$all is not a function so you can't use parentheses. You'll have to use square brackets [] to access the array value.
To simply get highest or lowest values from an array there are some perfectly suited functions built into the core of PHP - namely max and min.
$all=array( 0,1,23,99,34,838 );
$lowest = min( $all );
$highest= max( $all );
echo $lowest,', ', $highest;
/* output: 0, 838 */
Man... it is not $all($i), but $all[$i].
Assuming your function is called all then your if clause should be called like below
if(all($i)<= $low){ // line 34
Note the missing $ from the beginning, thus the name is a string and not a variable.
This line :
$lowest = all($i);
Means that you're calling the function all() with $i as a parameter.
But you $all is actually an array not a function so to access an element of an array you use [].
So you have to change it to :
$lowest = $all[$i];
First:
Change $all($i) to $all[$i] (on line 34)
Second: Change $lowest = all($i); (below the line 34) with $lowest = $all[$i];. In this you were missing a $ sign in front of all and $i was to be kept inside [] because $all is a variable (containing an array).

My php echo statements do not output expected array contents from SQL query

I have the following code which selects ID's of a database with many regions in it. I create the array as follows:
$old2 = $this->query("SELECT regionid FROM theregions WHERE regionparent = '808'");
$old2_array = mysqli_fetch_array($old2);
And then I try and output the array like so:
while($row = mysqli_fetch_array($old2)) {
echo $row[0] . '<br>';
}
It produces the following:
800
834
933
However, if I use phpMyAdmin and run the same SQL statement, I get:
604
800
834
933
If I try using print_r(array_values($old2_array)); I get:
Array ( [0] => 604 [1] => 604 )
Have searched many SO pages, and have tried this:
$length = count($old2_array);
for ($i = 0; $i < $length; $i++) {
echo $old2_array[$i], "<br />";
};
...which produces:
604
Notice: Undefined offset: 1 in /index.php on line 128
I understand that php echo do not handles arrays directly but you have to use a syntax that includes curvy brackets if Im right.
if I were you, I would use php var_dump(array_here)
or load the array variable into a single variable first before echo it
The answer is not to have that second line. Once I remove this:
$old2_array = mysqli_fetch_array($old2);
Then the following loop works:
while( $reg = $old2->fetch_object() ) {
echo $reg->region_id . "<br>";
}
For some reason, the mysqli_fetch_array() statement screws with following commands.

Undefined offset 0

I have problem with this error script.
private function setCentroidCluster(){
for ($i=0;$i<count($this->centroidCluster);$i++){
$countObj = 0;
$x = array();
for ($j=0;$j<count($this->objek);$j++){
if ($this->objek[$j]->getCluster()==$i){
for ($k=0;$k<count($this->objek[$j]->data);$k++){ // Error
$x[$k] += $this->objek[$j]->data[$k];
The error is:
Notice: Undefined offset: 0
Notice: Undefined offset: 1
The error in line:
$x[$k] += $this->objek[$j]->data[$k];
First:
$x is an empty array. You want to add something at index $k. This is undefined. You need to define at least something there. There's a diff between auto-assigning array values and incrementing an existing array element:
for ($k=0;$k<count($this->objek[$j]->data);$k++){
if ( !isset($x[$k]) )
$x[$k] = 0; // depending on the type of data[$k] !!!
$x[$k] += $this->objek[$j]->data[$k];
}
should do the trick.
And as a recommendation, make yourself familiar with foreach:
foreach ($this->objek as $obj => $dat )
{
if ( $obj->getCluster() == $i )
{
foreach ( $dat as $datelem )
....
etc.

I'm receiving Undefined offset error

Why do I receive Undefined offset error? I'm trying to add 10,20,20 for each element in array. Please help. Thanks in advance
<?php
$arr = array("a","b","c");
$counter = 0;
$status = array();
foreach($arr as $a){
$status[$counter] += 10;
$status[$counter] += 20;
$status[$counter] += 20;
echo $status[$counter]."<br>";
$counter ++;
}
?>
Error:
Notice: Undefined offset: 0 in C:\xampp\htdocs\test\index.php on line 6
300
Notice: Undefined offset: 1 in C:\xampp\htdocs\test\index.php on line 6
300
Notice: Undefined offset: 2 in C:\xampp\htdocs\test\index.php on line 6
300
`
you are trying to add 10 in a undefined array element in this line:
$status[$counter] += 10;
try like this:
$arr = array("a","b","c");
$counter = 0;
$status = array();
foreach($arr as $a){
$status[$counter] = 10;//assign first
$status[$counter] += 20; //concate with assigned element
$status[$counter] += 20;
echo $status[$counter]."<br>";
$counter ++;
}
it should not provide any notices.
In your code, $status is an empty array, so when you attempt to add something to an undefined index you will see that notice (only for the first time).
To initialize $status as an array with values 0 based on the number of elements in $arr:
$status = array_fill(0, count($arr), 0);
You're using an 'add and assign' operator.
If you just want to assign a value then
$status[$counter] = 10;
Will work just fine.
However, you're asking PHP to add something to an existing element in your array, but there's no element there yet since you haven't initialised it. Just initialise your array before you start your loop.

Undefined offset 0, but still gives me correct result

$options = array('health', 'strength', 'agility', 'stamina', 'defence');
$total = array();
foreach ($options as $value)
{
foreach ($objects as $object)
{
$total[$value] += $object->$value;
}
}
var_dump($total);
I have some objects in an array called $objects. It's giving the data to the $total as it should do.
But by some reason it's whining about some undefined offset.
Notice: Undefined index: health in C:\wamp\www\objbattle\index.php on line 32
Line 32 is: $total[$value] += $object->$value;
Why!? And how do I get rid of it?
Because $total['health'] doesn't exist on the first iteration.
It's because you try to increment an uninitialized value. Try initalize them first.
$total = array('health' => 0, 'strength' => 0, 'agility' => 0, 'stamina' => 0, 'defence' => 0);
The key 'health' doesn't exist of the first loop through the for each. As a result you will get the notice.
You can do:
$total[$value] = 0;
Right above the second foreach.
Or, you could do it the lazy way and suppress the NOTICE:
error_reporting(E_ALL ^ ~E_NOTICE);
Why!?
I think because Sets which PHP errors are reported.
I expect because you are trying to increment an un-initialized value...
try assigning on the first round instead, like this:
$total[$value] = $object->$value;
or more simply, initializing the array values...
$total = array();
$total['health'] = 0;
etc...
or do it like #josmith suggests in his answer

Categories