1
$target = 285
$array = array("260-315", "285-317", "240-320")

I need to search the array for the value that begins with the $target value. Also, the $target value will not be limited to 3 digits so I'm searching for a match of the digits before the hyphen.

So I want to end up with

$newTarget = 285-317
$finalTarget = 317

Note: I'm only searching for a match of the digits before the hyphen so "200-285" would not be a match

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
user300979
  • 389
  • 1
  • 4
  • 18

6 Answers6

1

What you asked me in comment(below my answer),for that you can do it like below (My changed answer):-

<?php
$target = 285;
$array = array('260-315', '285-317', '240-320',"200-285");
foreach($array as $key=>$value){
    if($target ==explode('-',$value)[0]){
       echo $newTarget = $array[$key];
       echo PHP_EOL;
       echo $finalTarget = explode('-',$array[$key])[1];
    }
}
?>

https://eval.in/702862

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • What if one of the values is 200-285? The digits before the hyphen are the target. – user300979 Dec 22 '16 at 13:14
  • What I mean is the only digits I'm trying to find a match with are the digits before the hyphen. So if the target is 285 then 200-285 should not be a match. – user300979 Dec 22 '16 at 13:22
0

I can help you filter your array down to members that start with your target.

You can then split the return values to get to your final target.

<?php
$target = '285';
$array = array('260-315', '285-317', '240-320');


$out = array_filter($array, function($val) use ($target) {
    return strpos($val, $target) === 0;
});

var_export($out);

Output:

array (
  1 => '285-317',
)
Progrock
  • 7,373
  • 1
  • 19
  • 25
0

Instead of finding what matches, you could exclude what doesn't match with array_filter.

For example:

$target = 285;

$original = array('260-315', '285-317', '240-320');

$final = array_filter($original, function ($value) use ($target) {

    // Check if match starts at first character. Have to use absolute check
    // because no match returns false
    if (stripos($value, $target) === 0) {

       return true;

    }

    return false;

});

The $final array will be a copy of the $original array without the non-matching values.

To output the first digits, you can then loop through your array of matches and get the value before the hyphen:

foreach ($final as $match) {

    $parts = explode('-', $match);

    if (is_array($parts) && ! empty($parts[0])) {

        // Show or do something with value
        echo $parts[0];

    }
}
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
0

Something like this could work for you ? array_filter

$target = 285;
$array = array("260-315", "285-317", "240-320");
$newTarget = null;
$finalTarget = null;

$filteredArray = array_filter($array, function($val) use ($target) {
    return strpos($val, $target."-") === 0;
});

if(isset($filteredArray[0])){
    $newTarget = $filteredArray[0];
    $finalTarget = explode($filteredArray[0], "-")[1];
}
Gauthier
  • 1,116
  • 2
  • 16
  • 39
0
<?php
$target = 285;
$arrStack = array(
    "260-315",
    "285-317",
    "240-320",
);


$result = preg_grep('/'.$target.'/',$arrStack);

echo "<pre>"; print_r($result); echo "</pre>";
Henry
  • 597
  • 4
  • 12
0

Use array_filter:

Example:

$target = '260';
$array = ['260-315', '285-317', '240-320'];

$matches = array_filter($array, function($var) use ($target) { return $target === explode('-', $var)[0]; });

print_r($matches);

Output:

Array
(
    [0] => 260-315
)