<?php
function empty_values()
{
$values = func_get_args();
while (list(, $value) = each($values))
{
if (empty($value))
{
return false;
}
}
return true;
}
?>
I want to pass the values from $_POST like this
if (empty_values(implode("," , $_POST)
{
//some code
}
Use array_filter() instead and compare the length to the length of the $_POST array.
if (count($_POST) > count(array_filter($_POST))) {
// At least one value was empty
}
Related
I have an array, say
$updates = array();
$updates['U1'] = array('F1', 'F2', 'F5');
$updates['U2'] = array('F3');
$updates['U3'] = array('F3', 'F4');
I need search for a value say F5 so it should return the key U1.
And also if there is multiple occurrence of a value, should return the last key.
Eg. searching F3 should return U3 and not U2.
I have searched a lot and can't find a way. I am looking for a solution without using loops.
without using loop:
function findArrVal($arr = [], $param){
static $indx = 0;
if($indx == 0){
krsort($arr);
}
$keys = array_keys($arr);
$values = array_values($arr);
if( count($values) == $indx ){
return false;
} else if( is_array($values[$indx]) && in_array($param, $values[$indx])){
return $keys[$indx];
} else {
++$indx;
return findArrVal($arr, $param);
}
return FALSE;
}
using loop:
function findArrVal($arr = [], $param){
krsort($arr);
foreach($arr as $key => $ar){
if(is_array($ar) && in_array($param, $ar)){
return $key;
}
}
return FALSE;
}
findArrVal($updates,'F3');
krsort - sorts the array in reverse order. ( to find the value at first occurrence )
is_array to check if the child value is an array type.
in_array to find the item on the child array.
Maybe It's helpful for you.
function _getFindArrayKey(array $arr, $key)
{
if (array_key_exists($key, $arr)) {
return true;
}
// check arrays contained in this array
foreach ($arr as $element) {
if (is_array($element)) {
if (_getFindArrayKey($element, $key)) {
return true;
}
}
}
return false;
}
i am trying to create function to check if array element is the last array element in one array. Original array looks like:
array(1001,
1002,
array('exam'=>true, 'index'=>10),
1003,
1004,
1005,
array('exam'=>true, 'index'=>20),
1006,
1007,
array('exam'=>true, 'index'=>30),
1008,
1009
)
I this case to prove if "array('exam'=>true, 'index'=>30)" is the last.
I have index position of that element, but I do not know how to check if that is the last array element.
function is_last_exam_in_survey($array, $exam_position) {
foreach($array as $element) {
if(!is_numeric($element) {
// check if that is the last array element in array
//return true;
} else {
// return false;
}
}
}
I would be grateful for any advice:)
function get_last_exam_in_survey($array) {
$last = null;
foreach($array as $element) {
if(is_array($element) && !empty($element['exam'])) {
$last = $element;
}
}
return $last;
}
function is_last_exam_in_survey($array, $exam_position) {
$last_exam = get_last_exam_in_survey($array);
return !empty($last_exam) && ($last_exam['index']==$exam_position);
}
I think this would be the quickest solution:
function is_last_exam_in_survey($array, $exam_position) {
$last_index = array_key_last( $array );
if( $exam_position == $last_index ){
return true;
}else{
return false;
}
}
You can still change the conditional statement if you are trying to compare values from the last element, for example:
if( isset($last_index['index']) && $exam_position == $last_index['index'] ){
Also, if you want to get the latest array value instead of key, you could use this:
$last_index = end( $array );
I would reverse the array, and look for the first element. Something like:
function is_last_exam_in_survey($array, $exam_position) {
foreach(array_reverse($array) as $element) {
if(!is_numeric($element) {
return $element['index'] === $exam_position;
}
}
return false;
}
Seems like the most efficient and simplest solution to me.
this solution avoid loop. at first we find out the last index of array.Then we use is_array() function for check the last element is array or not.
function get_last_exam_in_survey(array $arr)
{
$lastOfset = count($arr) - 1;
if(is_array($arr[$lastOfset])){
return true;
}else{
return false;
}
}
I think you can use array_column function to do that
function is_last_exam_in_survey($array,$exam_position){
$ac = array_column($array, 'index'); // return array(10,20,30)
$la = $ac[count($ac) - 1]; // 30
foreach ($array as $element) {
if (!is_numeric($element)) {
// check if $element['index'] === 30
if($element['index'] === $la){
return true;
}
}
}
}
How about using array_slice to extract the values in the array that are after the position you are looking at, then array_filter to remove any values that are not arrays. If there are any values left, then the entry you are looking at is not the last array entry in the array.
This may not be very efficient if you are calling it a lot of times with a large array. It may be better to rethink the way the data is stored or loaded into the array.
function is_last_exam_in_survey($array, $exam_position)
{
return isset($array[$exam_position])
&& is_array($array[$exam_position])
&& !array_filter(array_slice($array, $exam_position + 1), 'is_array');
}
when i try use in_array function in php for the second time for same array variable i got the following error saying:
in_array() expects parameter 2 to be array, string given in
when i wrap the function in condition is_array, it returns false, i already print the variable using print_r and its showing array structure, here's the code:
$chosenCour = array();
$chosenServ = array();
foreach ($preferences as $preference) {
if(!in_array($preference['courier'],$chosenCour)){
$chosenCour[] = $preference['courier'];
}
if(!in_array($preference['courier_service'],$chosenServ)){
$chosenServ[]= $preference['courier_service'];
}
}
foreach ($couriers as $courier) {
$courCond = false;
if(is_array($chosenCour)){
if(in_array($courier['courier_id'],$chosenCour)){
$courCond = true;
}
}
}
Check the additional condition for non empty array
foreach ($couriers as $courier) {
$courCond = false;
if(is_array($chosenCour) && count($chosenCour) > 0){
if(in_array($courier['courier_id'],$chosenCour)){
$courCond = true;
}
}
}
I'm creating a function that "cleans" data (escapes html to avoid xss) from the database before sending it to the views. All the data is passed on in 1 array. This arrays contains variables and arrays that contain other variables and arrays, and so on.
This is what I have now, it works, but it just doesn't look right. Is there any way to avoid going through a new foreach for every array inside an array?
public function clean_output(&$data)
{
if(!is_array($data))
{
$data = htmlspecialchars($data);
}
else
{
foreach($data as &$data_1)
{
if(!is_array($data_1))
{
$data_1 = htmlspecialchars($data_1);
}
else
{
foreach($data_1 as &$data_2)
{
if(!is_array($data_2))
{
$data_2 = htmlspecialchars($data_2);
}
else
{
foreach($data_2 as &$data_3)
{
$data_3 = htmlspecialchars($data_3);
}
}
}
}
}
}
}
Thanks to Antoine, I got a new function. Suggestions still welcome offcourse!
public function clean_output(&$data)
{
if(!is_array($data))
{
$data = htmlspecialchars($data);
}
else
{
foreach($data as &$data_1)
{
$this->clean_output($data_1);
}
}
}
You must use recursive function !
http://www.elated.com/articles/php-recursive-functions/
I have a form posting a multidimensional array to my PHP script, I need to know if all the values in the array are empty or not.
Here is my array:
$array[] = array('a'=>'',
'b'=>array('x'=>''),
'c'=>array('y'=>array('1'=>'')),
'd'=>'');
I tried using array_reduce(), but it's just returning an array:
echo array_reduce($array, "em");
function em($a,$b){
return $a.$b;
}
Any ideas?
I noticed this has been hanging around for a while, this is a custom function that works quite well.
function emptyArray($array) {
$empty = TRUE;
if (is_array($array)) {
foreach ($array as $value) {
if (!emptyArray($value)) {
$empty = FALSE;
}
}
}
elseif (!empty($array)) {
$empty = FALSE;
}
return $empty;
}
if all items in the array is empty then the function will return true, but if one item in the array is not empty then the function will return false.
Usage:
if (emptyArray($ARRAYNAME)) {
echo 'This array is empty';
}
else {
echo 'This array is not empty';
}