string(3) "2_1"  [2] => string(3) "2"/>  string(3) "2_1"  [2] => string(3) "2">
Home > Backend Development > PHP Tutorial > 这样获取数组值

这样获取数组值

WBOY
Release: 2016-06-13 12:08:59
Original
878 people have browsed it

这么获取数组值?
array(100) {
  [0] => string(3) "1_1"
  [1] => string(3) "2_1"
  [2] => string(3) "2_2"
  [3] => string(3) "3_1"
  [4] => string(3) "3_2"
  [5] => string(3) "3_3"
  .....
}


输入"3_"就得
array(3) {
  ["0"] => string(1) "1"
  ["1"] => string(1) "2"
  ["2"] => string(1) "3"
请问这么取?
------解决思路----------------------

$data =array("1_1","2_1","2_2","3_1","3_2","3_3");<br />$need="3_";<br /><br /><br />$ret = array_filter($data,function($item) use($need){<br />	return strpos($item,$need) === 0;<br />});<br /><br />var_dump($ret);
Copy after login

------解决思路----------------------
$d = array(<br />  "1_1",<br />  "2_1",<br />  "2_2",<br />  "3_1",<br />  "3_2",<br />  "3_3",<br />);<br />$in = '3_';<br />$out = array_values(<br />  array_map(function($s) use (&$in) {<br />    return substr($s, strlen($in));<br />  }, preg_grep("/$in/", $d))<br />);<br />print_r($out);
Copy after login
Array<br />(<br />    [0] => 1<br />    [1] => 2<br />    [2] => 3<br />)<br /><br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template