Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

php - In_array bug: the first element will be always evaluate to true even if it is already present in the array

Here's my problem:

function parse_xml($url) {
    $fileContents= file_get_contents($url);
    $simpleXml = simplexml_load_string($fileContents, null
        , LIBXML_NOCDATA);
    $json = json_encode($simpleXml);
    return $json;
}

function gk_install_data() {

    $args = array(
        'numberposts' => -1,
        'post_type'   => 'jobs_api'
    );

    $latestjobs = get_posts( $args );


    $jobtitle = [];
    foreach ($latestjobs as $latestjob) {
        $jobtitle[] = $latestjob->post_title;
    }

    $jsonto = parse_xml('something.html');

    $json_a = json_decode($jsonto, true);

    foreach ($json_a['Job'] as $job => $jobvalue) {

        if (is_array($jobvalue["JobShortDescription"])) {
            $jobdesc = 'Nothing interesting';
        } else {
            $jobdesc = $jobvalue["JobShortDescription"];
        }
        
        if (!in_array($jobvalue["JobTitle"], $jobtitle, true)) {
            $my_jobs_api = array(
                'post_author' => 1,
                'post_content' => '<!-- wp:lazyblock/content {"content":"'.$jobdesc.'"} /-->',
                'post_title' => $jobvalue["JobTitle"],
                'post_status' => 'publish',
                'post_type' => 'jobs_api'

            );
            wp_insert_post( $my_jobs_api );
        }
        
    }


}

gk_install_data();

I'm parsing an XML file and I turned it into a JSON file.

The idea is to post it to my DB (WordPress) with the function wp_insert_post.

So far so good, it works, except for a very disturbing detail: I don't want any duplicate item, so I'm checking if a field of the array (post_title) is already present in the other array (within a for-each loop) with the function in_array, but for some reason after the first time that I run the code (that will trigger the insert function), the very first value is always returning a true statement so it will always insert within the DB.

I have no idea why, the only difference that I can see (and that's the reason why I added a true value for the third parameter, which is the strict mode) is that the first title is a string "Test vacancy " with space at the very end.

I will describe the problem from another angle:

foreach ($json_a['Job'] as $job => $jobvalue) {

    if (!in_array($jobvalue["JobTitle"], $jobtitle, true)) {
        echo "problem";
    } else {
        echo "solution";
    }

}

If I run this snippet, the first string that will be print will be "problem" and then "solution".

Even after the posts will be inserted into the DB, when all of them will have the very same title, the first post will be always insert so the first post_title even if it will be equal to the element inserted in the DB will be evaluated as false and re-insert into the DB.

Any Idea?

UPDATE:

var_dump(array_values($jobtitle));

array(12) {
  [0]=>
  string(22) "Recruitment Consultant"
  [1]=>
  string(15) "Product Manager"
  [2]=>
  string(69) "Ondernemende avonturier (starter/starter+) voor uitbouwen nieuw label"
  [3]=>
  string(12) "Webdeveloper"
  [4]=>
  string(14) "Office manager"
  [5]=>
  string(15) "Dummy vacancy 1"
  [6]=>
  string(15) "Dummy vacancy 2"
  [7]=>
  string(15) "Dummy vacancy 3"
  [8]=>
  string(15) "Dummy vacancy 4"
  [9]=>
  string(15) "Dummy vacancy 5"
  [10]=>
  string(20) "Vacature test Jasper"
  [11]=>
  string(12) "Test vacancy"
}

Duplicate item:

string(13) "Test vacancy "

question from:https://stackoverflow.com/questions/66052028/in-array-bug-the-first-element-will-be-always-evaluate-to-true-even-if-it-is-al

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...