This code helps me find the minimum of certain sums, so I set a function as the one below:
function GetMin($s)
{
for($j=1;$j<=count($s)-1;$j++)
{
$min=$s[1];
if($s[$j] < $s[$j+1] && $s[$j] < $min)
{
$min=$s[$j];
}
else {continue;}
}
echo $min;
}
$enc=".129.25.24.154.546.214.142.254.256";
$ar=explode(".",$enc);
for($i=0;$i<count($ar)-1;$i+=3)
{
$s[$i+1]=$ar[$i+1]+$ar[$i+2]+$ar[$i+3];
echo $s[$i+1]."<br>";
}
GetMin($s)
The code should work this way:
Calculates the sum of every 3 terms of the array and store it in a term of another array called "s", the code is working well, until I try to find the minimum, it prints the minimum but with the error below:
Notice: Undefined offset: 2 in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\decrypt.php on line 9 Notice: Undefined offset: 2 in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\decrypt.php on line 9 Notice: Undefined offset: 3 in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\decrypt.php on line 9 178
It looks like you're trying to get the lowest value in an array of values, so I've changed your GetMin() function to work with the non-consecutively indexed array that you create prior to calling GetMin(). This should work for you:
<?php
function GetMin($s){
$min = NULL;
foreach($s as $v){
if(is_null($min)){
$min = $v;
} elseif($v < $min){
$min = $v;
}
}
return $min;
}
$enc = ".129.25.24.154.546.214.142.254.256";
$ar = explode(".",$enc);
$ar_count = count($ar);
for($i=0;$i<$ar_count-1;$i+=3)
{
$s[$i+1]=$ar[$i+1]+$ar[$i+2]+$ar[$i+3];
echo $s[$i+1]."<br>";
}
echo GetMin($s);
Related
Here is the code that I wrote:
class init{
private $time_spent;
public function __construct(){
$this->time_spent = array(); //<- caching variable
}
function get_course_unit_time_spent( $user_id,$course_id,$unit_id ){
if(empty($this->time_spent) || empty($this->time_spent[$course_id]) || empty($this->time_spent[$course_id][$unit_id])){
$this->time_spent[$course_id][$unit_id] = get_user_meta($user_id,'time_spent_'.$course_id.'_'.$unit_id,true);
if( empty($this->time_spent[$course_id][$unit_id]) ){
$this->time_spent[$course_id][$unit_id] = 0;
}
}
return $this->time_spent[$course_id][$unit_id];
}
function get_course_time_spent( $user_id,$course_id ){
if( empty($this->time_spent[$course_id]) ){
if(!bp_course_is_member($course_id,$user_id))
return 0;
$time_spent = 0;
$this->time_spent[$course_id] = array();
$course_curriculum = bp_course_get_curriculum( $course_id );
foreach ($course_curriculum as $key => $unit_id) {
if( is_numeric($unit_id) ){
$time_spent += $this->get_course_unit_time_spent( $user_id,$course_id,$unit_id );
}
}
$this->time_spent[$course_id] = $time_spent;
}
return $this->time_spent[$course_id];
}
}
There are lots of code but the issue is with the first function I wrote above, in two different places I am getting the above to warnings.
1) Warning: Cannot use a scalar value as an array on line 10 and 12 (in the above code).
2) Warning: Illegal offset type in isset or empty on line 8 (in the above code).
3) Warning: Illegal offset type on line 10, 11 and 12 (in the above code).
The second function is not throwing any error but whenever I use the 1st function or the second everytime the warning comes from the 1st function. I am not sure what is it, can someone help to correct it ?
UPDATE: Updated the code and now the first warning is gone but still getting the illegal ofset error.
Your calling get_course_time_spent(), which creates an array for
$this->time_spent[$course_id][$unit_id] in
get_course_unit_time_spent()
Then your setting $this->time_spent[$course_id] as an int once the foreach loop has finished.
Then on the next call to get_course_unit_time_spent your checking
as if its an array empty($this->time_spent[$course_id][$unit_id])
I get these error from nowhere... What is wrong?
Warning: Division by zero in
/home//public_html/wp-content/plugins//includes/classes/controller/SearchPageController.php
on line 60
Warning: Invalid argument supplied for foreach() in
/home/vineanimals/public_html/wp-content/plugins/*/includes/classes/controller/ImportPageController.php
on line 70
Warning: array_merge(): Argument #1 is not an array in
/home//public_html/wp-content/plugins//includes/classes/controller/AImportPageController.php
on line 75
line codes:
line 60: $last = ceil($load_products_result['total'] / $load_products_result['per_page']);
line 70-75:
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}
A) Check your SQL query or Array feed
B) Then change
$last = ceil($load_products_result['total'] / $load_products_result['per_page']);
to:
$last = ( isset($load_products_result['per_page']) ? ceil($load_products_result['total'] / $load_products_result['per_page']) : 0)
C) and change the foreach as below:
if (is_array($product['sku_products']['variations'])){
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
if (isset($product['description']) && is_array($product['all_images']) && is_array($this->woocommerce_model->get_images_from_description($product['description'])) )
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}
This question already has answers here:
Illegal string offset Warning PHP
(17 answers)
Closed 7 years ago.
I have this chunk of PHP code which is giving me the error:
Warning: Illegal string offset 'mod_modulecode' in C:\xampp\htdocs\MP\multi\functions.php on line 29
This is the code that the warning is relating to:
function set_rights($menus, $menuRights) {
$data = array();
for ($i = 0, $c = count($menus); $i < $c; $i++) {
$row = array();
for ($j = 0, $c2 = count($menuRights); $j < $c2; $j++) {
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
if (authorize($menuRights[$j]["rr_create"]) || authorize($menuRights[$j]["rr_edit"]) ||
authorize($menuRights[$j]["rr_delete"]) || authorize($menuRights[$j]["rr_view"])
) {...................'
Any help would be greatly appreciated.
What PHP is saying is that at some iteration in
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
$menus[$i] is a string, not an array, and you're trying to access it like an array containing the key mod_modulecode.
To help track down this bug, I'd suggest:
if(is_string($menus[$i]) {
var_dump('string bug','i',$i,'j',$j,'$menus[$i]',$menus[$i]);
}
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
Or even better yet, if you have xdebug installed, which will show local variables when an uncaught exception is thrown: throw new Exception('string bug');
I have some problem with this code. Warning: Illegal offset type in Line 22
$this->word[$kata][]=array($i,$j);
and the full code is below
private $jmldoc = 0; private $word = array();
public function getIndex($D) {
$this->jmldoc = count($D);
for($i=0; $i<$this->jmldoc; $i++) {
$pp = new prePro($D[$i]);
$kata = $pp->tokenize();
$n = count($kata);
for($j=0; $j<$n; $j++) {
$this->word[$kata]=array($i,$j);
}
}
}
Can you help me to fix it?
You are passing an array, not a string/integer index to your $this->word.
//I suppose from the context of your code that $kata is an array also
//so if that's true, it can't be used as an index
$this->word[$kata][]=array($i,$j);
Keep in mind that $this->word is an array. So probably there is something wrong with your program logic. To fix this, use an integer or string to access the elements of an array.
I've got Undefined Offset error, but I don't know what's wrong.
Here's the code
<?php
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$fileName = "$documentRoot/Tutorials/PHP/Assignments/Assignment 3/data/quote.txt";
$filePointer = fopen($fileName, 'r');
$lineCounter = 0;
$display = "";
while(true)
{
$line = fgets($filePointer);
list($firstName, $lastName, $contactMethod, $phoneMail, $resideCity, $comments) = explode("|", $line);
if (!isset($comments))
{
$comments = "";
}
$lineCounter++;
if(feof($filePointer))
{
break;
}
if($lineCounter % 2 == 0)
{
$style = "style = 'background-color:white';";
}
else
{
$style = "style = 'background-color:lightgray';";
}
//Write to table
print"<tr $style>";
print"<td>$firstName</td>";
print"<td>$lastName</td>";
print"<td>$contactMethod</td>";
print"<td>$phoneMail</td>";
print"<td>$resideCity</td>";
print"<td>$comments</td>";
print"</tr>";
}
fclose($filePointer);
?>
I added:
if (!isset($comments))
{
$comments = "";
}
Because I assume the offset error appears because I didn't type in anything in comments. However, I still get this error.
Please help.
Here's the error message:
Notice: Undefined offset: 5 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 4 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 3 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 2 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 1 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
The problem is before your isset... so when you do explode and pass results to list... that expects 6 parametes only receive 5 or less and you get the error.
I suggest the following replacement:
$line = fgets($filePointer);
$data=explode("|", $line);
while(count($data)<6) $data[]="";
list($firstName, $lastName, $contactMethod, $phoneMail,
$resideCity, $comments) = $data;
That will add enough data for list to set vars.
and should work!
Happy coding!