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');
Related
I am trying to scrape the table from the following link and place into an array.
https://www.tradingview.com/markets/currencies/cross-rates-overview-prices/
I have tried various ways, just cannot get it right.
<?php
$htmlContent = file_get_contents("https://www.tradingview.com/markets/currencies/cross-rates-overview-prices/");
$DOM = new DOMDocument();
$DOM->loadHTML($htmlContent);
$Header = $DOM->getElementsByTagName('th');
$Detail = $DOM->getElementsByTagName('td');
//#Get header name of the table
foreach($Header as $NodeHeader)
{
$aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
}
//print_r($aDataTableHeaderHTML); die();
//#Get row data/detail table without header name as key
$i = 0;
$j = 0;
foreach($Detail as $sNodeDetail)
{
$aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
$i = $i + 1;
$j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
}
//print_r($aDataTableDetailHTML); die();
//#Get row data/detail table with header name as key and outer array index as row number
for($i = 0; $i < count($aDataTableDetailHTML); $i++)
{
for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
{
$aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
}
}
$aDataTableDetailHTML = $aTempData; unset($aTempData);
print_r($aDataTableDetailHTML); die();
This is the error output: (Note, there are quite a few lines of these errors)
Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 405 in C:\xampp\htdocs\Testing\scraper.php on line 6
Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 405 in C:\xampp\htdocs\Testing\scraper.php on line 6
Warning: Undefined variable $aDataTableDetailHTML in C:\xampp\htdocs\Testing\scraper.php on line 30
Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, null given in C:\xampp\htdocs\Testing\scraper.php:30 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Testing\scraper.php on line 30
Your help would be much appreciated.
There are two problems:
The errors are from validating the html. Take a look this answer how to deal with that.
The actual html where you are interested in (the table-data) are not present in the source-html. They are created via JavaScript somehow. To deal with that kind of pages you could use i.e. Selenium. See this answer on how to do that in php.
This question already has answers here:
Illegal string offset Warning PHP
(17 answers)
Closed 2 years ago.
The code below worked correctly in PHP 7.0, but after upgrading to 7.3 it now gives this warning:
PHP Warning: Illegal string offset on line 71
// Loop through routes
$array_result = "";
$array_index_result = "";
foreach($routes as $key => $route){
$route_parts = explode("/", $route);
$index = 0;
$match = TRUE;
// Reset array_result
$array_result = "";
foreach($route_parts as $route_part)
{
if(substr($route_part,0,1) != "$"){
if(isset($url_parts[$index])){
if($route_parts[$index]!=$url_parts[$index]) $match = FALSE;
}else{
$match = FALSE;
}
}else{
if(isset($url_parts[$index])){
$array_result[substr($route_parts[$index],1)] = $url_parts[$index];
}
}
if(isset($url_parts[$index])){
$array_index_result[$index] = $url_parts[$index];
$index++;
}
}
if($match && (count($route_parts) == count($url_parts)))
{
$Info = new ModRewriteInfo($array_result, $array_index_result);
return $Info;
}
}
// No match found
return FALSE;
Line 71 is
$array_result[substr($route_parts[$index],1)] = $url_parts[$index];
The code runs on PHP 7.0 but not in PHP 7.3. Why is that?
$array_result is a string.
$array_result = "";
In PHP 7.0 and before, assigning to an index of an empty string with [] would result in an array. From 7.1 onward, it stays a string and you get that warning.
See the documentation for backward incompatible changes in PHP 7.1: Assignment via string index access on an empty string
From the context of the rest of the code, it doesn't look like that variable should ever be a string, so you should initialize it with $array_result = []; instead.
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])
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
I'm trying to import data from excel to my database,
The database sucessfully inserted but an error offset : 6 show. Can someone tell me what's wrong with my array?
Controller :
$user = $this->input->post('user');
$path = "assets/excel/Book1.xls";
$this->load->library('excel_reader');
$this->excel_reader->read($path);
$data = $this->excel_reader->sheets[0] ;
$dataexcel = Array();
for ($i = 2; $i <= $data['numRows']; $i++) {
if($data['cells'][$i][2] == '')
break;
$dataexcel[$i-2]['item_id'] = $data['cells'][$i][2];
$dataexcel[$i-2]['measure_cd_from'] = $data['cells'][$i][3];
$dataexcel[$i-2]['qty_from'] = $data['cells'][$i][4];
$dataexcel[$i-2]['measure_cd_to'] = $data['cells'][$i][5];
$dataexcel[$i-2]['qty_to'] = $data['cells'][$i][6];
}
$this->prodConv->insertConvtData($dataexcel);
$this->load->view('formSukses');
Model :
function insertConvtData($dataarray)
{
for($i=0;$i<count($dataarray);$i++){
$data = array(
'item_id'=>$dataarray[$i]['item_id'],
'measure_cd_from'=>$dataarray[$i]['measure_cd_from'],
'qty_from'=>$dataarray[$i]['qty_from'],
'measure_cd_to'=>$dataarray[$i]['measure_cd_to'],
'qty_to'=>$dataarray[$i]['qty_to']
);
$this->db->insert('tb_t_product_convertion',$data);
}
}
Try to use your for loop as like that:
$j = 2; // initialize an integer with 2 for values
for ($i = 0; $i <= $data['numRows']; $i++) {
if($data['cells'][$i][2] == '')
break;
$dataexcel[$i]['item_id'] = $data['cells'][$j][2];
$dataexcel[$i]['measure_cd_from'] = $data['cells'][$j][3];
$dataexcel[$i]['qty_from'] = $data['cells'][$j][4];
$dataexcel[$i]['measure_cd_to'] = $data['cells'][$j][5];
$dataexcel[$i]['qty_to'] = $data['cells'][$j][6];
$j++;
}
Actually you want to use $i in both array key and array value and you try to use with only one incremental variable.
When your loop work on last iteration it will return you the undefined.
So, its better to use an another incremental variable for array value.
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.