<?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);
Related
<?php
$tmRand = srand(floor(time() /60*30));
$x = array('"site.com/$user"','"site.com/$mail"');
$y = $x[array_rand($x)];
?>
<?php
for ($i = 0; $i <= 5; $i++) {
$user = "username";
$mail = "email";
$arr = array($y);
$url = $arr[array_rand($arr)];
echo "$url\n";
sleep(2);
}
$i++;
here I am trying a random url with the variable $user and $mail with time intervals per 30 minutes.
<?php
$tmRand = srand(floor(time() /60*30));
$x = array('"site.com/$user"','"site.com/$mail"');
$y = $x[array_rand($x)];
?>
output "site.com/$user" or "site.com/$mail"
from the results of the above output I try to randomly use a loop by calling the variable $y in array $arr = array($y); but the results that come out in the loop "site.com/$user" not "site.com/username"
<?php
for ($i = 0; $i <= 5; $i++) {
$user = "username";
$mail = "email";
$arr = array($y);
$url = $arr[array_rand($arr)];
echo "$url\n";
sleep(2);
}
$i++;
I'm breaking this into multiple steps here. You might be able to condense it:
$x = []
$userElement = 'site.com/'.$user;
$mailElement = 'site.com/'.$mail;
array_push($x, $userElement);
array_push($x, $mailElement);
var_dump($x);
How do I make sure that the password contains at least one character from each array? I could use do..while, right? What I don't quite understand is what I will need to put in the if statement inside do...while.
Is do...while the only way to ensure at least one character from each array is in the final password or are there other ways?
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$result = ""; //final random password
for($i = 0; $i < $length; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
echo $result;
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$resultArr[] = $a1[array_rand($a1)];
$resultArr[] = $a2[array_rand($a2)];
$resultArr[] = $a3[array_rand($a3)];
for($i = 0; $i < $length-3; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$resultArr[] = $values[$chosen][array_rand($values[$chosen])];
}
shuffle($resultArr);
$result = implode($resultArr); //final random password
echo $result;
You can surround the for wit ha do..while and in the condition check with preg_match() and a regex pattern that checks if at least one character is in the result:
<?php
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$result = ""; //final random password
do {
for($i = 0; $i < $length; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
} while(!(preg_match('/^(?=.*?[0-9])(?=.*?[a-z])(?=.*?[%*+?!]).{1,}/', $result)));
echo $result;
If the there's no coincidences with the pattern using preg_match, then will return false, in the do..while condition we check if one preg_match is false, if it is, do it again.
I made my answer general for any number of arrays.
I would use preg_match() for this, but for the question you asked, yes, you need to check each array separately:
$user_inputted_password = $_POST['user_inputted_password'];
$length = strlen($user_inputted_password); // user input password - you are capturing this somewhere, right
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$validate = array();
$result = ""; //final random password
function populate_validation_array($match_num, &$validate) {
if (!in_array($match_num, $validate)) {
$validate[] = $match_num;
}
}
$numeric_indexes = array(1, 2, 3);
$index_count = count($numeric_indexes);
for($i = 0; $i < $length; $i++){
for($j = 0; $j < $index_count; $j++) {
$numeric_index = $numeric_indexes[$j];
if (in_array($user_inputted_password[$i], ${"a" . $numeric_index})) {
populate_validation_array($numeric_index, $validate);
}
}
}
if (count($validate) === $index_count) {
$values = array();
for($i = 0; $i < $index_count; $i++) {
$values[] = ${"a" . ($i + 1)};
}
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
echo $result;
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
I'm searching for a function which can reverse a string in another way.
it should always takes the last and the first char of the string.
In example the string
123456
should become
615243
Is there any php function?
EDIT
This is my code so far
$mystring = "1234";
$start = 0;
$end = strlen($mystring);
$direction = 1;
$new_str = '';
while ($start === $end) {
if ($direction == 0) {
$new_str .= substr($mystring, $start, 1);
$start++;
$direction = 1;
} else {
$new_str .= substr($mystring, $end, -1);
$end--;
$direction = 0;
}
}
I couldn't help myself, I just had to write your code for you...
This just takes your string, splits it into an array, then builds up your output string taking letters from the front and end.
$output = '';
$input = str_split('123456');
$length = count($input);
while(strlen($output) < $length) {
$currLength = strlen($output);
if($currLength % 2 === 1) {
$output .= array_shift($input);
}
else {
$output .= array_pop($input);
}
}
echo $output;
Example: http://ideone.com/Xyd0z6
Not very different from Scopey's answer with a for loop:
$str = '123456';
$result = '';
$arr = str_split($str);
for ($i=0; $arr; $i++) {
$result .= $i % 2 ? array_shift($arr) : array_pop($arr);
}
echo $result;
This should work for you:
<?php
$str = "123456";
$rev = "";
$first = substr($str, 0, strlen($str)/2);
$last = strrev(substr($str, strlen($str)/2));
$max = strlen($first) > strlen($last) ? strlen($first): strlen($last);
for($count = 0; $count < $max; $count++)
$rev .= (isset($last[$count])?$last[$count]:"" ) . (isset($first[$count])?$first[$count]: "");
echo $rev;
?>
Output:
615243
i have this function:
function GetListByKeywords($keywords)
{
$HOST_DB = "localhost";
$NAME_DB = "jobs";
$USER_DB = "root";
$PWD_DB = "";
$connect = mysql_connect($HOST_DB, $USER_DB, $PWD_DB);
$db = mysql_select_db($NAME_DB);
$table = split('[;]', $keywords);
$Log_query = mysql_query("SELECT id FROM employment") or die(mysql_error());
$p = 0;
while ($Res_user = mysql_fetch_array($Log_query)) {
$id[$p] = $Res_user;
$p++;
}
$Log_query = mysql_query("SELECT description FROM employment") or die(mysql_error());
$p = 0;
while ($Res_user = mysql_fetch_array($Log_query)) {
$responsabilite[$p] = $Res_user;
$p++;
}
$p = 0;
for ($i = 0; $i < sizeof($table); $i++) {
for ($j = 0; $j < sizeof($responsabilite); $j++) {
if (strcmp($table[$i], $responsabilite[$j]) > 0) {
$marques[$p] = $id[$j];
$p++;
}
}
}
return $marques;
}
i want to compare the string $keywords to the fields of $responsabilite. so i convert it to the table $table and i compare each element by each element in the table responsabilite. so i need a function that can replace strcmp because i need to know if the element of $keywords exist in the string $responsabilite[$i] or not
Why don't you check if element in one array exist in other array with array_ intersect