即使存在值,array_search也会因特定值而失败

时间:2017-03-29 14:28:43

标签: php

我有一个非常大的PHP数组,我使用array_search来获取此数组的预定义值。仅对于3个键,array_search失败

 $headings = array("Summary ","Experience","Projects","Languages","Skills & Expertise","Education","Volunteer Experience","Certifications","Courses","Publications","Honors and Awards","Test Scores","Organizations","Patents");
for ($x = 0; $x <= 13; $x++) {
      $present = $headings[$x];
      $key = array_search($present, $cvdata, true);

      if($key) {
      echo "<br />";    echo "Key Present-----------------".$present.$key;
      }else {
        echo "<br />"; echo "No Match-----------".$present;
      }

    } 

上述代码的输出为

No Match-----------Summary
No Match-----------Experience
Key Present-----------------Projects20

Key Present-----------------Languages41

Key Present-----------------Skills & Expertise46

Key Present-----------------Education58

No Match-----------Volunteer Experience
Key Present-----------------Certifications71

Key Present-----------------Courses79

Key Present-----------------Publications251

Key Present-----------------Honors and Awards259

Key Present-----------------Test Scores267

Key Present-----------------Organizations277

Key Present-----------------Patents285

总结,经验和志愿者经验 - array_search无法找到密钥。可能是什么原因? 这是我的阵列。

