Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Get part of Array

Posted on 2011-10-05
10
Medium Priority
?
439 Views
Last Modified: 2012-05-12
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
Comment
Question by:Pedro Chagas
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:EMB01
ID: 36919664
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

0
 
LVL 4

Expert Comment

by:CKY092
ID: 36919681
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

0
 
LVL 4

Expert Comment

by:CKY092
ID: 36919726
Sorry, misread the question. Thought you wanted specific indexes, not ranges.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:EMB01
ID: 36919959
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

by:Pedro Chagas
ID: 36920595
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

by:EMB01
ID: 36921426
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

by:Pedro Chagas
ID: 36921517
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

0
 
LVL 17

Accepted Solution

by:
GreatGerm earned 800 total points
ID: 36921577
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);

Open in new window

0
 
LVL 17

Expert Comment

by:GreatGerm
ID: 36921582
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

0
 
LVL 14

Assisted Solution

by:EMB01
EMB01 earned 1200 total points
ID: 36921636
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);

Open in new window

0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question