2012-12-06 62 views
0

我想检查数组中的值。如果它不在那里,它会被推到一个新的数组中。如果该值已经存在,则不会将其添加到新值中。 但与我的代码的值检查不起作用。检查in_array php不工作

如果存在特定值,它仍然会添加数组。

我得到这个代码:

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我在里面有一个阵列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此信息。

回答

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; 
      } 
+0

$ matched_movies_array确实包含电影名称。正如您在打印出的例子中可以看到的:[name] => Texas Chainsaw 3D。它每次都获得通过。那就是问题所在。这是在if(!in_array ...) – user1386906

+0

之后的数组中打印出来的,但是in_array不会以递归方式搜索该值。在你的情况下,你添加一个包含名称的数组,而不仅仅是名称本身。 – sroes

+0

'name'是推入的数组的一部分。只有'$ name'不存在于$ movie_facebook_page中时才应该将其推入。但那不工作,你可以看到。我有两个相同的数组。 – user1386906

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

第一个回声是“找不到”,第二个回声是“找到”。您应该使用整个$ movie_facebook_page数组作为针。

+0

是的,我明白了。谢谢 :) – user1386906