Removing empty variable from array - php

I want to remove empty variable from array. I have explored different methods but couldn't be able to do this correctly. I have tried in the following ways:
$field1 = 6;
$field2 = 15;
$field3 = 7;
$demo = array($field1=>"info1", $field2=>"info2",$field3=>"field3");
echo "$demo[$field3]";
If any of the variables ($field1, $field2 or field3) is zero the result should be Null.

try this
$field1 = 6;
$field2 = 15;
$field3 = 7;
$demo = array($field1=>"info1", $field2=>"info2",$field3=>"field3");
$demo = isset($demo[0]) ? null : $demo;
echo "$demo[$field3]";

<?php
foreach($array as $key => $value){
if($key == false || $key == 0){
unset($array[$key]);
}
}
?>
Do you mean that?
With that, echo "$demo[$field3]"; will output NULL if $field3 == 0.

please try this way;
for($i = 0; $i<count($demo); $i++){
if($demo[$i) == 0)$demo[$i]=NULL
}

In this code if the key of array is Null or 0. Item Delete from array.
<?php
foreach($array as $key => $value){
if($key == false || $key == 0){
unset($array[$key]);
}
}
?>

You should try this :
$field1 = 6;
$field2 = 15;
$field3 = 7;
$demo = array($field1=>"info1", $field2=>"info2",$field3=>"field3");
foreach($demo as $key=>$val){
//echo $key."----------".$val; just to check
if($key == '0'){
$val='Null';
}
}

Related

php how to find index of element in array value of which is less than number? [duplicate]

This question already has answers here:
How to find first array element with value greater than X in PHP?
(2 answers)
Closed 1 year ago.
Say, there is an array and a number:
$values = ['a'=>10, 'b'=>20, 'c'=>30];
$num = 25;
How can I find an index of the array element the value of which is less than the number?
In the above example it will be the 'b' index of which is 1.
You could sort the array by the values then loop through it until a number is hit above the $num. Here is a quick example:
$values = ['a'=>10, 'b'=>20, 'c'=>30];
$num = 25;
$sortedValues = $values;
asort($sortedValues);
while (($current = current($sortedValues)) !== false && $current < $num) {
$lastValue = key($sortedValues);
next($sortedValues);
}
echo $lastValue;
I got it:
$values = ['a'=>1, 'b'=>20, 'c'=>30, 'd'=>40, 'e'=>50];
$num = 55;
$i = null;
foreach ($values as $k => $v){
if ($v == $num){
echo $v;
break;
}
elseif ($num >= 1 && $v > $num){
echo $values[$i];
break;
}
elseif ($num > end($values)){
echo end($values);
break;
}
$i = $k;
}
May be array_filter can be some more handy:
$values = ['a'=>1, 'b'=>20, 'c'=>30, 'd'=>40, 'e'=>50];
$num = 45;
// filter items less than $num
$lesser = array_filter($values, function($v) use($num) {
return $v < $num;
});
// get necessary item/index from filtered, last for example
$lesserKeys = array_keys($lesser);
$lastOfLessKey = end($lesserKeys);
$lastOfLess = end($lesser);
var_dump($lastOfLessKey, $lastOfLess); // d 40
$values = ['a'=>10, 'b'=>20, 'c'=>30];
$num = 25;
foreach($value as $key => $val){
if( (int) $val == (int) $num){
echo 'this is what you are searching. key is: '.$key.', value is: '.$val;
}
}

How to insert array if pass is value

<?php
$test1 ="pass";
$test2 = "fail";
$test3 = "pass";
$check = array();
for($i=0;$i<3;$i++){
if($test1== "pass"){
$num = 1;
}
if($test2 == "pass"){
$num = 2;
}
if($test3 == "pass"){
$num = 3;
}
echo $num;
$check[$i] = $num;
}
?>
value: test1="pass",test2="fail",test3="pass"
I want: $check(1,3)
Take only the "pass"
I want to put a value equal to "pass" in the array. The condition is that the value must be equal to "pass".
but test1,test2,test3 not array.
Presumably you are looking for this:
$test1 = "pass";
$test2 = "fail";
$test3 = "pass";
$check = array();
for($i = 1; $i <=3; $i++) {
$t = 'test'.$i;
if(${$t} == 'pass')
$check[] = $i;
}
print_r($check);
Gives you:
Array
(
[0] => 1
[1] => 3
)
It's somewhat difficult to tell, but if so, this uses Variable Variables, cutting down on some script when using sequential variables.
$test1 ="pass";
$test2 = "fail";
$test3 = "pass";
$num =array();
for($i=0;$i<3;$i++){
if($test1 == "pass"){
$num[] = 1;
}
if($test2 == "pass"){
$num[] = 2;
}
if($test3 == "pass"){
$num[] = 3;
}
}
print_r(array_unique($num));
You can do this like :
$test1 ="pass";
$test2 = "fail";
$test3 = "pass";
$check = array();
$num = '';
if($test1== "pass"){
$num .= '1,';
}
if($test2 == "pass"){
$num .= '2,';
}
if($test3 == "pass"){
$num .= '3,';
}
$num = trim($num,',');
$check = explode(',',$num);

Loop Two Array in Single Loop

