I have one cycle while:
while ($userEquipments = mysql_fetch_array($getUserEquipments))
and in this cycle have one if with arrays:
if ($userEquipments['cloth_id'] == $clothes['id'] && $userEquipments['cloth_is_used'] == 1)
$isUsed = array('cloth_type' => $clothes['type_cloth'], 'cloth_name' => $clothes['name'], 'cloth_image' => $clothes['image']);
My question is how to return all information in this arrays?
You should declare your array outside your while, this way you can access it outside the loop.
Try this:
$isUsed[];
while ($userEquipments = mysql_fetch_array($getUserEquipments))
{
if ($userEquipments['cloth_id'] == $clothes['id'] && $userEquipments['cloth_is_used'] == 1)
{
$isUsed['cloth_type'] = $clothes['type_cloth'];
$isUsed['cloth_name'] = $clothes['name'];
...
break;
}
}
// Print the array
print_r($isUsed);
Related
is it any way to stop this repeated data.
if ($employees_csa[0]->csa_taken == 2 && $employees_csa[1]->csa_taken == 2 && $employees_csa[2]->csa_taken == 2 && $employees_csa[3]->csa_taken == 2 && $employees_csa[4]->csa_taken == 2 && $employees_csa[5]->csa_taken == 2 && $employees_csa[6]->csa_taken == 2 && $employees_csa[7]->csa_taken == 2) {
echo "data";
}
i tried for key range(0 , 8)
like this
foreach (range(0, count($employees_csa)) as $number) {
if ($employees_csa[$number]->csa_taken == 2) {
echo "data";
}
}
i tried that way not get any succes. i any another way to write easy condition.
You can loop arrays out of the box:
$all_taken = true;
foreach ($employees_csa as $employee) {
if ($employee->csa_taken != 2) {
$all_taken = false;
break;
}
}
if ($all_taken) {
echo 'data';
}
Another approach would be array_reduce() but this doesn't abort looping when there's already an answer:
$all_taken = array_reduce($employees_csa, function ($all_taken, $employee) {
if ($employee->csa_taken != 2) {
return false;
}
return $all_taken;
}, true);
if ($all_taken) {
echo 'data';
}
Alternatively, you could do it like this using array_column to pull out all the csa_taken properties, then reducing to 1 item if they are all the same with array_unique() and then checking that the same value is the expected number 2 with reset().
$csa_taken = array_column($employees_csa, 'csa_taken');
if (reset($csa_taken) === 2 && count(array_unique($csa_taken)) === 1) {
echo 'data';
}
Reusable function version: https://3v4l.org/4kYiE
A simple for-loop could work
$condition_met=true;
for($i=0;$i<8;++$i){
if( $employees_csa[$i]->csa_taken != 2){
$condition_met=false;
break;
}
}
if($condition_met===true){
//success
}
else{
//fail
}
A simple method could be done like
foreach($employees_csa as $singleEmployee){
if($singleEmployee->csa_taken == 2){
echo "data";
}
}
I'm just wondering if there's a way to simplify this code?
foreach ($parent_data as $ind_port_record) {
if ( isset($ind_port_record['port_name']) && (strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2') ){
$record_to_include['remote_id'] = $ind_port_record['remote_id'];
$record_to_include['remote_name'] = $ind_port_record['remote_name'];
$record_to_include['remote_object_id'] = $ind_port_record['remote_object_id'];
$record_to_include['remote_object_name'] = $ind_port_record['remote_object_name'];
break;
}
}
//make sure you have something in remote object details
if ( ! isset($record_to_include['remote_id']) ){
$record_to_include['remote_id'] = '';
$record_to_include['remote_name'] = '';
$record_to_include['remote_object_id'] = '';
$record_to_include['remote_object_name'] = '';
}
I just need to make sure the values inside the $record _to_include are not uninitialized or NULL.
Thanks.
First off, simplify the if()
You currently have a lot of conditions
(strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2')
Let's make that check an array
in_array( strtoupper($ind_port_record['port_name'], array('GI/2','G2','GI2')) )
Now to check if $record_to_include are not uninitialized or NULL
Let's loop through the array and do a simple check.
foreach($record_to_include as $record => $value) {
$record_to_include[$record] = is_null($value) OR !isset($record_to_include[$record]) ? '' : $value;
}
$record = array_filter($parentData, function (array $record) {
return isset($record['port_name']) && preg_match('!^(GI/2|G2|GI2)$!i', $record['port_name']);
});
$recordToInclude = $record ? current($record) : array(
'remote_id' => null,
'remote_name' => null,
'remote_object_id' => null,
'remote_object_name' => null
);
$gi_constraint = array('GI/2', 'G2', 'GI2'); // you are checking one and the same variable for different values, so you can use in_array here
foreach ($parent_data as $ind_port_record) {
if (isset($ind_port_record['port_name']) && in_array(strtoupper($ind_port_record['port_name']), $gi_constraint)){
foreach ($ind_port_record as $k=>$v) {
$record_to_include[$k] = $v; // as they have the same keys, you can specify the key and assign to the value of $in_port_record
}
break;
}
}
//make sure you have something in remote object details
if (!isset($record_to_include['remote_id']) ){
foreach ($record_to_include as $k => &$v) {
$v = ''; // when using reference, it will change the original array
}
}
Explanations in the code
Try
$arr = array('remote_id','remote_name','remote_object_id','remote_object_name');
if ( isset($ind_port_record['port_name']) && (strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2') ){
foreach($arr as $ar){
$record_to_include[$ar] = (isset($ind_port_record[$ar]) && isset($record_to_include['remote_id']))?$ind_port_record[$ar]:NULL;
}
}
I'm currently writing up a function in order to validate a URL by exploding it into different parts and matching those parts with strings I've defined. This is the function I'm using so far:
function validTnet($tnet_url) {
$tnet_2 = "defined2";
$tnet_3 = "defined3";
$tnet_5 = "defined5";
$tnet_7 = "";
if($exp_url[2] == $tnet_2) {
#show true, proceed to next validation
if($exp_url[3] == $tnet_3) {
#true, and next
if($exp_url[5] == $tnet_5) {
#true, and last
if($exp_url[7] == $tnet_7) {
#true, valid
}
}
}
} else {
echo "failed on tnet_2";
}
}
For some reason I'm unable to think of the way to code (or search for the proper term) of how to break out of the if statements that are nested.
What I would like to do check each part of the URL, starting with $tnet_2, and if it fails one of the checks ($tnet_2, $tnet_3, $tnet_5 or $tnet_7), output that it fails, and break out of the if statement. Is there an easy way to accomplish this using some of the code I have already?
Combine all the if conditions
if(
$exp_url[2] == $tnet_2 &&
$exp_url[3] == $tnet_3 &&
$exp_url[5] == $tnet_5 &&
$exp_url[7] == $tnet_7
) {
//true, valid
} else {
echo "failed on tnet_2";
}
$is_valid = true;
foreach (array(2, 3, 5, 7) as $i) {
if ($exp_url[$i] !== ${'tnet_'.$i}) {
$is_valid = false;
break;
}
}
You could do $tnet[$i] if you define those values in an array:
$tnet = array(
2 => "defined2",
3 => "defined3",
5 => "defined5",
7 => ""
);
Basically I have this code scenario:
if($_SESSION['player_1_pawn'][0]['currentHealth'] <=0 &&
$_SESSION['player_1_pawn'][1]['currentHealth'] <=0 &&
$_SESSION['player_1_pawn'][2]['currentHealth'] <=0 &&
$_SESSION['player_1_pawn'][3]['currentHealth'] <=0 &&
$_SESSION['player_1_pawn'][4]['currentHealth'] <=0) {
//some code here
}
Is there any way to check or to loop through all of the indexes if all of ['player_1_pawn'][index]['currentHealth'] is smaller than 0, instead of writing it one by one like I posted?
Just write a foreach construct that loops through all of the array elements you need to check:
$flag = true; // after the foreach, flag will be true if all pawns have <= 0 health
foreach ($_SESSION['player_1_pawn'] as $value)
{
// for each pawn, check the current health
if ($value['currentHealth'] > 0)
{
$flag = false; // one pawn has a positive current health
break; // no need to check the rest, according to your code sample!
}
}
if ($flag === true) // all pawns have 0 or negative health - run code!
{
// some code here
}
One more solution is to use array_reduce() to check the condition:
if (array_reduce($_SESSION['player_1_pawn'], function (&$flag, $player) {
$flag &= ($player['currentHealth'] <=0);
return $flag;
}, true));
P.S. Be careful when array $_SESSION['player_1_pawn'] is empty.
So this all stemmed from not wanting to check each POST in a huge If statement with tons of ANDs like so:
if(isset($_POST[$question], $_POST[$choice1], $_POST[$choice2], $_POST[$choice3], $_POST[$choice4], $_POST[$choice5], $_POST[$password]) &&
strlen(question > 0) &&
strlen(choice1 > 0) &&
strlen(choice2 > 0) &&
strlen(choice3 > 0) &&
strlen(choice4 > 0) &&
strlen(choice5 > 0) &&
strlen(password > 0))
{
$cat=$_POST['cat'];
$question=$_POST['question'];
$choice1=$_POST['choice1'];
$choice2=$_POST['choice2'];
$choice3=$_POST['choice3'];
$choice4=$_POST['choice4'];
$choice5=$_POST['choice5'];
$password=$_POST['password'];
}
else{
//Incorrect password or missing data
}
Instead I created an array with a string for each variable I wanted to check for and a foreach loop to run my tests
//create array with all wanted variables
$variables = array('cat', 'question', 'choice1', 'choice2', 'choice3', 'choice4', 'choice5', 'password');
$returnError = FALSE;
//run through array to check that all have been posted and meet requirements
foreach($variables as $var) {
if(!isset($_POST[$var]) || !strlen($_POST[$var]) > 0) {
$returnError = TRUE;
break;
}
}
Now I want to take the string names and instantiate variables with the same name
//create array with all wanted variables
$variables = array('cat', 'question', 'choice1', 'choice2', 'choice3', 'choice4', 'choice5', 'password');
$returnError = FALSE;
//run through array to check that all have been posted and meet requirements
foreach($variables as $var) {
if(!isset($_POST[$var]) || !strlen($_POST[$var]) > 0) {
$returnError = TRUE;
break;
}
else{
global $*/variable named $var*/ = $_POST[$var];
}
}
Is that possible? If not, how would I do something similar with an array of variables like so:
$variables = array($cat,$question,$choice1,$choice2,$choice3,$choice4,$choice5,$password);
You can use Variable variables, although it is not recommended.
global $$var = $_POST[$var];
Use eval();
http://www.php.net/eval
Be aware that creating variable with eval() from post data could be a security risk.