My first solution to your problem would be to initially define $ranksAllowed
as an array instead of a pipe-character-delimited string:
$ranksAllowed = array(1, 2, 3, 4, 67, 7);
This would make more sense in almost any foreseeable situation. If for some reason you'd rather keep $ranksAllowed
as a string...
Some simplification
$rank_ids = explode('|', trim("|".$ranksAllowed."|", '|'));
can be simplified to:
$rank_ids = explode('|', trim($ranksAllowed, '|'));
Decide on string or array format
Right now it looks like you're trying to do 2 things at once (and achieving neither)
One possibility is you want to turn your pipe-delimited ("|1|2|3|...") string into a comma delimited string (like "1, 2, 3, ..."). In this case, you could simply do a string replace:
$commaDelimited = str_replace('|', ',', trim($ranksAllowed, '|'));
The other possibility (and I believe the one you're looking for) is to produce an array of all the allowable ranks, which you've already accomplished in an earlier step, but assigned to $rank_ids
instead of $arrayofallowed
:
$arrayofallowed = explode('|', trim($ranksAllowed, '|'));
//Should print out data in array-format, like you want
print_r($arrayofallowed);
//Echo the length of the array, should be 6
echo count($arrayofallowed);
implode
returns a string; it converts an array to a string. So if you put that string inside a new array, as you have, you end up with an array containing one element. – Utkanos Oct 20 '14 at 21:09count($rank_ids)
while it is still an array. You could also use substr_count to count how many,
exist in the string and add 1. Edit: I guess since you are assigning the string to a new variable, you could justcount($rank_ids)
without moving it above the implode. – Jonathan Kuhn Oct 20 '14 at 21:13