简体   繁体   中英

check if in_array php not working

I want to check a value in an array. If its not there it gets pushed to a new array. If the value is already there then its not added to the new value. But with my code the checking of the value is not working.

Its still adding the array if the specific value is present.

I got this code:

foreach($user_movie_info['data'] as $movie) {

            similar_text($movie_facebook_page['genre'], $movie['genre'], $percent);

            if ($percent > 30) {
                echo $movie_facebook_page['genre']. "" ."</br>";
                echo $movie['genre']. "" ."</br>";
                echo $percent. "" ."</br>";
                echo "match! </br></br>";

                // add all movie information to matched array, only if its not already present
                if (!in_array($movie_facebook_page['name'], $matched_movies_array)) {

                    array_push($matched_movies_array, $movie_facebook_page);
                }




        } //foreach 

If i print out the $matched_movies_array i got one array 2 times in it:

Array
(
    [0] => Array
        (
            [about] => In theaters January 4th 2013 www.TexasChainsaw3D.com
            [can_post] => 1
            [description] => Lionsgate’s TEXAS CHAINSAW 3D continues the legendary story of the homicidal Sawyer family, picking up where Tobe Hooper’s 1974 horror classic left off in Newt, Texas, where for decades people went missing without a trace.  The townspeople long suspected the Sawyer family, owners of a local barbeque pit, were somehow responsible.  Their suspicions were finally confirmed one hot summer day when a young woman escaped the Sawyer house following the brutal murders of her four friends.  Word around the small town quickly spread, and a vigilante mob of enraged locals surrounded the Sawyer stronghold, burning it to the ground and killing every last member of the family – or so they thought. 

Decades later and hundreds of miles away from the original massacre, a young woman named Heather learns that she has inherited a Texas estate from a grandmother she never knew she had.  After embarking on a road trip with friends to uncover her roots, she finds she is the sole owner of a lavish, isolated Victorian mansion. But her newfound wealth comes at a price as she stumbles upon a horror that awaits her in the mansion’s dank cellars…

With gruesome surprises in store for a whole new generation, TEXAS CHAINSAW 3D stars Alexandra Daddario, Dan Yeager, Tremaine ‘Trey Songz’ Neverson, Scott Eastwood, Tania Raymonde, Shaun Sipos, Keram Malicki-Sanchez, James MacDonald, Thom Barry, Paul Rae and Richard Riehle, along with special appearances from four beloved cast members from previous installments of the franchise: Gunnar Hansen (the original Leatherface), Marilyn Burns, John Dugan and Bill Moseley. The film is directed by John Luessenhop (TAKERS), from a screenplay by Adam Marcus & Debra Sullivan and Kirsten Elms, based on a story by Stephen Susco and Adam Marcus & Debra Sullivan and based on characters created by Kim Henkel and Tobe Hooper, and produced by Carl Mazzocone. Lionsgate presents a  production and Main Line Pictures production.

            [directed_by] => John Luessenhop 
            [genre] => Horror
            [is_published] => 1
            [produced_by] => Millennium Films
            [release_date] => January 4th 2012
            [screenplay_by] => Adam Marcus & Debra Sullivan and Kirsten Elms
            [starring] => Alexandra Daddario, Dan Yeager, Tremaine ‘Trey Songz’ Neverson, Scott Eastwood, Tania Raymonde, Shaun Sipos, Keram Malicki-Sanchez, James MacDonald, Thom Barry, Paul Rae and Richard Riehle
            [studio] => Lionsgate
            [talking_about_count] => 62964
            [username] => TexasChainsaw3D
            [website] => www.texaschainsaw3d.com, twitter.com/lionsgatehorror, http://pinterest.com/lionsgatemovies/texas-chainsaw-3d, https://plus.google.com/u/0/+LionsgateMovies, http://instagr.am/p/Qpm0JMPtDr/
            [were_here_count] => 0
            [written_by] => based on a story by Stephen Susco and Adam Marcus & Debra Sullivan
            [category] => Movie
            [id] => 323192834416509
            [name] => Texas Chainsaw 3D
            [link] => http://www.facebook.com/TexasChainsaw3D
            [likes] => 367992
            [cover] => Array
                (
                    [cover_id] => 4.14284428641E+14
                    [source] => http://sphotos-c.ak.fbcdn.net/hphotos-ak-ash3/s720x720/530974_414284428640682_1806025466_n.png
                    [offset_y] => 0
                )

        )

    [1] => Array
        (
            [about] => The official Facebook Page for The Shining |  All work and no play makes Jack a dull boy.
            [awards] => (1981) Saturn Award, Best Supporting Actor, Scatman Crothers
            [can_post] => 1
            [description] => Get The Shining at the WB Shop: http://bit.ly/shiningdvd
            [directed_by] => Stanley Kubrick 
            [genre] => Horror, Suspense/Thriller 
            [is_published] => 1
            [plot_outline] => All work and no play makes Academy Award-winner Jack Nicholson ("As Good As It Gets," "Batman"), the caretaker of an isolated resort, go way off the deep end, terrorizing his young son and wife Shelley Duvall ("Roxanne"). Master filmmaker Stanley Kubrick's ("Full Metal Jacket," "2001: A Space Odyssey") visually haunting chiller, based on the bestseller by master-of-suspense Stephen King ("The Stand," "Carrie," "The Shawshank Redemption"), is an undeniable contemporary classic. Newsweek Magazine calls this "the first epic horror film," full of indelible images, and a signature role for Nicholson whose character was recently selected by AFI for its' 50 Greatest Villains.

            [produced_by] => Jan Harlan, Stanley Kubrick
            [release_date] => 5/23/80
            [screenplay_by] => Stephen King, Diane Johnson  
            [starring] => Jack Nicholson, Shelley Duvall, Danny Lloyd, Scatman Crothers 
            [studio] => Warner Bros.
            [talking_about_count] => 5594
            [username] => KubrickShining
            [website] => http://bit.ly/shiningdvd
            [were_here_count] => 0
            [written_by] => Stephen King
            [category] => Movie
            [id] => 135347089926692
            [name] => The Shining
            [link] => http://www.facebook.com/KubrickShining
            [likes] => 832526
            [cover] => Array
                (
                    [cover_id] => 2.24275514367E+14
                    [source] => http://sphotos-f.ak.fbcdn.net/hphotos-ak-ash4/320182_224275514367182_46004854_n.jpg
                    [offset_y] => 85
                )

        )

    [2] => Array
        (
            [about] => The official Facebook Page for The Shining |  All work and no play makes Jack a dull boy.
            [awards] => (1981) Saturn Award, Best Supporting Actor, Scatman Crothers
            [can_post] => 1
            [description] => Get The Shining at the WB Shop: http://bit.ly/shiningdvd
            [directed_by] => Stanley Kubrick 
            [genre] => Horror, Suspense/Thriller 
            [is_published] => 1
            [plot_outline] => All work and no play makes Academy Award-winner Jack Nicholson ("As Good As It Gets," "Batman"), the caretaker of an isolated resort, go way off the deep end, terrorizing his young son and wife Shelley Duvall ("Roxanne"). Master filmmaker Stanley Kubrick's ("Full Metal Jacket," "2001: A Space Odyssey") visually haunting chiller, based on the bestseller by master-of-suspense Stephen King ("The Stand," "Carrie," "The Shawshank Redemption"), is an undeniable contemporary classic. Newsweek Magazine calls this "the first epic horror film," full of indelible images, and a signature role for Nicholson whose character was recently selected by AFI for its' 50 Greatest Villains.

            [produced_by] => Jan Harlan, Stanley Kubrick
            [release_date] => 5/23/80
            [screenplay_by] => Stephen King, Diane Johnson  
            [starring] => Jack Nicholson, Shelley Duvall, Danny Lloyd, Scatman Crothers 
            [studio] => Warner Bros.
            [talking_about_count] => 5594
            [username] => KubrickShining
            [website] => http://bit.ly/shiningdvd
            [were_here_count] => 0
            [written_by] => Stephen King
            [category] => Movie
            [id] => 135347089926692
            [name] => The Shining
            [link] => http://www.facebook.com/KubrickShining
            [likes] => 832526
            [cover] => Array
                (
                    [cover_id] => 2.24275514367E+14
                    [source] => http://sphotos-f.ak.fbcdn.net/hphotos-ak-ash4/320182_224275514367182_46004854_n.jpg
                    [offset_y] => 85
                )

        )

)

I get this info from the open graph api from Facebook.

$matched_movies_array does not contain the movie names. So in_array will never pass.

Try something like:

$movieIds = array();
foreach($user_movie_info['data'] as $movie) {

        similar_text($movie_facebook_page['genre'], $movie['genre'], $percent);

        if ($percent > 30) {
            echo $movie_facebook_page['genre']. "" ."</br>";
            echo $movie['genre']. "" ."</br>";
            echo $percent. "" ."</br>";
            echo "match! </br></br>";

            // add all movie information to matched array, only if its not already present
            if (!in_array($movie_facebook_page['id'], $movieIds)) {
                $movieIds[] = $movie_facebook_page['id'];
                array_push($matched_movies_array, $movie_facebook_page);
            }




    } //foreach 

Or maybe even better:

            $id = $movie_facebook_page['id'];
            if (!isset($matched_movies_array[$id])) {
                $matched_movies_array[$id] = $movie_facebook_page;
            }

The in_array() function doesn't work searching for a string in a multi-dimensional array. Take this for example:

$find = array("name" => "test");
$matches = array(array("name" => "test"), array("name" => "test2"));

echo (in_array($find['name'], $matches)) ? "found" : "not found";

echo "<br /><br />";

echo (in_array($find, $matches)) ? "found" : "not found";

The first echo is "not found", the second one is "found". You should use your entire $movie_facebook_page array as the needle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM