我有一个数组
array(153) { [0]=> string(47) "colg - colgate palmolive (pakistan) ltd. " [1]=> string(47) "gatm - gul ahmed textile mills ltd. " [2]=> string(47) "atlh - atlas honda limited " [3]=> string(33) "ffbl - fauji fertilizer bin qasim" [4]=> string(16) " - general news" [5]=> string(49) "searl - the searl company ltd. " [6]=> string(47) "jdws - j. d. w. sugar mills ltd. " [7]=> string(47) "aabs - al- abbas sugar mills limited. " [8]=> string(37) "hmb - habib metropolitan bank limited" [9]=> string(37) "hmb - habib metropolitan bank limited" [10]=> string(23) "ogdc - oil & gas dev.co" [11]=> string(39) "ffc - fauji fertilizer company limited." [12]=> string(46) "bop - bank of punjab limited. " [13]=> string(46) "bop - bank of punjab limited. " [14]=> string(30) "luck - lucky cement limited " [15]=> string(47) "natf - national foods limited. " [16]=> string(46) "pol - pakistan oilfields ltd. " [17]=> string(47) "psmc - pak suzuki motor co. ltd. " [18]=> string(35) "nbp - national bank of pakistan ltd" [19]=> string(27) "ncpl - nishat chunian power" [20]=> string(25) "tgl - tariq glass limited" [21]=> string(47) "bahl - bank al-habib limited " [22]=> string(33) "ffbl - fauji fertilizer bin qasim" [23]=> string(136) " kse 100 kse 30 kmi 30 " [24]=> string(9) "26,730.82" [25]=> string(5) "60.22" [26]=> string(18) "volume: 96,370,040" [27]=> string(9) "19,289.00" [28]=> string(6) "-18.68" [29]=> string(18) "volume: 43,582,400" [30]=> string(9) "43,968.61" [31]=> string(6) "-61.00" [32]=> string(18) "volume: 27,252,400" [33]=> string(3) "anl" [34]=> string(4) "9.73" [35]=> string(4) "0.58" [36]=> string(10) "23,192,000" [37]=> string(4) "jscl" [38]=> string(5) "12.53" [39]=> string(4) "1.00" [40]=> string(10) "20,936,000" [41]=> string(4) "jsil" [42]=> string(5) "13.75" [43]=> string(4) "0.35" [44]=> string(10) "10,966,500" [45]=> string(5) "power" [46]=> string(4) "8.00" [47]=> string(4) "0.71" [48]=> string(9) "7,161,000" [49]=> string(4) "piaa" [50]=> string(4) "9.80" [51]=> string(4) "0.16" [52]=> string(9) "6,978,500" [53]=> string(4) "lpcl" [54]=> string(5) "10.40" [55]=> string(5) "-0.04" [56]=> string(9) "5,988,500" [57]=> string(3) "bop" [58]=> string(5) "11.34" [59]=> string(4) "0.11" [60]=> string(9) "5,785,000" [61]=> string(4) "byco" [62]=> string(4) "9.39" [63]=> string(4) "0.47" [64]=> string(9) "4,972,000" [65]=> string(5) "kasbb" [66]=> string(4) "2.39" [67]=> string(4) "0.34" [68]=> string(9) "4,910,500" [69]=> string(3) "spl" [70]=> string(5) "19.78" [71]=> string(4) "0.65" [72]=> string(9) "4,600,000" [73]=> string(6) "nestle" [74]=> string(9) "11,989.16" [75]=> string(6) "570.91" [76]=> string(5) "2,100" [77]=> string(4) "iltm" [78]=> string(8) "1,564.92" [79]=> string(5) "74.52" [80]=> string(3) "500" [81]=> string(5) "mureb" [82]=> string(6) "913.18" [83]=> string(5) "43.48" [84]=> string(6) "57,500" [85]=> string(5) "wyeth" [86]=> string(8) "4,600.00" [87]=> string(5) "33.50" [88]=> string(3) "600" [89]=> string(4) "colg" [90]=> string(8) "1,700.00" [91]=> string(5) "15.50" [92]=> string(3) "140" [93]=> string(4) "hino" [94]=> string(6) "292.87" [95]=> string(5) "13.94" [96]=> string(3) "400" [97]=> string(3) "ici" [98]=> string(6) "323.00" [99]=> string(5) "11.55" [100]=> string(7) "103,500" [101]=> string(4) "pict" [102]=> string(6) "270.98" [103]=> string(4) "9.88" [104]=> string(6) "29,200" [105]=> string(4) "pkgs" [106]=> string(6) "309.00" [107]=> string(4) "9.04" [108]=> string(6) "37,800" [109]=> string(4) "jdmt" [110]=> string(6) "168.49" [111]=> string(4) "8.02" [112]=> string(5) "3,500" [113]=> string(4) "upfl" [114]=> string(8) "9,100.00" [115]=> string(6) "-90.00" [116]=> string(3) "120" [117]=> string(4) "mffl" [118]=> string(6) "855.00" [119]=> string(6) "-43.33" [120]=> string(3) "300" [121]=> string(4) "siem" [122]=> string(8) "1,331.00" [123]=> string(6) "-38.00" [124]=> string(3) "600" [125]=> string(4) "pakt" [126]=> string(6) "620.00" [127]=> string(6) "-27.90" [128]=> string(5) "1,000" [129]=> string(3) "sfl" [130]=> string(6) "409.00" [131]=> string(6) "-15.90" [132]=> string(5) "9,400" [133]=> string(4) "gadt" [134]=> string(6) "286.00" [135]=> string(6) "-13.37" [136]=> string(6) "48,800" [137]=> string(4) "atba" [138]=> string(6) "394.79" [139]=> string(6) "-12.96" [140]=> string(3) "900" [141]=> string(3) "btl" [142]=> string(6) "265.51" [143]=> string(5) "-8.49" [144]=> string(5) "1,200" [145]=> string(4) "natf" [146]=> string(6) "616.00" [147]=> string(5) "-8.26" [148]=> string(5) "1,200" [149]=> string(4) "admm" [150]=> string(6) "121.51" [151]=> string(5) "-6.39" [152]=> string(6) "98,500" }
我想找到位于23号钥匙的“kse 100 kse 30 kmi 30” 我已经尝试了in_array和其他php数组函数,包括preg_match,但它没有在数组中找到字符串。有没有办法在我的数组中找到字符串。如果是,请告诉我
答案 0 :(得分:0)
您应该使用array_search
在数组中搜索给定值,并在成功时返回相应的键
所以它必须像..
echo $key = array_search('kse 100 kse 30 kmi 30', $yourarray); //"prints" 23
<强> EDIT:
强>
<?php
$array = [
0 => "colg - colgate palmolive (pakistan) ltd. ",
1 => "gatm - gul ahmed textile mills ltd. ",
2 => "atlh - atlas honda limited ",
3 => "ffbl - fauji fertilizer bin qasim",
4 => " - general news",
5 => "searl - the searl company ltd. ",
23 => " kse 100 kse 30 kmi 30 ",
24 => "26,730.82",
];
$array = array_map('trim',$array);
$searchText = 'kse 100 kse 30 kmi 30';
$searchText = trim($searchText);
echo $key = array_search($searchText, $array); //"prints" 23
答案 1 :(得分:0)
你的“kse 100 kse 30 kmi 30”在数组中的字符串之前和之后都有一个空格“”。
string(136) " kse 100 kse 30 kmi 30 "
这是你找不到它的原因吗?因为array_serach,in_array和其他函数正在寻找精确的值。
否则使用strstr比较你的字符串是否在大海捞针内但要小心。如果您的阵列中有类似“Hello”和“Hello World”的类似内容,您可能会发现错误的条目并不是一个很好的独特解决方案。
无论如何,我要留下一个简单的例子:
$found = false;
foreach($myArray as $key => $value) {
if(strstr($value, "kse 100 kse 30 kmi 30") == true) {
$found = true;
break;
}
}
if($found == true) {
//Do something awesome!
} else {
//Do something not so awesome but still cool?
}
答案 2 :(得分:0)
谢谢你的帮助,但我自己解决了
for ($index = 10; $index < 35; $index++) {
if (substr_count(array[$index], 'kse 100') > 0) {
$IndexKey = $index;
}
}
获得该变量所在的键的值。
答案 3 :(得分:0)
对于匹配的第一个:
$key = key(preg_grep('/kse 100/', $array));