Array
(
    [0] => 
    [1] => Page
    [2] => 1
    [3] => Jagan Krishnaraj
    [4] => UI Architect
    [5] => test
    [6] => Summary
    [7] => I am passionate about creating web applications for people to use.
    [8] => Experience
    [9] => UI Architect 
    [10] => May 2015  -  Present 
    [11] => (1 year 11 months)
    [12] => Your Security Matters !
    [13] => The protection of privacy and the safeguarding of our Clients personal information is our highest precedence.
    [14] => At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by
    [15] =>  COMODOâ„¢ to protect your personal and confidential information
    [16] => UI Developer
    [17] => January 2013  -  April 2015  
    [18] => (2 years 4 months)
    [19] => Creation of UI design
    [20] => Projects
    [21] => LivePositiveWay
    [22] => August 2013 to November 2013
    [23] => Members:Jagan Krishnaraj
    [24] => LivePositiveWay.com is first of its kind to give people a personal online space to save and share the
    [25] =>  inspiration in their personal life with the world. The amount of inspiration every user spread to this world
    [26] =>  using this site is measured on a numerical scale called Positivity Score. The Positivity Score can also be a
    [27] =>  numerical estimation of amount of positivity every user created in this world
    [28] => Any user can create an account with livepositiveway.com and write their story, share their inspiration with
    [29] =>  the world for free & get inspired by the rating and social sharing happening on their inspiration that is
    [30] =>  accurately tracked and displayed on user's dashboard & also through the Positivity Score received.
    [31] => Responsive CV
    [32] => July 2013 to November 2014
    [33] => Members:Jagan Krishnaraj
    [34] => Your Security Matters !
    [35] => The protection of privacy and the safeguarding of our Clients personal information is our highest precedence.
    [36] => 
    [37] => Page
    [38] => 2
    [39] => At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by
    [40] =>  COMODOâ„¢ to protect your personal and confidential information
    [41] => Languages
    [42] => Tamil
    [43] => Telugu
    [44] => Hindi
    [45] => English
    [46] => Skills & Expertise
    [47] => JavaScript
    [48] => jQuery
    [49] => CSS
    [50] => MySQL
    [51] => XML
    [52] => PHP
    [53] => AJAX
    [54] => SQL
    [55] => Web Development
    [56] => User Interface Design
    [57] => Web Applications
    [58] => Education
    [59] => Ramakrishna School
    [60] => SSLC, Biology, 1995 - 2005
    [61] => Activities and Societies:  
    [62] => Participated in the School Footbal Team, Shool Table tennis team
    [63] => Arunai college of Engineering
    [64] => Bachelor's degree, Instrumentation Technology/Technician, 2005
    [65] => Activities and Societies:  
    [66] => Organized cultural events and symposium
    [67] => Volunteer Experience
    [68] => Back Office Executive  at   Isha Foundation
    [69] => January 2015  -  Present
    [70] => I  volunteered here. 
    [71] => Certifications
    [72] => Microsoft Certified
    [73] => Microsoft       February 2015 to April 2015
    [74] => Google Certified
    [75] => Google   License ANTPJ3242    March 2016
    [76] => 
    [77] => Page
    [78] => 3
    [79] => Courses
    [80] =>  
    [81] => UI Developer
    [82] => Symphony Teleca
    [83] => Angular 4
    [84] => 323233
    [85] =>  
    [86] => .
    [87] => .
    [88] => .
    [89] => .
    [90] => .
    [91] => .
    [92] => .
    [93] => .
    [94] => .
    [95] => .
    [96] => .
    [97] => .
    [98] => .
    [99] => .
    [100] => .
    [101] => .
    [102] => .
    [103] => .
    [104] => .
    [105] => .
    [106] => .
    [107] => .
    [108] => .
    [109] => .
    [110] => .
    [111] => .
    [112] => .
    [113] => .
    [114] => .
    [115] => .
    [116] => .
    [117] => .
    [118] => .
    [119] => .
    [120] => .
    [121] => .
    [122] => .
    [123] => .
    [124] => .
    [125] => .
    [126] => .
    [127] => .
    [128] => .
    [129] => .
    [130] => .
    [131] => .
    [132] => .
    [133] => .
    [134] => .
    [135] => .
    [136] => .
    [137] => .
    [138] => .
    [139] => .
    [140] => .
    [141] => .
    [142] => .
    [143] => .
    [144] => .
    [145] => .
    [146] => .
    [147] => .
    [148] => .
    [149] => .
    [150] => .
    [151] => .
    [152] => .
    [153] => .
    [154] => .
    [155] => .
    [156] => .
    [157] => .
    [158] => .
    [159] => .
    [160] => .
    [161] => .
    [162] => .
    [163] => .
    [164] => .
    [165] => .
    [166] => .
    [167] => .
    [168] => .
    [169] => .
    [170] => .
    [171] => .
    [172] => .
    [173] => .
    [174] => .
    [175] => .
    [176] => .
    [177] => .
    [178] => .
    [179] => .
    [180] => .
    [181] => .
    [182] => .
    [183] => .
    [184] => .
    [185] => .
    [186] => .
    [187] => .
    [188] => .
    [189] => .
    [190] => .
    [191] => .
    [192] => .
    [193] => .
    [194] => .
    [195] => .
    [196] => .
    [197] => .
    [198] => .
    [199] => .
    [200] => .
    [201] => .
    [202] => .
    [203] => .
    [204] => .
    [205] => .
    [206] => .
    [207] => .
    [208] => .
    [209] => .
    [210] => .
    [211] => .
    [212] => .
    [213] => .
    [214] => .
    [215] => .
    [216] => .
    [217] => .
    [218] => .
    [219] => .
    [220] => .
    [221] => .
    [222] => .
    [223] => .
    [224] => .
    [225] => .
    [226] => .
    [227] => .
    [228] => .
    [229] => .
    [230] => .
    [231] => .
    [232] => .
    [233] => .
    [234] => .
    [235] => .
    [236] => .
    [237] => .
    [238] => .
    [239] => .
    [240] => .
    [241] => .
    [242] => .
    [243] => .
    [244] => .
    [245] => .
    [246] => .
    [247] =>  
    [248] => Independent Coursework
    [249] =>  
    [250] => Angular 2
    [251] => Publications
    [252] => Publication On Angular & Node
    [253] =>    June 6, 2008
    [254] => Authors: Jagan Krishnaraj
    [255] => Your Security Matters !
    [256] => The protection of privacy and the safeguarding of our Clients personal information is our highest precedence.
    [257] => At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by
    [258] =>  COMODOâ„¢ to protect your personal and confidential information
    [259] => Honors and Awards
    [260] => Best Student Award
    [261] => Microsoft
    [262] => April 2014
    [263] => Your Security Matters !
    [264] => The protection of privacy and the safeguarding of our Clients personal information is our highest precedence.
    [265] => At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by
    [266] =>  COMODOâ„¢ to protect your personal and confidential information
    [267] => Test Scores
    [268] => Test Score
    [269] => February 2016   Score:98
    [270] => Your Security Matters !
    [271] => The protection of privacy and the safeguarding of our Clients personal information is our highest precedence.
    [272] => At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by
    [273] =>  COMODOâ„¢ to protect your personal and confidential information
    [274] => 
    [275] => Page
    [276] => 4
    [277] => Organizations
    [278] => Infosys
    [279] => manager
    [280] => January 2013 to May 2014
    [281] => Your Security Matters !
    [282] => The protection of privacy and the safeguarding of our Clients personal information is our highest precedence.
    [283] => At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by
    [284] =>  COMODOâ„¢ to protect your personal and confidential information
    [285] => Patents
    [286] => Responsive CV Patent
    [287] => India Patent Application 32333   
    [288] => Inventors: Jagan Krishnaraj
    [289] => Your Security Matters !
    [290] => The protection of privacy and the safeguarding of our Clients personal information is our highest precedence.
    [291] => At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by
    [292] =>  COMODOâ„¢ to protect your personal and confidential information
)

