0

Could somebody explaind me ***why array count is not 2

$value = '"305", "112", ';
//remove last comma and space
echo $value = rtrim($value, ', ');
echo '<br>';

$value = array($value);

echo $array_length = count($value); //***
6
  • 2
    why wouldn't it be? you only add one element to it? o.O Commented Mar 13, 2020 at 15:57
  • Also - print_r($value); Commented Mar 13, 2020 at 16:01
  • 1
    Perhaps you wanted str_getcsv($value)? Commented Mar 13, 2020 at 16:01
  • $value = array("305", "112"); so you will have two data in the array Commented Mar 13, 2020 at 16:02
  • @treyBake I have this value in mysql column $value = '"305", "112", '; and I think that this is 2 elements, is possible make it two, why I said is two because if I pass it here $value = array($value); is two elements Commented Mar 13, 2020 at 16:04

4 Answers 4

2

You should use explode function to get array like the following code :

$value = '"305", "112"'; 
        $value = rtrim($value, ', ');
        echo '<br>';
        $value = explode(',',$value); 
        echo $array_length = count($value); 
Sign up to request clarification or add additional context in comments.

Comments

1

you can use explode() to get it.

$value = '"305", "112", ';
//remove last comma and space
echo $value = rtrim($value, ', ');
echo '<br>';

$value = explode(',',$value);

echo $array_length = count($value); 

1 Comment

Note you'll have two string values that contain double quotes surrounding each number in your array.
0

you can also do this if you want to create an array from that these type of strings and do array count.

$arr = array_filter(array_map('trim', str_getcsv($value)));
print_r($arr);
echo count($arr);

Comments

0

when you are creating an array with array($value), the entire value of $value counted as a single element of an array. Thats why you are getting count($value) not equal to 2.

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.