This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to count non-empty entries in php array?
I want to count the number of keys in an array that aren't empty/null.
EG:
array(1>'asdf',2>'fdas',3>'');
count($array) would return 3, I want a function that returns 2 as 3 is empty
Are there any php built in functions to do this? How can it be done?
Use:
count(array_filter($array));
array array_filter ( array $input [, callable $callback = "" ] )
If no callback is supplied, all entries of input equal to FALSE (see
converting to boolean) will be removed.
Related
This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Why does counting an empty string return 1?
(3 answers)
Count() returning 1 with comma separated string with explode [duplicate]
(2 answers)
Closed 3 years ago.
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
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
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.
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
I set up a new site and want a workaround for the problem.
I have this text in the mysql table
{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}
I want to fetch the ID and its value by php
For example
$id[200] = 5
You can use json_decode() to convert JSON string into an array. Since your JSON string contains numeric indexes so you need to use curly brackets to fetch the values
$str = '{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}';
$array = json_decode($str);
echo $array->{'200'};
Or you can add second parameter in json_decode as true to fetch the value as normal array
$array = json_decode($str,true);
echo $array[200];
Here is the live demo
This question already has answers here:
How to add elements to an empty array in PHP?
(8 answers)
Closed 5 years ago.
Given this PHP array:
$options['systems'] = array(1, 2, 3)
How would I append the value 4 to the $systems array within the $options array?
You could use array_push to push additional items like so:
array_push($options['systems'], 4);
Or the shorthand version:
$options['systems'][] = 4;
You can use php array_push function. Like this. array_push($options['systems'],4);
you can read the detail of array_push from below link.array_push manual
This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 6 years ago.
I have a webservice that returns a very large json object. I decode this to an array for parsing.
What I'm trying to do though is extract all array members from the json array where there is [id] => 21 at the start of the individual array object.
A nice simple task using LINQ, but not an idea how to do this in PHP. There has to be a way to filter through and extract.
I'd use something like array_filter for this
function find_id($var)
{
// returns whether the is is 21
return($var['id'] === 21);
}
$result = array_filter($jsondata, "find_id"));
array_filter Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.
This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed 6 years ago.
array_filter only passes array values, not keynames... how to access keyname?
for example:
$array= array('country'=>'Spain');
array_filter($array,'my_func');
with my_func i want to check:
if KEYNAME == 'country' then ...
array_filter can pass them.
array_filter($array, 'my_func', ARRAY_FILTER_USE_BOTH);
and my_func will then be passed two params; the key, and the value.
array_filter doc page