I want to make a form input with dynamic Name Label and Value,
there are two arrays, how to make it loop in single foreach ?
This is an example:
<?php
$value = explode(',',$row['value']);
$name = explode(',',$row['name']);
for($x = 0; $x <= 10; $x++) {
echo $name;
echo $value;
}
?>
$row Variable is an Array,
This method didn't work for me. Any suggestions?
Why cant you use array_combine before loop, try this..
<?php
$value = explode(',',$row['value']);
$name = explode(',',$row['name']);
$combainedArray = array_combine ( $name , $value );
foreach($combainedArray as $name => $value ) {
echo $name, '=>', $value;
}
?>
OR
if you don't what to combine make it like this..
<?php
$value = explode(',',$row['value']);
$name = explode(',',$row['name']);
$count = count($value);
for($x = 0; $x < $count; $x++) {
echo $name[$x];
echo $value[$x];
}
?>
As a solution to above mentioned problem,Please try executing below mentioned code snippet.
In consideration with same no of form fields for name and value
<?php
$value = explode(',',$row['value']);
$name = explode(',',$row['name']);
for($x = 0; $x <= 10; $x++) {
echo $value[$x];
echo $name[$x];
}
?>

Count array values of an associative array not working

I am trying to count the values of an associative array, but it isnt doing what I want it to.
I am pulling data from a database, as I need all the answers to a question. The answers in the database are under the fields answer1, answer2, answer3 etc, up to answer20.
What I am trying to do is count the number of fields that contain data, and ignore the fields that dont have any data.
Here is my code so far:
<?php
$results = $db->get_results("SELECT * FROM wellness_hra_questions WHERE category='general' AND level='1' AND gender='All' AND active='yes' ORDER BY id ASC");
foreach($results as $result){
$id = $result->id;
}
$answers = $db->get_results("SELECT answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, answer11, answer12, answer13, answer14, answer15, answer16, answer17 FROM wellness_hra_questions WHERE id=".$id."");
//$db->debug();
for( $i=1; $i <= 17; $i++) {
foreach($answers as $a_result){
$answer = 'answer';
$answer_id = $answer.$i;
$answer_id2 = $a_result->$answer_id;
$answer1 = $a_result->answer1;
$array1 = array('1'=>''.$answer1.'');
$answer2 = $a_result->answer2;
$array2 = array('2'=>''.$answer2.'');
$answer3 = $a_result->answer3;
$array3 = array('3'=>''.$answer3.'');
$answer4 = $a_result->answer4;
$array4 = array('4'=>''.$answer4.'');
$answer5 = $a_result->answer1;
$array5 = array('5'=>''.$answer5.'');
$answer6 = $a_result->answer6;
$array6 = array('1'=>''.$answer6.'');
}
if($answer1 == TRUE){
$newvalue1 = $array1;
}
if($answer2 == TRUE){
$newvalue2 = $array2;
}
if($answer3 == TRUE){
$newvalue3 = $array3;
}
if($answer4 == TRUE){
$newvalue4 = $array4;
}
if($answer5 == TRUE){
$newvalue1 = $array5;
}
if($answer1 == TRUE){
$newvalue6 = $array6;
}
$newarray = array_merge($newvalue1,$newvalue2, $newvalue3, $newvalue4, $newvalue5);
$count = count($newarray, COUNT_RECURSIVE);
echo $count;
}
?>
You need a code cleanup here - if what you want is a count, and the array_merge is ONLY for this purpose, this should do what I believe you would like to do:
for( $i=1; $i <= 17; $i++) {
foreach($answers as $a_result){
$answer_id = 'answer'.$i;
$answer_id2 = $a_result->$answer_id;
$thisCount = 0;
$newarray = array();
for($j = 1; $j < 7; $j++) {
$var = "answer" . $j;
$$var = ${"a_result->answer" . $j};
if (strlen($$var) > 0) {
$arrayID = "array" . $j;
$$arrayID = array((string)$j => $$var);// I don't think you need this but if you do included array_merge here
array_merge($newarray, $$arrayID);
$thisCount++;
}
}
echo $count;
print_r($newarray); // show the contents of the merged arrays
}// end foreach
}//end for

Simple PHP Question: Using an IF statement inside a FOR loop

Inside a for loop, I'm trying to set a variable based on the what iteration of the loop it's on:
<?php
for ($k = 0; $k < 3; $k++){
if ($k = 0) : $var = 'zero';
elseif ($k = 1) : $var = 'one';
else : $var = 'two';
endif;
?>
This is iteration <?php echo $var; ?>.
<?php }; ?>
But it continues looping forever until my browser freezes... what's going on? Any help would be greatly appreciated!
You are essentially setting $k to be 0 and 1. Comparing values use '=='.
Try this instead.
<?php
for($k = 0; $k < 3; $k++){
if ($k == 0)
$var = 'zero';
elseif ($k == 1)
$var = 'one';
else
$var = 'two';
?>
This is iteration <?php echo $var; ?>.
<?php } ?>
if ($k = 0)
You're setting $k to 0 here. Use == to compare values, or === to compare values and their types.
In the if statements, you are using the = operator which assigns...
then $k will always be 0 and the loop will never end.
Replace = to == in the if statements. So it will compare instead of assign $k a value.
A clearer example.-
if ($k = 1) // It will return 1, because you are assigning $k, 1.
But in
if ($k == 1) // It will return a boolean **true** if $k equals 1, **false** otherwise.
for ($k = 0; $k < 3; $k++){
if($k == 0){
$var = 'zero';
}elseif($k == 1){
$var = 'one';
}else{
$var = 'two';
}
}
echo $var;

Categories