I have an array range $row['ADDwav'] - $row['ADDwav16']
How can I count the number of these that isset() and not NULL?
You can use array_filter with is_null (as callback) for that:
count(array_filter($row, "is_null"))
If you want the ones that are not null, then you'd have to subtract that from the original array length still count($row) - count(array_filter(...))
Try something like that
$counter = 0;
foreach($row as $r)
{
if($r !== null)
{
$counter++;
}
}
echo "Total not null items : " . $counter;
The above code will work. Also the values into foreach are all set, otherwhise will not loop through them ;)
In case your array is like that :
$row = array(
'ADDwav' => null,
'ADDwav1' => 'somevalue',
'ADDwav2' => 'anothervalue',
'ADDwav3' => '',
'ADDwav...' => '...',
'ADDwav16' => null
);
and you like to count the values that are not empty or null then you have to modify the code in the next form :
$counter = 0;
foreach($row as $k => $r)
{
if($r !== null || $r !== '')
{
$counter++;
}
}
echo "Total not null items : " . $counter;
use array_filter and is_null
foreach($row as $r)
if($r !== null)
...
..
.
This one will remove the array which is false or null...
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
);
echo count(array_filter($entry));
Related
name="qty<?php echo $key?>"
foreach ($_POST as $items => $value)
{
// check qty >1???
echo $key, ' => ', $value, '<br />';
}
How do I show only items which their values of [qty1]=>value,[qty2]=>value... >0 ?
Just use combination of array_filter and print_r.
$_POST = [
'notme' => 12,
'qty1' => 1,
'qty2' => 20,
'qty3' => -1,
'qty4' => 0,
'qty5' => 30
];
print_r(
array_filter($_POST, function ($value, $key) {
// check key starts with 'qty' and its value is greater than one
return strpos($key, 'qty') === 0 && $value > 1;
}, ARRAY_FILTER_USE_BOTH)
);
// Array ( [qty2] => 20 [qty5] => 30 )
Why don't you add a check in your loop and then get a filtered output like this:
if your POST request is not a multidimensional array then use this:
$output = array_filter($_POST, function ($value, $key) {
// check your keys start with 'qty' and its value is greater than one
return strpos($key, 'qty') === 0 && $value > 1;
}, ARRAY_FILTER_USE_BOTH);
// display final output
print_r($output);
If your POST request is a multidimensional array then use this:
$output = array(); // declare a blank array to store filtered output
foreach($_POST as $row)
{
if (is_array($row))
{
// this code is because qty is dynamic and we need to check it for all.
foreach($row as $key => $value)
{
if ("qty" == substr($key, 0, 3) && $value > 0)
{
$output[] = $row;
}
}
}
}
// display final array.
print_r($output);
Hope that will help!
Say I have an array that looks like this:
Array
(
[0] =>
[1] => 2017-01-01 00:00:00
)
How can I dynamically check to see if the area has any empty values?
You can use empty():
$array = [
null,
'2017-01-01 00:00:00',
'',
[],
# etc..
];
foreach($array as $key => $value){
if(empty($value)){
echo "$key is empty";
}
}
See the type comparison table for more information.
You can see if it has any empty values by comparing the array values to the result of array_filter (which removes empty values.)
$has_empty_values = $array != array_filter($array);
Something like
// $array = [ ... ];
$count = count($array);
for( $i=0; $i<=$count; $i++){
if( $array[$count] == 0 ){
// Do something
}
}
For this you have more possibility:
You can use array_filter function without without a second parameter
array_filter([ 'empty' => null, 'test' => 'test']);
but for this be carful because this remove all values which are equal false (null, false, 0)
You can use array_filter function with callback function:
function filterEmptyValue( $value ) {
return ! empty( $value );
}
array_filter([ 'empty' => null, 'test' => 'test'], 'filterEmptyValue');
You can use foreach or for:
$array = ['empty' => null, 'test' => 'test'];
foreach($array as $key => $value) {
if(empty($value)) {
unset($array[$key]);
}
}
$array = [null, 'test'];
for($i = 0; $i < count($array); $i++){
if(empty($array[$i])) {
unset($array[$i]);
}
}
This is samples so you must think and make good solution for your problem
I have two sets of arrays coming from $_POST. Keys for both will be numeric and the count will be the same, since they come in pairs as names and numbers:
$_POST[names]
(
[0] => First
[1] => Second
[2] =>
[3] => Fourth
)
$_POST[numbers]
(
[0] => 10
[1] =>
[2] => 3
[3] => 3
)
Now I need to combine those two, but remove each entry where either values are missing.
The result should be something like:
$finalArray
(
[First] => 10
[Fourth] => 3
)
Post data is dynamically created so there might be different values missing based on user input.
I tried doing something like:
if (array_key_exists('names', $_POST)) {
$names = array_filter($_POST['names']);
$numbers = array_filter($_POST['numbers']);
if($names and $numbers) {
$final = array_combine($names, $numbers);
}
}
But I can't seem to filter it correctly, since its giving me an error:
Warning: array_combine(): Both parameters should have an equal number of elements
How about using array_filter with ARRAY_FILTER_USE_BOTH flag on?
<?php
$array1 = [
0 => "First",
1 => "Second",
2 => "",
3 => "Fourth",
];
$array2 = [
0 => 10,
1 => "",
2 => 3,
3 => 3,
];
var_dump(array_filter(array_combine($array1, $array2), function($value, $key) {
return $key == "" || $value == "" ? false : $value;
}, ARRAY_FILTER_USE_BOTH ));
/*
Output:
array(2) {
["First"]=>
int(10)
["Fourth"]=>
int(3)
}
*/
Here's a fun way:
$result = array_flip(array_flip(array_filter(array_combine($_POST['names'],
$_POST['numbers']))));
// create array using $_POST['names'] as keys and $_POST['numbers'] as values
$result = array_combine($_POST['names'], $_POST['numbers']);
// remove entries that have empty values
$result = array_filter($result);
// remove entry with empty key
unset($result[null]);
print_r($result);
If both arrays will have the same count, and the keys will always be numeric, you could do the following:
$total = count($_POST['names']);
$final = array();
for ($i = 0; $i < $total; $i++) {
if (trim($_POST['names'][$i]) != '' && trim($_POST['numbers'][$i]) != '') {
$final[$_POST['names'][$i]] = $_POST['numbers'][$i];
}
}
Or if you prefer to use a foreach instead of for
$final = array();
foreach ($_POST['names'] as $key => $value) {
if (trim($value) != '' && trim($_POST['numbers'][$key]) != '') {
$final[$value] = $_POST['numbers'][$key];
}
}
Takeing your previous information into account:
both keys will be numeric and the count will be the same, since they come in pairs as names and numbers
$myNewArray = array();
$count = 0;
foreach ($_POST['names'] as $bufferArray)
{
if (($bufferArray[$count]!=NULL)&&($_POST['numbers][$count]!=NULL))
{
array_push($myNewArray, array($bufferArray[$count] => $_POST['numbers][$count]);
}
$count++;
}
Let me know if that helps! :)
Note: I made some edits to the code.
Also, my previous code checks if the empty array spaces are NULL. If you want to check if they are either NULL or "" (empty), then replace the line of code with:
if (($bufferArray[$count]!=NULL)&&($_POST['numbers][$count]!=NULL)&&($bufferArray[$count]!="")&&($_POST['numbers][$count]!=""))
{...}
i have array like this
<?php
$array =
array
(
array (
0 => 1,
1 => 'php',
2 => 11,
3 => 11,
4 => 11,
5 => 11,
6 => 11,
),
array (
0 => 1,
1 => 'php',
2 => 11,
3 => 11,
4 => 11,
5 => 11,
6 => ,
),
);
and i want to search in this multi-array to find if the key [6] => is empty.if it was empty in any array return false so how to do this
foreach($array as $item)
{
foreach($item as $key=>$value)
{
print($key);
if($key=="6" && $value==NULL)
{
echo "found";
return false;
}else{
echo "not found";
return true;
}
}
}
$empty = false;
foreach($array as $item)
{
if(empty($item[6]))
{
$empty=true;
break;
}
}
return $empty;
First, it's recommended to use only one return in a function, so define a boolean value, and turn it to TRUE when the condition satisfies.
Second, use break to stop the cycle(s) running (saves runtime)
http://php.net/manual/en/control-structures.break.php
Third, check keys and values appropriately. Your keys are numeric and you are comparing to a string "6". Use empty() if you are interested in that the value is an empty-ish value. Be aware that a numeric zero is an empty value. If you are specifically interested in NULL value, use === operator
http://php.net/manual/en/function.empty.php
http://php.net/manual/en/language.operators.comparison.php
+1 See Yoda-style conditions
http://en.wikipedia.org/wiki/Yoda_conditions
++1 Use K&R style indent, or don't use it. But do not try! ;)
http://en.wikipedia.org/wiki/Indent_style#K.26R_style
$found = false;
foreach ($array as $item) {
foreach ($item as $key => $value) {
print($key);
if (6 == $key && NULL === $value) { // or use 'empty($value)'
echo "found";
$found = true;
break 2;
} else {
echo "not found";
}
}
}
return !$found;
Here's an alternate for PHP >= 5.5.0 that checks for '', 0, null and false:
return !array_diff($six = array_column($array, 6), array_filter($six));
I want to check data in array to see if there are null values. If there are, I'd like to display an alert.
Example:
$data = array(1 => 'AKB48', 2 => '', 3 => 'JKT48');
The Array of index 1 ($data[1]) is null, and I want it to display "WARNING, data in array is null"
if data in array does not have empty/null values then don't show an alert:
$data = array(1 => 'AKB48', 2 => 'HKT48', 3 => 'JKT48');
(the above array will no trigger an alert)
How can I achieve this solution?
something like this?
$data = array(1 => 'AKB48', 2 => '', 3 => 'JKT48');
foreach($data as $val) {
if($val == '') {
echo "alert, array consist of empty value";
}
}
$data = array(1 => 'AKB48', 2 => '', 3 => 'JKT48');
foreach($data as $v)
{
if(empty($v))
{
echo "Array contains null value";
break;
}
}
Something like this?
isDefined will check if the value is a valid non-empty String.
function isDefined($var) {
return isset($var) && !is_null($var) && !empty($var);
}
$data = array(
array('AKB48', 'HKT48', NULL),
array('AKB49', '', 'JKT49'),
array('AKB50', 'HKT50', 'JKT50')
);
for ($i = 0; $i < count($data); $i++) {
foreach ($data[$i] as $col) {
if (!isDefined($col)) {
echo "<<<Attention: Array #$i contains an empty value!>>> ";
}
}
}
Running example of code above.