文字列(3) "2_1" [2] => 文字列(3) "2"/> 文字列(3) "2_1" [2] => 文字列(3) "2">
このような配列値を取得するにはどうすればよいでしょうか?
配列(100) {
[0] => 文字列(3) "1_1"
[1] => 文字列(3) "2_1"
[2] => (3) "2_2"
[3] => 文字列(3) "3_1"
[4] => 文字列(3) "3_2"
[5] => ) "3_3"
.....
}
「3_」と入力すると、
array(3) {
["0"] => を取得します。 string(1) "1"
["1"] => string(1) "2"
["2"] => 選び方?
------解決策のアイデア----------------------
$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);
$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);
Array<br />(<br /> [0] => 1<br /> [1] => 2<br /> [2] => 3<br />)<br /><br />