방법 1
$arr_main_array = array('test_val' => 123, 'other-value' => 456, 'test_result' => 789);
foreach($arr_main_array as $key => $value){
$exp_key = explode('-', $key);
if($exp_key[0] == 'test'){
$arr_result[] = $value;
}
}
if(isset($arr_result)){
print_r($arr_result);
} 방법 2
A functional approach
An array_filter_key type of function is taken, and applied to the array elements
$array = array_filter_key($array, function($key) {
return strpos($key, 'foo-') === 0;
}); 방법 3
절차적 접근 -
$val_1 = array();
foreach ($array as $key => $value) {
if (strpos($key, 'foo-') === 0) {
$val_1[$key] = $value;
}
} 방법 4
객체를 사용한 절차적 접근 -
예시
$i = new ArrayIterator($array);
$val_1 = array();
while ($i->valid()) {
if (strpos($i->key(), 'foo-') === 0) {
$val_1[$i->key()] = $i->current();
}
$i->next();
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Array(test_val => 123 test_result => 789)