1

in general I think I understand what the error message means. But in my case, it's a riddle I didn't succeed in solving...

    $keywords_all = array();
    $count = 0;

    for ($z = 0; $z < $num_results; $z++)
    {
        $keywords_array = explode(",", $row['free_keywords']);

        for ($i = 0; $i < count($keywords_array); $i++)
        {
            if (in_array(strtolower(trim($keywords_array[$i])), $keywords_all))
            {
                $count++;
            }
            else
            {
                echo "<br />".$keywords_array[$i];
                $keywords_all[$count] = $keywords_array[$i];
            }
        }

        $row = pg_fetch_array($result);
    }

So, what's wrong with that one? The error message pops up in the line

    $keywords_all[$count] = $keywords_array[$i];

I have no clue, seems to be alright to me. But guess, it's again a tiny, tiny thing I've neglected... Thanks for any hints!

4
  • Split the line into two; use a temporary variable; so you can determine if the error originates right or left hand side. Also try ${'keywords_all'}[$count] etc. while you're at it (though [] operator precedence is only a problem for object props mostly). Commented Dec 6, 2012 at 12:26
  • 2
    I can't reproduce this error; can you give us test values for $row['free_keywords'] and $keywords_all? Commented Dec 6, 2012 at 12:26
  • I split it into two lines, and it comes from "$keywords_all[$count] = $temp;". And the one in brackets {} doesn't work either. Values are just some words like "Agriculture","cereals","production","Population","female". Commented Dec 6, 2012 at 12:42
  • foreach() might make for neater code than those for() loops. Commented Dec 6, 2012 at 13:34

2 Answers 2

1

I was not able to reproduce your error message. I did find a bug in your code though (I am assuming that you are putting all your keywords in the $keywords_all array without any duplicates). So you should not increment $count inside your IF but instead update the $keywords_all count. See below:

if (in_array(strtolower(trim($keywords_array[$i])), $keywords_all)) {
    $count = count($keywords_all);
} else {
    echo "<br />".$keywords_array[$i];
    $keywords_all[$count] = $keywords_array[$i];
    $count++;
}

You will increment $count after storing a value to your $keywords_all array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that. I though I'd copy that one line where I get the error message from your code, you never know... And it worked. He?!?! Now, I have two lines which are the same, but where the first generates "$keywords_all[$count] = $keywords_array[$i];" the error message and the second one not: "$keywords_all[$count] = $keywords_array[$i];". Mystery!!
0
    $keywords_all = array();
    $count = 0; // what for you neet this var ?

    $myRow = 'keyW1,keyW2,keyW3,keyW2';

//    for ($z = 0; $z < $num_results; $z++) // there is no variable $num_results at your code so I've replaced it with constant
    for ($z = 0; $z < 1; $z++)
    {
//        $keywords_array = explode(",", $row['free_keywords']); 
        $keywords_array = explode(",", $myRow);

//        for ($i = 0; $i < count($keywords_array); $i++)
        foreach ($keywords_array as $keyword)
        {

            $keyword = strtolower( trim( $keyword ) ); // some syntax sugar would be nice

            if ( !in_array( $keyword, $keywords_all) )
            {
                echo "<br />".$keyword;

                $keywords_all[] = $keyword;

            }
        }

//        $row = pg_fetch_array($result);
    }

    var_dump($keywords_all);

This code would be better for you i think, but if you just want to get rid of duplicated records

array_uniq( array("1", "1", "2", "1") ) 

would be better solution for you.

1 Comment

Thanks a lot for the tweaking. Looks much nicer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.