I get a website as a cURL response, and I pass it to this function. Then I itterate through the string, doing some processing. It works, but I get this error:
Notice: Uninitialized string offset: 75817 in
/var/www/html/wp-content/plugins/crg-daily/lib/CragslistRawDumpProcessor.class.php
on line 7
Here is the code:
public function rollSausage($dump){
$anchorArray = array();
$dumpLength = strlen($dump);
$skipToLetter = 1;
while($skipToLetter < $dumpLength){
$skipToLetter++;
$letter = $dump[$skipToLetter];
...
}
}
Any ideas? I think it has something to do with the type of string being submitted. It is a raw cUrl response. I'm scraping a web page.
Increment your $skipToLetter after you use it (preferably at the end of the while loop). And you might also start at 0, not 1
$skipToLetter = 0;
while($skipToLetter < $dumpLength){
$letter = $dump[$skipToLetter];
...
$skipToLetter++;
}
}
Here's the reason: assume you have a string with length of 4. This means that the last index in the string is 3. Your index goes up to 3. It gets compared in the while loop (3<4)? and the answer is true. The code enters the while loop and increments the value of the index which will be greater than the last index of the string, thus causing the warning.
Updated your code...
public function rollSausage($dump){
$anchorArray = array();
$dumpLength = strlen($dump);
$skipToLetter = 1;
while($skipToLetter < $dumpLength){
$skipToLetter++;
if( siset( $dump[$skipToLetter]) )
$letter = $dump[$skipToLetter];
...
}
}
}
Related
I have the following:
$results = array();
$results[0] = $results[0] + $value;
which causes the warning in XAMPP
Warning Notice: Undefined offset;
because $results[0] does not have an intial value. Hosting the site assumes the value 0 on the first pass but locally XAMPP throws the warning.
I can bypass this by establishing the value of $results[0] using:
$results = array();
$results[0] = 0;
$results[0] = $results[0] + $value;
But if there is an indefinate number of keys (perhaps a $loop from 0 to n) can I establish the array with 0 values by default, without explicitly specifying 0 for each key, so that the following:
$results = array();
$results[$loop] = $results[$loop] + $value[$loop];
will work for any value of $loop with warning in XMAPP?
Best way to resolve this issue it to use isset() function. It will allow you to check the existing of an element in the array thus avoiding the error message.
Example
$results = array();
if((isset($results[$loop])) && (isset($value[$loop]))){
$results[$loop] = $results[$loop] + $value[$loop];
}
Hope this help you.
I am working on Do While loop in my project its working fine first time.
Before while statement, I assigned a value to an array I could able to print the array successfully at bottom of the code, BUT its become 0 when I check at top of the loop.
Code:
$looparray = array();
$loopend = 0;
$arraymer = array();
$poolafirtsid = $previous_array_values; //previous array values
do {
if (sizeof($looparray) == 0) {
$firstsponarray = $poolafirtsid;
} else {
$firstsponarray = $looparray;
}
$firstsponarray = getUserArray($poolafirtsid);
//get user arraylist of first
foreach ($firstsponarray as $avalue) {
$rooparray = membercount($avalue);
$bsponarray = getUserArray($avalue);
//get second users arraylist 9
if (sizeof($bsponarray > 0)) {
$barraymer = array_merge($barraymer, $bsponarray);
}
$aarraylist[$avalue] = $rooparray;
}
$asmallestsponid = getSmallestID($aarraylist);
//get smallest id in the array
if (membercount($asmallestsponid) < 3) {
$loopend = 1;
} else {
global $pooldata;
if (count($barraymer) > 0) {
$pooldata = $barraymer;
}
print_r($pooldata);
}
} while ($loopend == 1);
When I print in else its working but I am unable to print starting of do loop its showing array size is 0
I will ignore all the name issues but address your while loop issue:
$loopend =0;
do {
...
if(membercount($asmallestsponid)<3) {
$loopend = 1;
}else{
...
}
while ($loopend == 1);
There are 2 options:
The if condition is true: if so, $loopend will get 1 so the loop continue (which not seem fit the call him "loop end" but what ever...)
The if condition is false: then the $loopend stay the same (init as 0) so the loop will stops
IMHO - this will simplify your loop:
do {
...
while (membercount($asmallestsponid)<3);
We don't know what membercount($asmallestsponid) returns, but it appears (most likely) that on the 1st pass it gets into
} else {
$looparray = $arraymer;
//print_r($looparray);
}
the value of $loopend doesn't change, i.e. remains at 0 and on the first pass it compares 0 to 1 and decides to exit the do {} while loop as 0 != 1
There are several problems with your code. First of all, this does nothing:
if (sizeof($looparray)==0) {
$firstsponarray= $poolafirtsid;
} else {
$firstsponarray= $looparray;
}
since the very next line after that piece of code is:
$firstsponarray= getUserArray($poolafirtsid);
which overrides any prior assignment of $firstsponarray.
Second, the value of $looparray doesn't change at all in the do loop, so it'll always be an empty array. I found this line:
$rooparray=membercount($avalue);
which I assume is a typo and the correct line is $looparray=membercount($avalue);. Same with the line $aarraylist[$avalue]=$rooparray;. However, changing that also does nothing since $firstponarray will never be equal to $looparray for the reason I described at the top.
Try debugging your code first, and if the problem persists post the updated code.
I'm trying to perform two calculations in one function and return the values of both calculations to seperate variables. I think i have everything setup nearly correct but i'm getting an error message which i'll post after the code. I could easily write two functions to do what I want but i'd rather have it all in one function since the function is initiated by the click of one button.
function sg_gc1($gv_1, $g_1) {
$gv_1 = $gv_1 - 1;
Return $gv_1;
$g_1 = $g_1 + 20;
Return $g_1;
}
// This second part in my code is mostly irrelevant to my question but it helps paint the picture.
if (isset($_POST['Calculate']) && ($_SESSION['gv_1'] != 0) ) {
$_SESSION['gv_1'] = sg_gc1($_SESSION['gv_1']); // line 55
$_SESSION['g_1'] = sg_gc1($_SESSION['g_1']); // line 56
} // This statement assigns the new calculated values to my existing session variables and it worked just fine before i tried adding in the second argument
Warning: Missing argument 2 for sg_gc1(), called in F:PATH on line 55 and defined in F:PATH on line 6
Warning: Missing argument 2 for sg_gc1(), called in F:PATH on line 56 and defined in F:PATH on line 6
So the error is happening inside the function which leads me to believe the function is not set up properly for two arguments. It works just fine with 1 argument if i were to remove all the g_1 parameters.
Call function like this, because your function accepts two arguments and you are passing only one.
sg_gc1($_SESSION['gv_1'],$_SESSION['g_1']);
and in function you have to return like below. Your second return would never get executed.
function sg_gc1($gv_1, $g_1) {
$gv_1 = $gv_1 - 1;
$g_1 = $g_1 + 20;
return array($gv_1,$g_1);
}
Calling function part
if (isset($_POST['Calculate']) && ($_SESSION['gv_1'] != 0) ) {
list($_SESSION['gv_1'],$_SESSION['g_1']) = sg_gc1($_SESSION['gv_1'],$_SESSION['g_1']);
echo $_SESSION['gv_1']."--".$_SESSION['g_1'];//for checking purpose
}
You can't return two variables in a function. Return an array instead:
function sg_gc1($gv_1, $g_1) {
$gv_1 = $gv_1 - 1;
$g_1 = $g_1 + 20;
return array($gv_1, $g_1);
}
Now to retrieve the values, you can simply access the array indices:
$arr = sg_gc1($_SESSION['gv_1'], $_SESSION['g_1']);
$gv_1 = $arr[0];
$g_1 = $arr[1];
Or you could use the list construct to do it in one line:
list($gv_1, $g_1) = sg_gc1($_SESSION['gv_1'], $_SESSION['g_1']);
I need to php count returns 0 for empty using server or mysql. Actually, in my case lot of places i have used count function, when i am upload my code to client server php count returns 1 for empty.So that it is create many issue. If any solution then please reply me. I am not want to change all the places where count used.
If i am not getting you wrong, you are passing a non-array element to count.
Look at following example code..
Code 1:
$var = false;
echo count($var); // print 1
Code 2:
$var = array();
echo count($var); // print 0
Code 3:
$var = array('');
echo count($var); // print 1
So better you should check whether the element is an array or not.
if(is_array($var))
{
$var = array_filter($var);
echo count($var);
}
else
{
echo 0;
}
See Codepad.
I am getting the following error from the method presented below:
Notice: Uninitialized string offset: 5 in /path/to/file.php on line 30 Fatal error: Cannot access empty property in path/to/file.php on line 30
private function parse($xml, $index = '') {
echo count($xml->children()); //outputs 6
$count = 0;
foreach ($xml->children() as $key => $value) {
$this->$key[$count] = array();
$count++;
}
}
Any ideas why if I build an multi-dimensional in this way it results in an error?
If I change the assignment to:
$this->$key = array($count = > array());
This simply re-assigns the property each loop.
Thanks
Rich
Imagine you've got a string:
$string = 'abc`;
Doing substring access (which looks like array) will return you the character:
echo $string[2]; # c
Or you get your error when you're out of the index:
echo $string[3]; # null + warning
So now accessing a member of your object $this dynamically:
$this->$string[2]; # access $this->c
However this one breaks hardly:
$this->$string[3]; # access $this->null (not possible)
This gives you your fatal error of an empty property, a property with no name.
This explain what happens in your code, you have not told what you're trying to do so I hope this information will help you to continue with writing your parse function.
You should try to create the array before filling it.
I.e. $this->key = array();
That is, before looping through the XML elements.