2

Let's say I have an array like the following.

[0] => Array
    [0] => Peter drives 45km to work
    [1] => Tom rides 32km with his friends
    [2] => Lisa walks 6km to the gym
    [3] => Bob cycles 12km to keep fit
    [4] => Bill takes the train 63km to work
    [5] => Penny runs 8km to the shop
    [6] => Robert takes a taxi 21km to the museum

Suppose I want to keep only the people who travelled between 10-15km and remove all others from the array. Also, suppose the range I want to use is variable, ie: today I might like to see who travelled between 10-15km, but tomorrow I might like to see who travelled between 5-15km, then the next day I might like to see who travelled between 30-50km.

How would I go about searching through this array and either removing all the elements that do not fall within my specified range, or moving the elements I require to a new array?

With regard to the elements I require, I need to retain the entire value, not just the distance travelled.

WiLDRAGoN
  • 77
  • 1
  • 7
  • possible duplicate of [Delete an element from an array](http://stackoverflow.com/questions/369602/delete-an-element-from-an-array) – Risadinha Jun 14 '15 at 10:47
  • You should take a look at the php documentation. It offers a list of available array functions. Amongst those are filter functions. And there you should check out the variants that allow to specify a callback function for the actual filter decision. In that callback you define you have to use a regex to extract the number from the string and then make your decision. – arkascha Jun 14 '15 at 10:50
  • @Risadinha This certainly is _not_ a duplicate of that question. Please read questions till the end... – arkascha Jun 14 '15 at 11:07

2 Answers2

2

You can use php's array filter functions in combination with a callback that uses a regular expression to extract the relevant number from the strings:

<?php

$min = 10;
$max = 20;
$input = [
  "Peter drives 45km to work",
  "om rides 32km with his friends",
  "Lisa walks 6km to the gym",
  "Bob cycles 12km to keep fit",
  "Bill takes the train 63km to work",
  "Penny runs 8km to the shop",
  "Robert takes a taxi 21km to the museum",
];

$output = array_filter($input, function($string) use ($min, $max) {
  preg_match('/([0-9]+)km/', $string, $matches);
  $number = intval($matches[1]);
  return ($number>$min) && ($number<$max);
});

print_r($output);

The output of this example (with the given values for $min and $max) is:

Array
(
    [3] => Bob cycles 12km to keep fit
)

Different approaches are possible, here I used a "closure" to keep things compact. Obviously the values in $min and $max are the ones you want to define in a dynamic manner. Some error handling should be added inside the callback function in case the input strings have unexpected format. I left that out here to keep things compact again...

arkascha
  • 41,620
  • 7
  • 58
  • 90
0

If you remove any item from your array.

unset($a[2]);

$a is variable which is contain array.

Jakir Hossain
  • 2,457
  • 18
  • 23