0

I am trying to use sizeof or count to return the number of things inside the array, however whenever I use the $rank_ids2 to count rather than entering 1, 2, 3, 4, 67, 7 in manually, it just returns 1, but when I type them in the array directly, it counts 6 just fine!

$ranksAllowed = '|1|2|3|4|67|7|';

$rank_ids = explode('|', trim("|".$ranksAllowed."|", '|'));
$rank_ids2 = implode(", ", $rank_ids);

$arrayofallowed = array($rank_ids2);
echo sizeof($arrayofallowed);

$rank_ids is just turning the |1|2|.. format into 1, 2

Jayden
  • 437
  • 2
  • 7
  • 11
  • 4
    `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. – Mitya Oct 20 '14 at 21:09
  • 1
    If you want to count how many "ranks" are there, move your count to above the implode and `count($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 just `count($rank_ids)` without moving it above the implode. – Jonathan Kuhn Oct 20 '14 at 21:13

3 Answers3

1

implode converts an array to a string, so after everything you get this:

  • A string named $ranksAllowed that contains |1|2|3|4|67|7|
  • An array named $rank_ids that contains multiple elements, being them 1, 2, etc...
  • A string named $rank_ids2 that contains 1,2,3,4,67,7
  • An array named $arrayofallowed with only 1 element, being it the string inside $rank_ids2

To achieve a string that contains 1,2,3,4,67,7 from |1|2|3|4|67|7| you can just trim the | character as you do and replace | with ,. Is less CPU expensive.

$rank_ids2 = str_replace("|", ",", trim("|".$ranksAllowed."|", '|'));

If you want to count them you can explode it and count the elements:

$rank_ids2_array = explode(',', $rank_ids2);
echo sizeof($rank_ids2_array);

or with your code you can simply count the already exploded $rank_ids.

echo sizeof($rank_ids);
Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
0

Try something like the following:

$ranksAllowed = '|1|2|3|4|5|67|7|';
$rank_ids = explode('|', trim($ranksAllowed, '|'));
echo count($rank_ids);

Just to explain the above, the $arrayofallowed is imploding the array of $rank_ids, creating a string. This will not give you the expected results when counted. If you simply count the $rank_ids (as explode() will leave you with an array), you should see the desired result of 7 items.

The $rank_ids is your array, and $arrayofallowed was a string.

Please see the sections of the PHP manual related to the implode() and explode() functions for more information.

turbo
  • 76
  • 2
0

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); 
Gershom Maes
  • 7,358
  • 2
  • 35
  • 55