1

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

i am getting "ranksection" array at run time and after implementing ksort on "ranksection" i wanna move its data upward on null index as i am printing ranksection before moving its data upward if there were any free array i am successfully getting what i want but it also giving error "Undefined Index" i dont know why my code is,

$sortvar = count($ranksection);
$seqnum = 0;

for ($var = 0; $var <= $sortvar; $var++) {
  if ($ranksection[$var] !=  null) {
    $sequence[$seqnum] = $ranksection[$var];
    $seqnum++;
  }
}

print_r($sortvar);
print_r($ranksection);
print_r($sequence);

the result is,

3

Array ( [1] => Self Introduction [2] => Experience in Econometrics and multivariate S [3] => Experience ) 

Array ( [0] => Self Introduction [1] => Experience in Econometrics and multivariate S [2] => Experience )

Hopes for your suggestions

Community
  • 1
  • 1
Syed Raza
  • 331
  • 2
  • 13
  • 35
  • This error occurs only if you try to access array element that doesnt exist. for example if $array has 3 elements, then if you try to access array element $array[3] and so on. – WatsMyName Aug 10 '12 at 04:55
  • @Sabin array has 3 element so it will find it and the second thing is for loop it will not run after 3 index value – Syed Raza Aug 10 '12 at 05:08
  • @SyedRaza, You have problem in here for ($var = 0; $var <= $sortvar; $var++) Should be for ($var = 0; $var < $sortvar; $var++) – WatsMyName Aug 10 '12 at 05:10

4 Answers4

3

See your print_r section of second array it starts with index 1 and your $var assigned to 0. Now here you are trying to access the 0th index. that is why you're getting this error.

Try to use foreach

foreach($ranksection as $key => $value ) {
  if ($ranksection[$key] !=  null) {
    $sequence[$seqnum] = $ranksection[$key];
    $seqnum++;
  }
}
Vins
  • 8,849
  • 4
  • 33
  • 53
  • thanks for your reply it help me what is the line "foreach($ranksection as $key => $value )" doing actually ??? – Syed Raza Aug 10 '12 at 05:11
  • 1
    http://php.net/manual/en/control-structures.foreach.php Read the documentation. – Vins Aug 10 '12 at 05:14
0

Do this as the condition for your for loop:

for ($var = 0; $var <= $sortvar - 1; $var++) {

The -1 is important since arrays start from 0 and go the length of the array, minus one.

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
0

You ought to be using count()-1 in your for loop:

$sortvar = count($ranksection) -1;
$seqnum = 0;

for ($var = 0; $var <= $sortvar; $var++) {

Or, use less than (without less than equal to) as the operator:

for ($var = 0; $var < $sortvar; $var++) {
Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
0

You're going past the end of your array because:

$sortvar = count($ranksection); // This is 4

Array indexs start at 0, but count returns the number where 1 is the first item, not 0. Do this to fix it:

$sortvar = count($ranksection) - 1;

Or change <= to <

for ($var = 0; $var < $sortvar; $var++) {   
    if ($ranksection[$var] !=  null) {   
          $sequence[$seqnum] = $ranksection[$var];   
          $seqnum++;   
    }   
}
siva.k
  • 1,344
  • 14
  • 24