Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

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
Avatar of EMB01
EMB01
Flag of United States of America image

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);

Open in new window

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;
	}
}

Open in new window

Sorry, misread the question. Thought you wanted specific indexes, not ranges.
Also, this can be accomplished by using ranges, as CKY092 said.  See this for reference:

http://php.net/manual/en/function.range.php
Avatar of Pedro Chagas

ASKER

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
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));
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>
<head>
<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="Copyright" content="PCSXXI 2005">
<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>
    [2] => <head>
    [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>
    [1] => <head>
    [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

ASKER CERTIFIED SOLUTION
Avatar of GreatGerm
GreatGerm
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial