lundi 23 août 2021

Why is my if(condition){} logic not being executed inside my PHP Class?

I'm trying to parse some content from a webpage, and in doing so create an array with said content.

Example:

class ClassName {

    function __construct(){
        $this->file_array = file("https://www.domain.tld/");
        $this->outer_array = [];

        foreach($this->file_array as $index => $line) {
            $remove_these = ['<span>string one ', ' string two</span>'];
            
            if (strpos($line, $remove_these[0]) !== false) {
                // this finds the appropriate lines without issue

                $inner_array = explode(', ', str_replace($remove_these, '', $line));
                    // the second value in this array is to be the key in the outer_array

                // There are multiple instances/copies of $line,
                // and I only want one/unique entry into the outer_array

                if (in_array($inner_array[1], array_keys($this->outer_array))) {

                    // do nothing if the outer_array key already exists

                } else {

                    // $this->other_fn($inner_array) to add a key=>value pair into outer_array
                    // if the outer_array key doesn't exist

                }

            }
        }
    }
}

Problem is ---- the if (in_array($inner_array[1], array_keys($this->outer_array))) is not att all catching the existence of the key, so duplicates are being created - the else is being called on every iteration.

Where is my logic/syntax error please?

Aucun commentaire:

Enregistrer un commentaire