Notice: Undefined offset: 49

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • atang
    New Member
    • Jul 2010
    • 1

    #1

    Notice: Undefined offset: 49

    Hi, I found the code here, it's excately what i am looking for, ie8 give this error "Notice: Undefined offset: 49 in.." which is this line "if ($num_of_chars[49]==$required)", i'm a newbie, i don't know how to overcome this error, so can someone please help me out?
    thanks,
    Code:
    $string = "one two three";
    $word_array = explode(" ",$string);
    for ($i=count($word_array); $i>=1; $i--)
    {
    $word = $i == 1 ? " word" : " words";
    echo "Strings containing combination of ".$i.$word.":<br>";
    
    $flags = array();
    $combination_flags = get_combination_flags(count($word_array), $i);
    $flags_groups = explode(",",$combination_flags);
    foreach ($flags_groups as $val)
    {
    $temp = chunk_split($val,1,".");
    array_push($flags, explode(".", substr($temp,0,(strlen($temp)-1))));
    }
    for ($j=0; $j<count($flags); $j++)
    {
    $new_string = "";
    for ($k=0; $k<count($word_array); $k++)
    $new_string .= $flags[$j][$k] ? " ".$word_array[$k] : "";
    trim ($new_string);
    echo $new_string."<br>";
    }
    echo "<br>";
    }
    
    function get_combination_flags($total, $required)
    {
    $flag_array = array();
    $bin = "";
    for ($i=0; $i<$total; $i++)
    $bin .= "1";
    $dec = bindec($bin);
    for ($i=0; $i<=$dec; $i++)
    {
    $num_of_chars = count_chars(decbin($i), 1);
    [B][U]if ($num_of_chars[49]==$required)[/U][/B]
    {
    $temp = decbin($i);
    $length = strlen($temp);
    $pre = "";
    if ($length<$total) for ($j=$length; $j<$total; $j++) $pre .= "0";
    $temp = $pre.$temp;
    array_push($flag_array,$temp);
    }
    }
    return implode(",",$flag_array);
    }
    ?>
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    All that means is that your error_reporting in your php.ini file is set to report notices. There is two ways around this problem. First one is to set your error_reporting so it doesn't report notices:

    error_reporting = E_ALL & ~E_NOTICE

    that way you turn off all the notices.

    OR

    You can use extra condition in your if statement:

    Code:
    if ((isset($num_of_chars[49]))&&($num_of_chars[49] == $required))

    Comment

    Working...