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.
Related
How can I have same data stored as much as provided number?
Example
Store abc, 10 times
both abc and 10 are coming from form request
Code
nonuiqueAmount: 10
nonuiqueSerial: "abc"
if(!empty($request->input('nonuiqueSerial'))) {
foreach($request->input('nonuiqueAmount') as $item) { // this returns error
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
}
Error:
Invalid argument supplied for foreach()
You should use a for loop:
// nonuiqueAmount: 10
// nonuiqueSerial: "abc"
if (!empty($request->input('nonuiqueSerial'))) {
for ($i = 0; $i < $request->input('nonuiqueAmount', 0); ++$i) { // I've added the zero as a default value to prevent unnecessary loops
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
}
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. w3schools docs
Your nonuiqueAmount is an int. I would suggest simply stick with basic for loop
for ($x = 0; $x < $request->input('nonuiqueAmount'); $x++) {
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
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 have this function that strips illegal characters. I found it here http://php.net/manual/en/function.strtr.php, function fixoutput($str)
So, this is my code.
<?php
$stuff = 'Foo ◻◻◻◻◻◻◻◻◻◻◻◻';
function fix_output($str){
$newstr = '';
$good[] = 9; #tab
$good[] = 10; #nl
$good[] = 13; #cr
for($a=32;$a<127;$a++){
$good[] = $a;
}
$len = strlen($str);
$strs = array();
for($b=0;$b < $len+1; $b++){
if(in_array(ord($str[$b]), $good)){
$newstr .= $str[$b];
}//fi
}//rof
return $newstr;
}
echo fix_output($stuff);
echo '<br>'.$stuff;
And I have this output.
Notice: Uninitialized string offset: 40 in /<directory>/foo/foo.php on line 17
Foo
Foo ◻◻◻◻◻◻◻◻◻◻◻◻
I want a fix for this notice.
I'm having this notice because is $str a string in this context, not an array. I am are trying to treat it like an array, which doesn't work. I'm having trouble in creating a fix for this, could you guys lend me some ideas? Thanks a bunch!
Please mention the things that are unclear.
I have this piece of code which works well if the ['extensions'] array exists.. but if the array does not exist then it returns errors. How can I fix this code to not return anything if the extensions array does not exist?
-[UPDATE-
Sorry i had previously inserted the wrong code.. here is the proper code i need checked.
$oid = array('id-ce-subjectAltName');
$count = count($cert['tbsCertificate']);
for($i = 0; $i < $count; $i++) {
if(array_key_exists('extensions', $cert['tbsCertificate']) &&
in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n",'<b>[SANs]</b>',"\n","\n";
}
}
I get this warning when ['extensions'] does not exist - I would like to prevent any warnings from being generated.
Notice: Undefined index: extensions in
C:\xampp\htdocs\labs\certdecode\certdecode.php on line 142
AFTER UPDATE:
How is the structure of the array?
You count the number of items in $cert['tbsCertificate'],
but in your loop, your $i is for the number of items in $cert['tbsCertificate']['extensions'].
So maybe you try to do something like this?:
$oid = array('id-ce-subjectAltName');
if (array_key_exists('extensions', $cert['tbsCertificate']) &&
is_array($cert['tbsCertificate']['extensions'])
) {
$count = count($cert['tbsCertificate']['extensions']);
for ($i = 0; $i < $count; $i++) {
if (in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n", '<b>[SANs]</b>', "\n", "\n";
}
}
}
okay, seems to work by adding an isset() at the start of the code:
if(isset($cert['tbsCertificate']['extensions'])) {
thanks guys!
I have a class Tpl to mount template with this function (template.php)
function Set($var, $value){
$this->$var = $value;
}
A php file that call the function, example (form.php):
$t->Set("lbAddress","Address");
And a html file with the template with tags (template.html)
<tr><td>[lbAdress]</td></tr>
To print the html I have this function (template.php) - the notice points to this function
function Show_Temp($ident = ""){
// create array
$arr = file($this->file);
if( $ident == "" ){
$c = 0;
$len = count($arr);
while( $c < $len ){
$temp = str_replace("[", "$" . "this->", $arr[$c]);
$temp = str_replace("]", "", $temp);
$temp = addslashes($temp);
eval("\$x = \"$temp\";");
echo $x;
$c++;
}
} else {
$c = 0;
$len = count($arr);
$tag = "*=> " . $ident;
while( $c < $len ){
if( trim($arr[$c]) == $tag ){
$c++;
while( (substr(#$arr[$c], 0 ,3) != "*=>" ) && ($c < $len) ){
$temp = str_replace("[", "$" . "this->", $arr[$c]);
$temp = str_replace("]", "", $temp);
$temp = addslashes($temp);
eval("\$x= \"$temp\";"); //this is the line 200
echo $x;
$c++;
}
$c = $len;
}
$c++;
}
}
}
If the template .html have a line [lbName] and I don't have the line $t->Set("lbName","Name"); at the php code, I receive the error PHP Notice: Undefined property: Tpl::$lbName in ../template.php(200) : eval()'d code on line 1. The solution that I found is add lines like $t->Set("lbName","");, but if I have 50 tags in HTML that I don't use in PHP, I have to add all 50 $t->Set("tag_name","");. The error occurred after migrate to the PHP 5.
Can someone help me? Thanks
Perhaps a better way still would be not to rely on dynamic evaluation through eval (it's generally best to avoid eval where possible), but to replace [lbName] with the value stored in the object directly as and when needed. If you can replace [lbName] with $this->lbName, surely you can also replace it with the value of lBName that you've looked up on-the-fly?
To answer your original question, however:
If I understand correctly, you're setting the values like this:
$t->Set('foo', 'bar');
And – effectively – getting them like this:
$t->foo;
If so, you could implement a __get method to intercept the property references and provide your own logic for retrieving the value; e.g.:
public function __get($key)
{
// You can adapt this logic to suit your needs.
if (isset($this->$key))
{
return $this->$key;
}
else
{
return null;
}
}
In this case, you'd probably be better off using an associative array as the backing store, and then using __get and __set to access it; e.g.:
class Template
{
private $values = array();
public function __get($key)
{
if (array_key_exists[$key, $this->values])
{
return $this->values[$key];
}
else
{
return null;
}
}
public function __set($key, $value)
{
$this->values[$key] = $value;
}
}