Got fatal error in PHP array - php

$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).

Related

fputcsv: Empty rows in csv file after each array

I am trying to write a csv files with values from two arrays. Here's the code:
<?php
fopen('output.csv','w');
$merged_fields = array_combine($authors, $ids);
$range = count($merged_fields);//gives number of elements in array
$name = array_keys($merged_fields);
$id = array_values($merged_fields);
$name = str_replace(',',', ',$name);
for ($a = 0; $a <=$range; $a++) {
//filter out rows where id = '000000'
if($id[$a] == '000000'){
continue;
}
fputcsv($fp, array_filter(array($name[$a], $id[$a])));
}
}
fclose($fp);
?>
If the original array was "Smith, John" => "888888", "Smith, Jane"=>"777777" , and the next array is "Jones,John"=>"999999" there is a space after the line after each array, so in the csv the output ends up:
"Smith, John",888888
"Smith, Jane",777777
"Johnes, John",999999
I will use this csv to import data and need to get the extra lines out of there. I have tried applying "array_filter" to other parts of the array as it appears there is an empty element in there somewhere, but that hasn't worked.
The blank line is produced because you iterate one time too many over the array. You will have a few warning messages like these:
E_NOTICE : type 8 -- Undefined offset: ...
But the code will not be interrupted by that and so an empty value will be output by fputcsv, resulting in the empty line. If then you call the same code again to append to the generated file, the empty line will separate the two batches of output.
Fix this by replacing <= in the following line:
for ($a = 0; $a <= $range; $a++) {
so you get:
for ($a = 0; $a < $range; $a++) {
Remember that if an array has $range elements, the first element is at index 0 and the last at index $range-1, unless you have explicitly defined your array otherwise.

Can't do implode method to delimite number range

I have a range of number, say that it from 1 to 10.
//$front_id = 1;
//$until_id = 10;
foreach (range($front_id, $until_id) as $number)
{
print_r($number)
//12345678910
}
It print a range of number between two variables as expected.
However, in the print result is it combine all number without any delimiter.
So, I tried to add a delimiter of comma here:
$numbers = implode(',', $number);
But it doesn't work.
Message: implode(): Invalid arguments passed
You can store $number in array. Right now $number is not an array. That why you cannot implode. Define array first. Then inside foreach() you can store every $number in $numbers array.
$front_id = 1;
$until_id = 10;
$numbers = array();
foreach (range($front_id, $until_id) as $number)
{
$numbers[] = $number;
}
echo implode(',',$numbers);

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.

get key and value of second last element of an array

i have an array of undefined size, for example :
<?php
$array["foo"] = 86 ;
$array["bar"] = 49 ;
$array["matt"] = 96 ;
?>
i don't want to disturb array's internal pointer , but want to get a COPY of second last value of array instead.
I don't know, why you use a map, when in fact you want an ordered list instead, but
$tmp = array_values($array);
echo $tmp[count($tmp) -2];
should do it. With php5.4 this should work either
echo array_values($array)[count($array)-2];
I'm not sure what size your array is planned for, so copying all values into a separate array might not be a good idea.
The following code slices out an array of length 1 just from the second last position and sets $key and $value.
$pair = array_slice($array, -2, 1, true);
$key = key($pair);
$value = current($pair);
PS: Should probably be put into a simple separated function?!
You can do it this way.
$array["foo"] = 86 ;
$array["bar"] = 49 ;
$array["matt"] = 96 ;
$x = count($array);
foreach($array as $row)
{
if($x == 2)
{ $secondLast = $row;}
$x--;
}
echo $secondLast;
Because you are using associative array.

Undefined offset: 0 Error in preg_match

In the function below, how should I initialize $matches to avoid throwing undefined index on the commented line?
function save_rseo_nofollow($content) {
$my_folder = get_option('rseo_nofollow_folder');
preg_match_all('~<a.*>~isU',$content["post_content"],$matches);
for ( $i = 0; $i <= sizeof($matches[0]); $i++){
if ( !preg_match( '~nofollow~is',$matches[0][$i]) //ERROR UNDEFINED OFFSET HERE!
&& (preg_match('~' . $my_folder . '~', $matches[0][$i])
|| !preg_match( '~'.get_bloginfo('url').'~',$matches[0][$i]))){
$result = trim($matches[0][$i],">");
$result .= ' rel="nofollow">';
$content["post_content"] = str_replace($matches[0][$i], $result, $content["post_content"]);
}
}
return $content;
}
if ( isset($matches[0][$i]) && !preg_match( '~nofollow~is',$matches[0][$i])...
You can check if this offset... is set.
Edit : or :
for ( $i = 0; $i <= sizeof($matches[0])-1; $i++){
because, let's say your $matches[0] array have 10 choices, it'll go from 0 to 9 and not 10 (which is the size of your array) you follow ?
if(isset($matches['0'][$i]))
{
$myVariable= $matches['0'][$i];
}
IF ISSET checking has a weird effect, making that index readable.
In my case I could see the array in print_r but the undefined index error kept me from using it. After 2 hours of debugging I happended to put this and now it works!!!
Add $matches = array(); before you use it.
Also, you might want to check to make sure the array is filling how you expect it to. Getting undefined offset means the array in question doesn't have the requested key, so either it's not using the keys you're expecting it to use, or it's not filling the array (you'll also probably want to add in a check to make sure there's actually stuff in your array before you try accessing it).

Categories