0

配列の値を確認したい。そこにない場合は、新しいアレイにプッシュされます。値がすでに存在する場合、その値は新しい値に追加されません。しかし、私のコードでは、値のチェックが機能していません。

特定の値が存在する場合は、引き続き配列を追加します。

私はこのコードを手に入れました:

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 

$ matched_movies_arrayを印刷すると、1つの配列が2回取得されます。

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
                )

        )

)

この情報は、FacebookのオープングラフAPIから取得します。

4

2 に答える 2

1

$matched_movies_arrayには映画名が含まれていません。したがって、in_arrayは通過しません。

次のようなものを試してください:

$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 

または多分さらに良い:

            $id = $movie_facebook_page['id'];
            if (!isset($matched_movies_array[$id])) {
                $matched_movies_array[$id] = $movie_facebook_page;
            }
于 2012-12-06T21:49:10.763 に答える
0

in_array()関数は、多次元配列内の文字列の検索では機能しません。これを例に取ってください:

$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";

最初のエコーは「見つかりません」、2番目のエコーは「見つかりました」です。$movie_facebook_page配列全体を針として使用する必要があります。

于 2012-12-06T22:08:12.393 に答える