Good day.
Me need check 42 names, but it very long:
Useally i use next code:
$info_1 = (isset($info['1'])) ? $info['1'] : 0;
$info_2 = (isset($info['2'])) ? $info['2'] : 0;
$info_42 = (isset($info['42'])) ? $info['42'] : 0;
Can i get dinamic name?
ex. i want use code:
for($i=0;$i<43;$i++){
$info_$i = (isset($info[$i])) ? $info[$i] : 0;
}
How aright add $i for $info?
And is it possible?
for($i=0;$i<43;$i++){
$name = 'info_'.$i;
$$name = (isset($info[$i])) ? $info[$i] : 0;
}
Better use an array:
for($i=0;$i<43;$i++){
$info[$i] = (isset($info[$i])) ? $info[$i] : 0;
}
Just another option:
for($i=0;$i<43;$i++){
${"info_$i"} = (isset(${"info_$i"}) ? ${"info_$i"} : 0;
}
You can do it like this
for($i = 0; $i<43; $i++) {
$var_name = "info_{$i}";
$$var_name = isset($info[$i])) ? $info[$i] : 0;
}
But if you really need this kind of stuff you're doing it real wrong and better off converting this long list of variables to an array;
Related
I would be much grateful if you could help in solving this PHP issue.
I needs to verify the value of $fl_blackriver, $fl_flacq, etc...
if value=="", assign 0 to this value
update the corresponding variable with the updated value
$fl_blackriver = $_REQUEST['fl_blackriver'];
$fl_flacq = $_REQUEST['fl_flacq'];
$fl_grandport = $_REQUEST['fl_grandport'];
$fl_moka = $_REQUEST['fl_moka'];
$fl_pamplemousses = $_REQUEST['fl_pamplemousses'];
$fl_plaineswilhems = $_REQUEST['fl_plaineswilhems'];
$fl_portlouis = $_REQUEST['fl_portlouis'];
$fl_rivieredurempart = $_REQUEST['fl_rivieredurempart'];
$fl_savanne = $_REQUEST['fl_savanne'];
$fl_finalize = $_REQUEST['fl_finalize'];
$district = array($fl_blackriver,$fl_flacq,$fl_grandport,$fl_moka,$fl_pamplemousses,$fl_plaineswilhems,$fl_portlouis,$fl_rivieredurempart,$fl_savanne,$fl_finalize);
$arrlength = count($district);
for($x = 0; $x < $arrlength; $x++)
{
if ($district[$x]=="")
$district[$x] = "0";
}
This doesn't look nice but it should do the trick:
$fl_blackriver = $_REQUEST['fl_blackriver'];
$fl_flacq = $_REQUEST['fl_flacq'];
$fl_grandport = $_REQUEST['fl_grandport'];
$fl_moka = $_REQUEST['fl_moka'];
$fl_pamplemousses = $_REQUEST['fl_pamplemousses'];
$fl_plaineswilhems = $_REQUEST['fl_plaineswilhems'];
$fl_portlouis = $_REQUEST['fl_portlouis'];
$fl_rivieredurempart = $_REQUEST['fl_rivieredurempart'];
$fl_savanne = $_REQUEST['fl_savanne'];
$fl_finalize = $_REQUEST['fl_finalize'];
$district = array(
'fl_blackriver' => $fl_blackriver,
'fl_flacq' => $fl_flacq,
'fl_grandport' => $fl_grandport,
'fl_moka' => $fl_moka,
'fl_pamplemousses' => $fl_pamplemousses,
'fl_plaineswilhems' => $fl_plaineswilhems,
'fl_portlouis' => $fl_portlouis,
'fl_rivieredurempart' => $fl_rivieredurempart,
'fl_savanne' => $fl_savanne,
'fl_finalize' => $fl_finalize
);
foreach ($district as $key => $value) {
if ($value === "") {
$$key = "0";
}
}
You can do that using ternary operator like this,
$fl_blackriver = !empty($_REQUEST['fl_blackriver']) ? $_REQUEST['fl_blackriver'] : 0 ;
$fl_flacq = !empty($_REQUEST['fl_flacq']) ? $_REQUEST['fl_flacq'] : 0 ;
$fl_grandport = !empty($_REQUEST['fl_grandport']) ? $_REQUEST['fl_grandport'] : 0 ;
$fl_moka = !empty($_REQUEST['fl_moka']) ? $_REQUEST['fl_moka'] : 0 ;
$fl_pamplemousses = !empty($_REQUEST['fl_pamplemousses']) ? $_REQUEST['fl_pamplemousses'] : 0 ;
$fl_plaineswilhems = !empty($_REQUEST['fl_plaineswilhems']) ? $_REQUEST['fl_plaineswilhems'] : 0 ;
$fl_portlouis = !empty($_REQUEST['fl_portlouis']) ? $_REQUEST['fl_portlouis'] : 0 ;
$fl_rivieredurempart = !empty($_REQUEST['fl_rivieredurempart']) ? $_REQUEST['fl_rivieredurempart'] : 0 ;
$fl_savanne = !empty($_REQUEST['fl_savanne']) ? $_REQUEST['fl_savanne'] : 0 ;
$fl_finalize = !empty($_REQUEST['fl_finalize']) ? $_REQUEST['fl_finalize'] : 0 ;
You can try the following process. It will check the value whether it is empty or not. If the value is empty then it will set the value to 0. Otherwise it will take the requested value.
$fl_blackriver = (isset($_REQUEST['fl_blackriver'])) ? $_REQUEST['fl_blackriver'] : 0;
$fl_flacq = (isset($_REQUEST['fl_flacq'])) ? $_REQUEST['fl_flacq'] : 0;
$fl_grandport = (isset($_REQUEST['fl_grandport'])) ? $_REQUEST['fl_grandport'] : 0;
$fl_moka = (isset($_REQUEST['fl_moka'])) ? $_REQUEST['fl_moka'] : 0;
$fl_pamplemousses = (isset($_REQUEST['fl_pamplemousses'])) ? $_REQUEST['fl_pamplemousses'] : 0;
$fl_plaineswilhems = (isset($_REQUEST['fl_plaineswilhems'])) ? $_REQUEST['fl_plaineswilhems'] : 0;
$fl_portlouis = (isset($_REQUEST['fl_portlouis'])) ? $_REQUEST['fl_portlouis'] : 0;
$fl_rivieredurempart = (isset($_REQUEST['fl_rivieredurempart'])) ? $_REQUEST['fl_rivieredurempart'] : 0;
$fl_savanne = (isset($_REQUEST['fl_savanne'])) ? $_REQUEST['fl_savanne'] : 0;
$fl_finalize = (isset($_REQUEST['fl_finalize'])) ? $_REQUEST['fl_finalize'] : 0;
$district = array($fl_blackriver,$fl_flacq,$fl_grandport,$fl_moka,$fl_pamplemousses,$fl_plaineswilhems,$fl_portlouis,$fl_rivieredurempart,$fl_savanne,$fl_finalize);
foreach($_REQUEST as $key => $value) {
if (! is_numeric($value)) {
$_REQUEST[$key] = 0;
}}
extract($_REQUEST);
I would do that in a variadic function returning an array when multiple arguments are given or source of any type when only one argument is present.
function request(string ...$names)
{
foreach ($names as &$v)
$v = '' === ($v = $_REQUEST[$v] ?? 0) ? 0 : $v;
return 1 === count($names) ? $names[0] : $names;
}
[$fl_blackriver, $fl_flacq] = request('fl_blackriver', 'fl_flacq');
$fl_grandport = request('fl_grandport');
$district = request('fl_blackriver','fl_flacq','fl_grandport','fl_moka','fl_pamplemousses','fl_plaineswilhems','fl_portlouis','fl_rivieredurempart','fl_savanne','fl_finalize');
var_dump($fl_blackriver, $fl_flacq, $fl_grandport);
var_dump($district);
To get key/value pairs in an associative array:
function request_assoc(string ...$names) : array
{
$names = array_flip($names);
foreach ($names as $k => &$v)
$v = '' === ($v = $_REQUEST[$k] ?? 0) ? 0 : $v;
return $names;
}
I have problem with my variable, I wanna make variable like these
$H1,H2, until $h48
but I know is kinda difficult, so I wanna change to like these
$H[1],$H[2],$H[3] until $H[48]
these I try with my code:
$loopCount = 1;
$H = array();
for ($loopCount = 1; $loopCount<49; $loopCount=$loopCount + 1)
{
$H[$loopCount] = 0;
}
But it is not working at all. How to make it work?? Any idea??
EDIT RESOLVED : Thanks To Ghost helping me out.
Here my revisi code :
$H = array();
for ($loopCount = 1; $loopCount<49; $loopCount=$loopCount + 1)
{
$H{$loopCount} = 0;
}
And just call $H{1} , is complete work. Thanks again to Ghost you're hero :)
You could try this :
$loopCount = 1;
$H = '';
for ($loopCount = 1; $loopCount<48; $loopCount=$loopCount + 1)
{
${"H" . $loopCount} = 0;
}
Below is the code which I'm willing to compress:
$somefield = 0;
if ($config->get('var1.one') && is_numeric($config->get('var1.one'))) {
$somefield = $this->entityManager->getStorage('node')->load($config->get('var1.one'));
}
$different_field = 0;
if ($config->get('var2.two') && is_numeric($config->get('var2.two'))) {
$different_field = $this->entityManager->getStorage('node')->load($config->get('var2.two'));
}
After your comment, what I think you want is something like:
$somefield = checkVar("var1.one");
$different_field = checkVar("var2.two");
function checkVar($name) {
if ($config->get($name) && is_numeric($config->get($name))) {
return $this->entityManager->getStorage('node')->load($config->get($name));
} else {
return 0;
}
}
I think this what you wanted. The names I used are not necessarily the best ones, you should use some better suited to the actual use of the function.
Another way to do it.
$field = [];
$array= ['var1.one', 'var2.two'];
for ($i = 0; $i < count($array); $i++) {
if ( $config->get($array[i] ) && is_numeric($config->get($array[$i])) )
{
array_push($field, $this->entityManager->getStorage('node')->load($config->get($array[$i])));
}
}
I am using multiple mysql queries from different databases, pulling out the values from the while loop with arrays and them reuniting them in a loop as this one:
$len = max(count($insrtdate),count($energy));
for($i=0;$i<$len;$i++){
$date = isset($insrtdate[$i]) ? $insrtdate[$i] : '';
$energy1 = isset($energy[$i]) ? $energy[$i] : '';
echo $energy1;
}
The value $energy1 is initially 0 and i want to detect the row in which this changes to any number and the row in which this returns to the initial value of 0.
I'm not 100% sure that I understand your question, but this should be what you want:
$len = max(count($insrtdate), count($energy));
$lastEnergy1 = null;
for($i=0; $i < $len; $i++){
$date = isset($insrtdate[$i]) ? $insrtdate[$i] : '';
$energy1 = isset($energy[$i]) ? $energy[$i] : '';
if ($lastEnergy1 !== $energy1) {
$onState = ($energy1 == "1" ? "on" : "off");
echo "Energy1 turned " . $onState . " at " . $date . "<br />";
$lastEnergy1 = $energy1;
}
}
Is there a easier/better way to get every second hour than this
if(date("H")=='00'){$chart_updates = '|02|04|06|08|10|12|14|16|18|20|22|00';}
if(date("H")=='01'){$chart_updates = '|03|05|07|09|11|13|15|17|19|19|23|01';}
if(date("H")=='02'){$chart_updates = '|04|06|08|10|12|14|16|18|20|21|00|02';}
if(date("H")=='03'){$chart_updates = '|05|07|09|11|13|15|17|19|21|23|01|03';}
if(date("H")=='04'){$chart_updates = '|06|08|10|12|14|16|18|20|22|00|02|04';}
if(date("H")=='05'){$chart_updates = '|07|09|11|13|15|17|19|21|23|01|03|05';}
if(date("H")=='06'){$chart_updates = '|08|10|12|14|16|18|20|22|00|02|04|06';}
if(date("H")=='07'){$chart_updates = '|09|11|13|15|17|19|21|23|01|03|05|07';}
if(date("H")=='08'){$chart_updates = '|10|12|14|16|18|20|22|00|02|04|06|08';}
if(date("H")=='09'){$chart_updates = '|11|13|15|17|19|21|23|01|03|05|07|09';}
if(date("H")=='10'){$chart_updates = '|12|14|16|18|20|22|00|02|04|06|08|10';}
if(date("H")=='11'){$chart_updates = '|13|15|17|19|21|23|01|03|05|07|09|11';}
if(date("H")=='12'){$chart_updates = '|14|16|18|20|22|00|02|04|06|08|10|12';}
if(date("H")=='13'){$chart_updates = '|15|07|19|21|23|01|03|05|07|09|11|13';}
if(date("H")=='14'){$chart_updates = '|16|08|20|22|00|02|04|06|08|10|12|14';}
if(date("H")=='15'){$chart_updates = '|17|09|21|23|01|03|05|07|09|11|13|15';}
if(date("H")=='16'){$chart_updates = '|18|20|22|00|02|04|06|08|10|12|16|16';}
if(date("H")=='17'){$chart_updates = '|19|21|23|01|03|05|07|09|11|13|15|17';}
if(date("H")=='18'){$chart_updates = '|20|22|00|02|04|06|08|10|12|14|16|18';}
if(date("H")=='19'){$chart_updates = '|21|23|01|03|05|07|09|11|13|15|17|19';}
if(date("H")=='20'){$chart_updates = '|22|00|02|04|06|08|10|12|14|16|18|20';}
if(date("H")=='21'){$chart_updates = '|23|01|03|05|07|09|11|13|15|17|19|21';}
if(date("H")=='22'){$chart_updates = '|00|02|04|06|08|10|12|14|16|18|20|22';}
if(date("H")=='23'){$chart_updates = '|01|03|05|07|09|11|13|15|17|19|21|23';}
I need this for google charts and wanted to check if this way is stupid.
1) take the current hour
2) mod2 (there are only two different sets of numbers, odd and even)
3) build array of hours
4) sort array by value
5) split array where the original hour was, and recombine.
$h = date("H");
$line = '';
for($i=0; $i<=24; $i++)
{
if($i % 2 == $h % 2)
$line .= '|' . ($i < 10 ? '0'.$i : $i);
}
One way is to create an array with keys:
$theHour['00'] = '|02|04|06|08|10|12|14|16|18|20|22|00';
Then you can call it like this:
$chart_updates = $theHour[date("H")];
There is also probably a better way to generate this too, but since you already typed it out, its there.. It would just suck if you want to make a change.
Nice code :)
There's actually much easier way to do this in php:
$chars = array();
$start = date("H")+2;
for( $i = 0; $i < 12; $i++){
$chars[] = str_pad( ($start+2*$i)%24, 2, '0', STR_PAD_LEFT);
}
$chart_updates = '|' . implode( '|', $chars);
function helper_add($h,$plus=0){
if($h+$plus > 23){
return $h+$plus-24;
}
return $h+$plus;
}
function helper_10($in){
return $in < 10 ? '0'.$in : $in;
}
function getchartupdates(){
$now = date('G');
for($i=($now%2==0?0:1); $i<=24 ;$i+=2)
$res[] = helper_10(helper_add($now,$i));
return '|'.implode('|',$res);
}
used this to test it !