1

I have an array like this,

$array = array(
    1,2,3,'4>12','13.1','13.2','14>30'
);

I want to find any value with an ">" and replace it with a range().

The result I want is,

array(
    1,2,3,4,5,6,7,8,9,10,11,12, '13.1', '13.2', 14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
);

My understanding:

if any element of $array has '>' in it,

    $separate = explode(">", $that_element);

    $range_array = range($separate[0], $separate[1]); //makes an array of 4 to 12.

Now somehow replace '4>12' of with $range_array and get a result like above example.

May be I can find which element has '>' in it using foreach() and rebuild $array again using array_push() and multi level foreach. Looking for a more elegant solution.

3 Answers 3

2

You can even do it in a one-liner like this:

$array = array(1,2,3,'4>12','13.1','13.2','14>30');
print_r(array_reduce(
 $array,
 function($a,$c){return array_merge($a,@range(...array_slice(explode(">","$c>$c"),0,2)));},  
 []
));

I avoid any if clause by using range() on the array_slice() array I get from exploding "$c>$c" (this will always at least give me a two-element array).

You can find a little demo here: https://rextester.com/DXPTD44420

Edit:

OK, if the array can also contain non-numeric values the strategy needs to be modified: Now I will check for the existence of the separator sign > and will then either merge some cells created by a range() call or simply put the non-numeric element into an array and merge that with the original array:

$array = array(1,2,3,'4>12','13.1','64+2','14>30');
print_r(array_reduce(
 $array, 
 function($a,$c){return array_merge($a,strpos($c,'>')>0?range(...explode(">",$c)):[$c]);}, 
 []
));

See the updated demo here: https://rextester.com/BWBYF59990

Sign up to request clarification or add additional context in comments.

2 Comments

It does take less time to execute than other solutions so if above problem can be solved I will accept this answer.
Now it works perfectly. The numbers of "x>y" are always numeric so no problem there.
1

It's easy to create an empty array and fill it while loop a source

$array = array(
    1,2,3,'4>12','13.1','13.2','14>30'
);

$res = [];

foreach($array as $x) {
    $separate = explode(">", $x);
    if(count($separate) !== 2) {
        // No char '<' in the string or more than 1
        $res[] = $x;
    }
    else {
        $res = array_merge($res, range($separate[0], $separate[1]));
    }
}

print_r($res);

Comments

1

range function will help you with this:

$array = array(
    1,2,3,'4>12','13.1','13.2','14>30'
);
$newArray = [];
foreach ($array as $item) {
    if (strpos($item, '>') !== false) {
        $newArray = array_merge($newArray, range(...explode('>', $item)));
    } else {
        $newArray[] = $item;
    }
}

print_r($newArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.