PHP Принадлежит ли число интервалу?
Всем привет, вопрос следующий.
К примеру у нас есть массив
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .php.geshi_code {font-family:monospace;} .php.geshi_code .imp {font-weight: bold; color: red;} .php.geshi_code .kw1 {color: #b1b100;} .php.geshi_code .kw2 {color: #000000; font-weight: bold;} .php.geshi_code .kw3 {color: #990000;} .php.geshi_code .co1 {color: #666666; font-style: italic;} .php.geshi_code .co2 {color: #666666; font-style: italic;} .php.geshi_code .co3 {color: #0000cc; font-style: italic;} .php.geshi_code .co4 {color: #009933; font-style: italic;} .php.geshi_code .coMULTI {color: #666666; font-style: italic;} .php.geshi_code .es0 {color: #000099; font-weight: bold;} .php.geshi_code .es1 {color: #000099; font-weight: bold;} .php.geshi_code .es2 {color: #660099; font-weight: bold;} .php.geshi_code .es3 {color: #660099; font-weight: bold;} .php.geshi_code .es4 {color: #006699; font-weight: bold;} .php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;} .php.geshi_code .es6 {color: #009933; font-weight: bold;} .php.geshi_code .es_h {color: #000099; font-weight: bold;} .php.geshi_code .br0 {color: #009900;} .php.geshi_code .sy0 {color: #339933;} .php.geshi_code .sy1 {color: #000000; font-weight: bold;} .php.geshi_code .st0 {color: #0000ff;} .php.geshi_code .st_h {color: #0000ff;} .php.geshi_code .nu0 {color: #cc66cc;} .php.geshi_code .nu8 {color: #208080;} .php.geshi_code .nu12 {color: #208080;} .php.geshi_code .nu19 {color:#800080;} .php.geshi_code .me1 {color: #004000;} .php.geshi_code .me2 {color: #004000;} .php.geshi_code .re0 {color: #000088;} .php.geshi_code span.xtra { display:block; }
И список или массив чисел 1, 2, 3, 4, 5, 6, 7, 8, 9 и т.д.
Вопрос: Как нам их запихнуть в массив по интервалу?
К примеру у нас есть массив
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .php.geshi_code {font-family:monospace;} .php.geshi_code .imp {font-weight: bold; color: red;} .php.geshi_code .kw1 {color: #b1b100;} .php.geshi_code .kw2 {color: #000000; font-weight: bold;} .php.geshi_code .kw3 {color: #990000;} .php.geshi_code .co1 {color: #666666; font-style: italic;} .php.geshi_code .co2 {color: #666666; font-style: italic;} .php.geshi_code .co3 {color: #0000cc; font-style: italic;} .php.geshi_code .co4 {color: #009933; font-style: italic;} .php.geshi_code .coMULTI {color: #666666; font-style: italic;} .php.geshi_code .es0 {color: #000099; font-weight: bold;} .php.geshi_code .es1 {color: #000099; font-weight: bold;} .php.geshi_code .es2 {color: #660099; font-weight: bold;} .php.geshi_code .es3 {color: #660099; font-weight: bold;} .php.geshi_code .es4 {color: #006699; font-weight: bold;} .php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;} .php.geshi_code .es6 {color: #009933; font-weight: bold;} .php.geshi_code .es_h {color: #000099; font-weight: bold;} .php.geshi_code .br0 {color: #009900;} .php.geshi_code .sy0 {color: #339933;} .php.geshi_code .sy1 {color: #000000; font-weight: bold;} .php.geshi_code .st0 {color: #0000ff;} .php.geshi_code .st_h {color: #0000ff;} .php.geshi_code .nu0 {color: #cc66cc;} .php.geshi_code .nu8 {color: #208080;} .php.geshi_code .nu12 {color: #208080;} .php.geshi_code .nu19 {color:#800080;} .php.geshi_code .me1 {color: #004000;} .php.geshi_code .me2 {color: #004000;} .php.geshi_code .re0 {color: #000088;} .php.geshi_code span.xtra { display:block; }
И список или массив чисел 1, 2, 3, 4, 5, 6, 7, 8, 9 и т.д.
Вопрос: Как нам их запихнуть в массив по интервалу?
1 ответов
как такой вариант?
$a = array(
'1-5' => '',
'6-10' => '',
'11-15' => '',
);
print_r($a);
// Array ( [1-5] => [6-10] => [11-15] => ) Array ( [0] => 1 [1] => 5 )
foreach( $a as $key => & $b )
{
$c = explode('-', $key);
$b = range($c[0], $c[1]);
}
print_r($a);
/* Array ( [1-5] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) [6-10] => Array ( [0] => 6 [1] => 7 [2] => 8 [3] => 9 [4] => 10 ) [11-15] => Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 [4] => 15 ) ) */
v.2
'1-5' => '',
'6-10' => '',
'11-15' => '',
);
print_r($a);
// Array ( [1-5] => [6-10] => [11-15] => ) Array ( [0] => 1 [1] => 5 )
foreach( $a as $key => & $b )
{
$c = explode('-', $key);
$b = range($c[0], $c[1]);
}
print_r($a);
/* Array ( [1-5] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) [6-10] => Array ( [0] => 6 [1] => 7 [2] => 8 [3] => 9 [4] => 10 ) [11-15] => Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 [4] => 15 ) ) */
v.2
$a = array(
'1-5' => '',
'6-10' => '',
'11-15' => '',
);
$d = array(1, 2, 3, 4, 6, 7, 8, 9, 12, 15);
foreach( $a as $key => & $b )
{
$c = explode('-', $key);
$e = range($c[0], $c[1]);
foreach( $d as $k => $f )
{
if( in_array($f, $e) )
{
$b[] = $f;
unset($d[$k]);
}
}
}
print_r($a);
где $d массив заданых чисел
'1-5' => '',
'6-10' => '',
'11-15' => '',
);
$d = array(1, 2, 3, 4, 6, 7, 8, 9, 12, 15);
foreach( $a as $key => & $b )
{
$c = explode('-', $key);
$e = range($c[0], $c[1]);
foreach( $d as $k => $f )
{
if( in_array($f, $e) )
{
$b[] = $f;
unset($d[$k]);
}
}
}
print_r($a);
где $d массив заданых чисел