Solved

# Get part of Array

Posted on 2011-10-05
428 Views
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
0
Question by:Pedro Chagas

LVL 14

Expert Comment

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

LVL 4

Expert Comment

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

LVL 4

Expert Comment

Sorry, misread the question. Thought you wanted specific indexes, not ranges.
0

LVL 14

Expert Comment

Also, this can be accomplished by using ranges, as CKY092 said.  See this for reference:

http://php.net/manual/en/function.range.php
0

LVL 3

Author Comment

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

LVL 14

Expert Comment

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

LVL 3

Author Comment

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

LVL 17

Accepted Solution

Quick and dirty without recursion:
``````\$startArray = YOUR ARRAY;
\$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

LVL 17

Expert Comment

And another version if you actually meant the key rather than values as you stated:
``````\$startArray = YOUR ARRAY;
\$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

LVL 14

Assisted Solution

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

## Featured Post

### Suggested Solutions

PhpMyAdmin, mysql database table editing 4 30
Json result print in html 31 59
Amazon Product image url. 12 25
php explode 3 28
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…