# Get part of Array

Hi E's, I try to know if have any easy way to get part of the array values in specif intervals.
The only way I now do that is using foreach with a counter, but I try to found a easy way to do that!
In practice I need to get the values of array, from [0] to [24] ant [36] to [170].

Have a easy way to get that intervals of array values?

The best regards, JC
LVL 3
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You could do it like this:

\$array = // this is your array, whatever it's called, we'll call it array for now

\$min = 0;
\$max = 24;

foreach (\$array as \$key => \$var) {

if (\$key > \$min && \$key < \$max) {

\$final[] = \$var;

}

// this is the final array
var_dump(\$final);

// do it again for 36 - 170

\$final = array();

\$min = 36;
\$max = 170;

foreach (\$array as \$key => \$var) {

if (\$key > \$min && \$key < \$max) {

\$final[] = \$var;

}

// this is the final array
var_dump(\$final);
0
Commented:
Assuming the interval is constant, this could be done using the % operator.
\$my_array = array('Apple','Orange','Pear','Tomatoe','Bannana');
\$interval = 3;

foreach(\$my_array as \$key =>\$data){
if(\$key % \$interval == 0){
echo \$data;
}
}
0
Commented:
Sorry, misread the question. Thought you wanted specific indexes, not ranges.
0
Commented:
Also, this can be accomplished by using ranges, as CKY092 said.  See this for reference:

http://php.net/manual/en/function.range.php
0
WebmasterAuthor Commented:
Hi @EMB01, your script work almost well. Almost well because have a little bug. In line3 if the value was 0, the first value of array it was the second line of the file. So, for I get the first line of the file I have to change the value of \$min from 0 to -1, and in that way work well!
If not give you too much work, can you change your code for \$min = 0 be the first line of the file?

Regards, JC
0
Commented:
I'm not sure I understand.  If you could post the output of your array, it would help me understand.  You can post it by running this code on your array:

die(var_dump(\$yourArray));
0
WebmasterAuthor Commented:
The output is: array(4) { [0]=> string(6) "" [1]=> string(46) "" [2]=> string(72) "" [3]=> string(78) "

Is better I explain.
First I get the array values from a file, like: ||||||||\\$lines = explode("\n", file_get_contents('index.htm'));|||||||
The file index.htm is composed:
======================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<TITLE>Fotos do Azinhal (Castro Marim)</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META name="Robots" content="all">
<META name="Language" content="pt">
<META name="Distribution" content="Global">
<META name="Rating" content="General">
<META name="Author" content="Pedro Chagas">
<META name="Keywords" content="fotos, portugal, fotografia digital, imagens, azinhal, arte digital, cameras digitais, fotos digitais, ferias, viagem, voos, turismo rural, algarve, hotel, ">
=====================================
If \$min = -1 and \$max = 10 the result is:
====================================
Array
(
[0] => <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
[1] => <html>
[3] => <TITLE>Fotos do Azinhal (Castro Marim)</TITLE>
[4] => <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
[5] => <META name="Robots" content="all">
[6] => <META name="Language" content="pt">
[7] => <META name="Distribution" content="Global">
[8] => <META name="Rating" content="General">
[9] => <META name="Author" content="Pedro Chagas">
)
===========================================
If \$min = 0 and \$max = 10 the result is:
==========================================
Array
(
[0] => <html>
[2] => <TITLE>Fotos do Azinhal (Castro Marim)</TITLE>
[3] => <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
[4] => <META name="Robots" content="all">
[5] => <META name="Language" content="pt">
[6] => <META name="Distribution" content="Global">
[7] => <META name="Rating" content="General">
[8] => <META name="Author" content="Pedro Chagas">
)
============================================
The difference between the 2 result is:
with -1 appear [0] => <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> and with 0 not appear.

Regards, JC

0
Commented:
Quick and dirty without recursion:
\$endArray = array();

\$start1 = 0;
\$end1 = 24;
\$start2 = 36;
\$end2 = 170;

foreach (\$startArray as \$key => \$value) {
if ((\$value >= \$start1 && \$value <= \$end1) || (\$value >= \$start2 && \$value <= \$end2)) {
\$endArray[] = \$value;
}
}

print_r(\$endArray);
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
And another version if you actually meant the key rather than values as you stated:
\$endArray = array();

\$start1 = 0;
\$end1 = 24;
\$start2 = 36;
\$end2 = 170;

foreach (\$startArray as \$key => \$value) {
if ((\$key >= \$start1 && \$key <= \$end1) || (\$key >= \$start2 && \$key <= \$end2)) {
\$endArray[] = \$value;
}
}

print_r(\$endArray);
0
Commented:
Yes, sorry, I forgot the code should be like this:

\$array = // this is your array, whatever it's called, we'll call it array for now

\$min = 0;
\$max = 24;

foreach (\$array as \$key => \$var) {

if (\$key >= \$min && \$key <= \$max) {

\$final[] = \$var;

}

// this is the final array
var_dump(\$final);

// do it again for 36 - 170

\$final = array();

\$min = 36;
\$max = 170;

foreach (\$array as \$key => \$var) {

if (\$key >= \$min && \$key <= \$max) {

\$final[] = \$var;

}

// this is the final array
var_dump(\$final);
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.