Simple, here you go
$number='3';
$stdt= ["3-45", "4-35"];
array_map(function($item)use($number){if(0===strpos($item,$number.'-'))echo substr($item,strlen($number.'-'))."\n";},$stdt);
Output
45
Sandbox
UPDATE1
I thought about "golfing" it ... ha ha (97 bytes 92 without the newline).
$n='3';
$a= ["3-45", "4-35"];
array_map(function($i)use($n){$n.='-';echo 0===strpos($i,$n)?substr($i,strlen($n))."\n":'';},$a);
Smallifed Version
It even correctly accounts for things like
$number = 3;
//where 33 could be matched by "false !== strpos($i,$n)"
$stdt= ["3-45", "4-35", "33-99"];
I will explain the smallified version as its more complex.
array_map walks though an array and applies a callback function, you can think of it like a loop.
- in this case its a user supplied closure, with the input if
$i
- we need the number
$n in so we pass it in by using use($n)
- foreach loop, we append the
- to the number. $n=3 becomes $n='3-'
- this just shortens things for us latter on
- we use a shorthand "ternary" if statement it takes the from of
condition ? if true : if false;
strpos - check the position of one string in another and returns it
- in this case we want to check if
$n='3-' is 0 or the first position (0 based index)
- by adding the
- to the number we can be sure it doesn't match things like $i='33-'. For example 3- does not match 33- but 3 can.
- we use
=== strict type checking because strpos returns boolean false if it's not in the string and false == 0 is true, but false === 0 is not because they are different types.
- [IF TRUE] then we basically remove
$n from $i.
- we can use the length of
$n which is $n='3-' or 2 to get the start position for sub string. So we could write it like substr('3-45', 2) but because $n can be anything, it's better to measure it
- [IF false] we just return an empty string which gets returned to echo.
Expanded Version Of Code:
array_map(
function( $i ) use ( $n ){
$n .= '-';
echo (0 === strpos( $i, $n ) ) ? substr( $i, strlen( $n ) ) : '';
},
$a);
Difference between 1 & 2
The first version is basically the same, but I realized I could shorten it by adding the - on to the number before hand instead of appending it twice.
The major differences are
- shortened the variables to 1 letter (just to save space)
- Using ternary instead of a normal
if statement
UPDATE2
Last thing I will do is write a Bigger more normal looking version:
$number = 3;
$stdt= ["3-45", "4-35", "33-99", '3-33'];
//makes life easier, also solves some issues by converting number to a string.
//We set it outside the loop for performance, we could add it with . every time but that's just waste.
$n = $number .'-';
//length of n
$l = strlen($n);
foreach($stdt AS $st){
//find the position of $n, 0 = first, false = not found
//again === strict type chcking
if(0 === ($pos = strpos($st, $n))){
//use the length of $n (one of those makes life easier things)
//here we can use substr its much safer then trim.
echo substr($st, $l))."\n";
}
}
Bigified Version
There is probably a million ways you can do this. I did the above because it gets boring so it's fun to try to make it as small and short as possible.
Cheers!
:-p - thanks for the fun!
PS I got rid of the one with trim, it's to problematic and hard to explain when they remove stuff in different ways. Now they are are roughly equivalent.