- 编辑请求的信息 -

$ cvdata来自pdf文件,通过php类将其转换为HTML然后转换为纯文本。

这是cvdata的var_dump

array(300) { [0]=> string(0) "" [1]=> string(4) "Page" [2]=> string(1) "1" [3]=> string(17) "Jagan Krishnaraj " [4]=> string(37) "UI Architect at International " [5]=> string(24) "jaxplore@gmail.com " [6]=> string(8) "Summary " [7]=> string(67) "I am passionate about creating web applications for people to use. " [8]=> string(11) "Experience " [9]=> string(40) "UI Architect at International " [10]=> string(21) "May 2015 - Present " [11]=> string(19) "(1 year 11 months) " [12]=> string(24) "Your Security Matters ! " [13]=> string(110) "The protection of privacy and the safeguarding of our Clients personal information is our highest precedence. " [14]=> string(104) "At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by " [15]=> string(65) " COMODOâ„¢ to protect your personal and confidential information " [16]=> string(35) "UI Developer at Symphony Teleca " [17]=> string(29) "January 2013 - April 2015 " [18]=> string(19) "(2 years 4 months) " [19]=> string(22) "Creation of UI design " [20]=> string(8) "Projects" [21]=> string(16) "LivePositiveWay " [22]=> string(29) "August 2013 to November 2013 " [23]=> string(25) "Members:Jagan Krishnaraj " [24]=> string(102) "LivePositiveWay.com is first of its kind to give people a personal online space to save and share the " [25]=> string(110) " inspiration in their personal life with the world. The amount of inspiration every user spread to this world " [26]=> string(110) " using this site is measured on a numerical scale called Positivity Score. The Positivity Score can also be a " [27]=> string(79) " numerical estimation of amount of positivity every user created in this world " [28]=> string(108) "Any user can create an account with livepositiveway.com and write their story, share their inspiration with " [29]=> string(107) " the world for free & get inspired by the rating and social sharing happening on their inspiration that is " [30]=> string(100) " accurately tracked and displayed on user's dashboard & also through the Positivity Score received. " [31]=> string(14) "Responsive CV " [32]=> string(27) "July 2013 to November 2014 " [33]=> string(25) "Members:Jagan Krishnaraj " [34]=> string(24) "Your Security Matters ! " [35]=> string(109) "The protection of privacy and the safeguarding of our Clients personal information is our highest precedence." [36]=> string(0) "" [37]=> string(4) "Page" [38]=> string(1) "2" [39]=> string(104) "At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by " [40]=> string(65) " COMODOâ„¢ to protect your personal and confidential information " [41]=> string(9) "Languages" [42]=> string(5) "Tamil" [43]=> string(6) "Telugu" [44]=> string(5) "Hindi" [45]=> string(7) "English" [46]=> string(18) "Skills & Expertise" [47]=> string(10) "JavaScript" [48]=> string(6) "jQuery" [49]=> string(3) "CSS" [50]=> string(5) "MySQL" [51]=> string(3) "XML" [52]=> string(3) "PHP" [53]=> string(4) "AJAX" [54]=> string(3) "SQL" [55]=> string(15) "Web Development" [56]=> string(21) "User Interface Design" [57]=> string(16) "Web Applications" [58]=> string(9) "Education" [59]=> string(19) "Ramakrishna School " [60]=> string(27) "SSLC, Biology, 1995 - 2005 " [61]=> string(27) "Activities and Societies: " [62]=> string(65) "Participated in the School Footbal Team, Shool Table tennis team " [63]=> string(30) "Arunai college of Engineering " [64]=> string(63) "Bachelor's degree, Instrumentation Technology/Technician, 2005 " [65]=> string(27) "Activities and Societies: " [66]=> string(40) "Organized cultural events and symposium " [67]=> string(21) "Volunteer Experience " [68]=> string(44) "Back Office Executive at Isha Foundation " [69]=> string(25) "January 2015 - Present " [70]=> string(22) "I volunteered here. " [71]=> string(14) "Certifications" [72]=> string(20) "Microsoft Certified " [73]=> string(44) "Microsoft February 2015 to April 2015 " [74]=> string(17) "Google Certified " [75]=> string(40) "Google License ANTPJ3242 March 2016" [76]=> string(0) "" [77]=> string(4) "Page" [78]=> string(1) "3" [79]=> string(7) "Courses" [80]=> string(1) " " [81]=> string(13) "UI Developer " [82]=> string(16) "Symphony Teleca " [83]=> string(9) "Angular 4" [84]=> string(6) "323233" [85]=> string(2) " " [86]=> string(1) "." [87]=> string(1) "." [88]=> string(1) "." [89]=> string(1) "." [90]=> string(1) "." [91]=> string(1) "." [92]=> string(1) "." [93]=> string(1) "." [94]=> string(1) "." [95]=> string(1) "." [96]=> string(1) "." [97]=> string(1) "." [98]=> string(1) "." [99]=> string(1) "." [100]=> string(1) "." [101]=> string(1) "." [102]=> string(1) "." [103]=> string(1) "." [104]=> string(1) "." [105]=> string(1) "." [106]=> string(1) "." [107]=> string(1) "." [108]=> string(1) "." [109]=> string(1) "." [110]=> string(1) "." [111]=> string(1) "." [112]=> string(1) "." [113]=> string(1) "." [114]=> string(1) "." [115]=> string(1) "." [116]=> string(1) "." [117]=> string(1) "." [118]=> string(1) "." [119]=> string(1) "." [120]=> string(1) "." [121]=> string(1) "." [122]=> string(1) "." [123]=> string(1) "." [124]=> string(1) "." [125]=> string(1) "." [126]=> string(1) "." [127]=> string(1) "." [128]=> string(1) "." [129]=> string(1) "." [130]=> string(1) "." [131]=> string(1) "." [132]=> string(1) "." [133]=> string(1) "." [134]=> string(1) "." [135]=> string(1) "." [136]=> string(1) "." [137]=> string(1) "." [138]=> string(1) "." [139]=> string(1) "." [140]=> string(1) "." [141]=> string(1) "." [142]=> string(1) "." [143]=> string(1) "." [144]=> string(1) "." [145]=> string(1) "." [146]=> string(1) "." [147]=> string(1) "." [148]=> string(1) "." [149]=> string(1) "." [150]=> string(1) "." [151]=> string(1) "." [152]=> string(1) "." [153]=> string(1) "." [154]=> string(1) "." [155]=> string(1) "." [156]=> string(1) "." [157]=> string(1) "." [158]=> string(1) "." [159]=> string(1) "." [160]=> string(1) "." [161]=> string(1) "." [162]=> string(1) "." [163]=> string(1) "." [164]=> string(1) "." [165]=> string(1) "." [166]=> string(1) "." [167]=> string(1) "." [168]=> string(1) "." [169]=> string(1) "." [170]=> string(1) "." [171]=> string(1) "." [172]=> string(1) "." [173]=> string(1) "." [174]=> string(1) "." [175]=> string(1) "." [176]=> string(1) "." [177]=> string(1) "." [178]=> string(1) "." [179]=> string(1) "." [180]=> string(1) "." [181]=> string(1) "." [182]=> string(1) "." [183]=> string(1) "." [184]=> string(1) "." [185]=> string(1) "." [186]=> string(1) "." [187]=> string(1) "." [188]=> string(1) "." [189]=> string(1) "." [190]=> string(1) "." [191]=> string(1) "." [192]=> string(1) "." [193]=> string(1) "." [194]=> string(1) "." [195]=> string(1) "." [196]=> string(1) "." [197]=> string(1) "." [198]=> string(1) "." [199]=> string(1) "." [200]=> string(1) "." [201]=> string(1) "." [202]=> string(1) "." [203]=> string(1) "." [204]=> string(1) "." [205]=> string(1) "." [206]=> string(1) "." [207]=> string(1) "." [208]=> string(1) "." [209]=> string(1) "." [210]=> string(1) "." [211]=> string(1) "." [212]=> string(1) "." [213]=> string(1) "." [214]=> string(1) "." [215]=> string(1) "." [216]=> string(1) "." [217]=> string(1) "." [218]=> string(1) "." [219]=> string(1) "." [220]=> string(1) "." [221]=> string(1) "." [222]=> string(1) "." [223]=> string(1) "." [224]=> string(1) "." [225]=> string(1) "." [226]=> string(1) "." [227]=> string(1) "." [228]=> string(1) "." [229]=> string(1) "." [230]=> string(1) "." [231]=> string(1) "." [232]=> string(1) "." [233]=> string(1) "." [234]=> string(1) "." [235]=> string(1) "." [236]=> string(1) "." [237]=> string(1) "." [238]=> string(1) "." [239]=> string(1) "." [240]=> string(1) "." [241]=> string(1) "." [242]=> string(1) "." [243]=> string(1) "." [244]=> string(1) "." [245]=> string(1) "." [246]=> string(2) ". " [247]=> string(1) " " [248]=> string(22) "Independent Coursework" [249]=> string(1) " " [250]=> string(9) "Angular 2" [251]=> string(12) "Publications" [252]=> string(30) "Publication On Angular & Node " [253]=> string(16) " June 6, 2008 " [254]=> string(26) "Authors: Jagan Krishnaraj " [255]=> string(24) "Your Security Matters ! " [256]=> string(110) "The protection of privacy and the safeguarding of our Clients personal information is our highest precedence. " [257]=> string(104) "At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by " [258]=> string(65) " COMODOâ„¢ to protect your personal and confidential information " [259]=> string(17) "Honors and Awards" [260]=> string(19) "Best Student Award " [261]=> string(10) "Microsoft " [262]=> string(11) "April 2014 " [263]=> string(24) "Your Security Matters ! " [264]=> string(110) "The protection of privacy and the safeguarding of our Clients personal information is our highest precedence. " [265]=> string(104) "At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by " [266]=> string(65) " COMODOâ„¢ to protect your personal and confidential information " [267]=> string(11) "Test Scores" [268]=> string(11) "Test Score " [269]=> string(25) "February 2016 Score:98 " [270]=> string(24) "Your Security Matters ! " [271]=> string(110) "The protection of privacy and the safeguarding of our Clients personal information is our highest precedence. " [272]=> string(104) "At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by " [273]=> string(64) " COMODOâ„¢ to protect your personal and confidential information" [274]=> string(0) "" [275]=> string(4) "Page" [276]=> string(1) "4" [277]=> string(13) "Organizations" [278]=> string(8) "Infosys " [279]=> string(8) "manager " [280]=> string(25) "January 2013 to May 2014 " [281]=> string(24) "Your Security Matters ! " [282]=> string(110) "The protection of privacy and the safeguarding of our Clients personal information is our highest precedence. " [283]=> string(104) "At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by " [284]=> string(65) " COMODOâ„¢ to protect your personal and confidential information " [285]=> string(7) "Patents" [286]=> string(21) "Responsive CV Patent " [287]=> string(34) "India Patent Application 32333 " [288]=> string(28) "Inventors: Jagan Krishnaraj " [289]=> string(24) "Your Security Matters ! " [290]=> string(110) "The protection of privacy and the safeguarding of our Clients personal information is our highest precedence. " [291]=> string(104) "At TheResponsiveCV, we use the highest levels of security, including 128-bit SSL encryption verified by " [292]=> string(64) " COMODOâ„¢ to protect your personal and confidential information" [293]=> string(0) "" [294]=> string(4) "Page" [295]=> string(1) "5" [296]=> string(17) "Jagan Krishnaraj " [297]=> string(37) "UI Architect at ' International " [298]=> string(24) "jaxplore@gmail.com " [299]=> string(25) "Contact Jagan on LinkedIn" }

1 个答案:

答案 0 :(得分:0)

按照评论中的建议,从"Summary "数组中的$headings末尾删除空格。

$headings = array("Summary", ...

然后,从$cvdata数组中删除任何前导或尾随空格。

$cvdata = array_map('trim', $cvdata);

然后你已经拥有的代码应该工作。请注意,如果在密钥false上找到某个项目,则应在array_search返回时使用与0的显式比较,这会使$keyif ($key)评估为false {1}}。所以改为使用

if($key === false) { ...

the documentation for array_search中的红色警告框中提到了这一点。对于你这里的具体例子来说无关紧要,但总的来说肯定可以。