How can i loop through a post and find if any of the answers are "no"?
Here is my code so far:
if($_POST["minRequirementsForm"]) {
foreach($_POST as $key => $value) {
$_SESSION['minrequirements'][$key] = $value;
}
}
Thanks in advance.
You could use in_array() or array_search() and save a loop:
if (in_array('no', $_POST)) echo 'they said no'.
if (($key = array_search('no', $_POST)) !== false) echo "$key was answered with no";
if($_POST["minRequirementsForm"]) {
foreach($_POST as $key => $value) {
if ('no' == $value) {
// do something
}
$_SESSION['minrequirements'][$key] = $value;
}
}
$NoAsnwers = false;
if($_POST["minRequirementsForm"]) {
foreach($_POST as $key => $value) {
if ($value == 'No') $NoAsnwers = true;
}
}
Maybe so?
Related
I am very new to the PHP, i want to check the value is present inside the associative array,please help me to acheive this thing.
public $array=[
'id','name',
'value.details'=>['type'=>'myFunction']
];
foreach($array as $key=>$condition){
if(in_array('myFunction',$array)){
//My logic
}
}
if you know the keys:
if($array['value.details']['type'] === 'myFunction') { }
if you walk through your array:
foreach($array as $key=>$val) {
if(is_array($val) && in_array('myFunction', $val)) {
//
}
}
Your array is mix of string and array, you might also wanna check the array inside your array.
$array=[
'id','name',
'value.details'=>['type'=>'myFunction']
];
$query = 'myFunction';
$isPresent = false;
foreach ($array as $data) {
if (gettype($data) === "array") {
foreach ($data as $val) {
if ($val === $query) {
$isPresent = true;
continue;
}
}
} else {
if ($query === $data) {
$isPresent = true;
continue;
}
}
}
array(
'World'=>array(
'Asia'=>array(
'Japan'=>array(
'City'=>'Tokyo'
)
)
)
);
In my array I am searching for a key:
foreach ($array as $key => $item) {
if(is_array($item)){
if (stripos($key, "Japan") !== false){
echo $key;
}
}
}
The result is Japan.
For each key I want to check if the child key is "City". So I did the following:
foreach ($array as $key => $item) {
if(is_array($item)){
if (stripos($key, "Japan") !== false){
echo $key;
foreach ($key as $k => $i) {
if (stripos($k, "City") !== false){
echo "true";
} else {
echo "false";
}
}
}
}
I expect the result Japan true or at least the result Japan false but the result is still only Japan I do not understand.
In your second foreach your using a single element ($key) while you should use a set of elements ($array[$key], considering it's a multidimensional array).
foreach ($array as $key => $item) {
if(is_array($item)){
if (stripos($key, "Japan") !== false){
echo $key;
foreach ($array[$key] as $k => $i) {
if (stripos($k, "City") !== false){
echo "true";
} else {
echo "false";
}
}
}
}
I'd go with a recursion algorithm to solve the problem:
function find_array_children_key($array, $children_key, $parent_key=''){
$returning_value = false;
if(is_array($array))
{
foreach($array as $key=>$value)
{
if($key===$children_key)
$returning_value = $parent_key;
else
$returning_value = find_array_children_key($array,$children_key,$key);
if($returning_value!==false)
break;
}
}
return $returning_value;
}
Which you'd call, for instance in your case, find_array_children_key($array,'City')
i'have an array, and then i have a script who gets the category i'm browsing (using wordpress) and put it in the $category variable.
So i test if the category i'm browsing it's equal to the $array key and then i paste some text
$array = array ('key' => 'value', ... )
//...
// a script who gets the category i'm browsing and store it in the $category variable
//...
/* starting the foreach loop */
foreach( $array as $key => $value) {
if ($category == $key) {
echo "some $value here";
} elseif ($category !== $key) {
echo "nothing";
}
The problem is that this loop does echo "nothing" for each time the $category is not equal to the $key for each element of the array.
So if i have 20 key => value in the array this loop paste one time "some $value here" and 19 times "nothing"
there is a way to echo "nothing" only one time?
Thank you!
You can use array_key_exists instead of the foreach loop:
if (array_key_exists($category, $array)) {
echo $array[$category];
} else {
echo 'nothing';
}
$i=0;
foreach( $array as $key => $value) {
$i++;
if ($category == $key) {
echo "some $value here";
} elseif($category !== $key)
{
if($i<=1)
{
echo "nothing";
}
else{}
}
Try with -
$i = 1;
foreach( $array as $key => $value) {
if ($category == $key) {
echo "some $value here";
} elseif ($category !== $key) {
$i++;
}
}
if (count($array) == $i) {
echo "nothing";
}
How to return $value after loop with its returned data ? I think to create array before loop and equal it to $v to use it after loop but it didn't work.
Any idea on how to solve this problem ?
// create array
$v = array();
// start loop
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
// return $value with data
return $v = $value ;
}
echo $v->country_name
try this:
$v = array();
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
{
if(!in_array($value,$v))
{
array_push($v,$value);
}
}
}
try this
$v = array();
$i=0;
// start loop
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
// return $value with data
$i++;
$v[$i] = $value ;
}
//print $v
print_r($v)
If like using 'return' try this.
$v = iLikeUsingReturn($this,$data);
function iLikeUsingReturn($t,$d){
foreach ($t->json_data->locations as $key => $value) {
if ($value->country_name == $d['city']->country_name)
return $value ;
}
return array();
}
I think the following code will helps you.
// create array
$v = array();
// start loop
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
// return $value with data
array_push($v, $value);
}
return $v;
I have several $_POST variables, they are
$_POST['item_number1']
$_POST['item_number2']
and so on
I need to write a loop tha displays the values of all the variables (I don't know how many there are). What would be a simplest way to go about it? Also what would be the simplest way if I do know how many variables I have?
This will echo all POST parameters whose names start with item_number:
foreach($_POST as $k => $v) {
if(strpos($k, 'item_number') === 0) {
echo "$k = $v";
}
}
PHP Manual: foreach(), strpos()
If you know how many do you have:
for ($i=0; $i < $num_of_vars; $i++)
echo $_POST['item_number'.$i]."<br />";
UPDATE:
If not:
foreach($_POST as $k => $v) {
$pos = strpos($k, "item_number");
if($pos === 0)
echo $v."<br />";
}
Gets all POST variables that are like "item_number"
UPD 2: Changed "==" to "===" because of piotrekkr's comment. Thanks
try:
foreach($_POST as $k => $v)
{
if(strpos($k, 'item_number') === 0)
{
echo "$k = $v";
}
}
In the above example, $k will be the array key and $v would be the value.
if you know the number of variables:
<?php
$n = 25; // the max number of variables
$name = 'item_number'; // the name of variables
for ($i = 1; $i <= $n; $i++) {
if (isset($_POST[$name . $i])) {
echo $_POST[$name . $i];
}
}
if you don't know the number:
<?php
$name = 'item_number';
foreach ($_POST as $key) {
if (strpos($key, $name) > 0) {
echo $_POST[$key];
}
}
If you must stick with those variable names like item_numberX
foreach (array_intersect_key($_POST, preg_grep('#^item_number\d+$#D', array_keys($_POST))) as $k => $v) {
echo "$k $v \n";
}
or
foreach (new RegexIterator(new ArrayIterator($_POST), '#^a\d+$#D', null, RegexIterator::USE_KEY) as $k => $v) {
echo "$k $v \n";
}
Better to use php's input variable array feature, if you can control the input names.
<input name="item_number[]">
<input name="item_number[]">
<input name="item_number[]">
then php processes it into an array for you.
print_r($_POST['item_number']);
foreach($_POST as $k => $v) {
if(preg_match("#item_number([0-9]+)#si", $k, $keyMatch)) {
$number = $keyMatch[1];
// ...
}
}
try:
while (list($key,$value) = each($_POST))
${$key} = trim($value);