1

i have this data which when tested with direct input it works and file_put_contents confrims that data is exactly same but when i try to get the value through site it gives only 1 i tried to declare array but everytime it gives 1 only

this array count gives count as 6

$total_id_counttt = count(array(13068,13067,13066,13065,13064,13063));
echo $total_id_counttt;

but when i use this here it return 1

$str_finalppo ='13068,13067,13066,13065,13064,13063';    
$schools_array = array($str_finalppo);
$total_id_counttt = count($schools_array);

it returns 1 can anyone tell where i am doing mistake

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
sonamona
  • 41
  • 1
  • 3

2 Answers2

2

First of all, Convert the string into an array. Look it in this code you may get the idea.

Use explode function to convert string to array.

$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
$total_id_counttt = count($schools_array);

Check more about explode

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
  • Pay attention. Do: $schools_array = ($str_finalppo != '') ? explode(",", $str_finalppo) : array(); Because count return 1 if string is empty – Gianluca Demarinis Feb 05 '20 at 14:50
0

In PHP, value in quotes('') consider as string. So when you try to count this is always returning the 1.

Try to convert the string into an array by using explode().

for example:

$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
echo $total_id_counttt = count($schools_array);

this will give you the right answer. And if you still confuse try to print @schools_array before using dd($schools_array); It will definitely remove your confusion.

Rahul Gupta
  • 991
  • 4
  • 12