ホームページ >バックエンド開発 >PHPチュートリアル >このような配列値を取得するにはどうすればよいでしょうか?
配列(100) {
[0] => 文字列(3) "1_1"
[2] => 文字列(3) "2_2"
[ 3] => 文字列(3) "3_1"
[4] => 文字列(3) "3_2"
.....
}
「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");$need="3_";$ret = array_filter($data,function($item) use($need){ return strpos($item,$need) === 0;});var_dump($ret);
$d = array( "1_1", "2_1", "2_2", "3_1", "3_2", "3_3",);$in = '3_';$out = array_values( array_map(function($s) use (&$in) { return substr($s, strlen($in)); }, preg_grep("/$in/", $d)));print_r($out);
Array( [0] => 1 [1] => 2 [2] => 3)
$data =array("1_1","2_1","2_2","3_1","3_2","3_3");$need="3_";$ret = array_filter($data,function($item) use($need){ return strpos($item,$need) === 0;});var_dump($ret);
ああ、後半だけ
$data =array("1_1","2_1 ","2_2"," 3_1 ","3_2","3_3");
$need="3_";