I want to add values to an array so the output will be like this (1,2,3,4,5).
To accomplish this i tried to use array_push. I've tried a lot to get this working but it just outputs the last number which in this case is '5'. Do you guys see what is wrong with my code? Thanks in advance!
$websites = array();
case 'Hengelsport':
if ($waarde == 'true') {
array_push($websites,1);}
break;
case 'Diervoeders':
if ($waarde == 'true') {
array_push($websites,2);}
break;
case 'Vijverconcurrent':
if ($waarde == 'true') {
array_push($websites,3);}
break;
case 'Broqx':
if ($waarde == 'true') {
array_push($websites,4);}
break;
case 'Dekrabpaal':
if ($waarde == 'true') {
array_push($websites,5);}
break;
$this->articleData['website_ids'] = $websites;
You can't to have other value that last number, if you juste set $websites variable, like this : $websites = array();
And don't forget switch operator.
switch ($i) {
case "Hengelsport":
echo "i égal 0";
if ($waarde == 'true') {
array_push($websites,1);
}
break;
//...
}
Is it full code that you show us ?
Or for an easy way :
$website_ids = array(
'Hengelsport' => 1,
'Diervoeders' => 2,
'Vijverconcurrent' => 3,
'Broqx' => 4,
'Dekrabpaal' => 5
);
if ($waarde == 'true')
{
array_push($websites, $website_ids[$your_var_containing_name]);
}
Related
I got an issue with switch.
Right now from my understanding it works like this:
if ( sizeof( $a ) !== sizeof( $types ) ) {
$type = $a[ 0 ];
switch ( $type ) {
case 'Red' :
$type = 'winered';
break;
case 'blue' :
$type = 'royalblue';
break;
case 'yellow' :
case 'lime' :
break;
case 'beige' :
$type = 'bright';
break;
default :
$type = get_option( 'my_option' );
break;
}
} else {
$type = get_option( 'my_option' );
}
So far so good. Whatever I select, it shows the case.
My issue is, it does it only one by one, I am able to select multiple cases like
case Red: case Blue:
$type = 'winered';
break;
But this won't work for me. In my scenario it is a checkbox I got case "Red" AND case "Blue" selected and want to display both "results": "winered" AND "royalblue". Right now it falls back to royalblue.
Any suggestions?
Thank you!
I dont think it is possible with a switch case to select multiple outcomes. I'd rather use an if statement something like:
if($type = 'red' || $type = 'blue'){
$type = 'winered';
$type = $type.'royalblue';
}
It doesn't really make sense why it would fall back to 'royalblue' other than:
'Red' != 'red', so it never really goes into the first case
Maybe this is how you have the types stored in $a, so that $a[0] is in fact 'blue', I would check that.
In any case, if you were interested in combination of different conditions, I would think that 'regular' if-then-else is the way to go. Case is generally faster, but offers itself for 'simpler' conditionals.
I hope this helps!
Your code is incomplete. However, I think the key is here:
$type = $a[ 0 ];
switch ( $type ) {
You only check the first value of $a. You can check all its values using a simple foreach:
// Put the selected values here; will join them at the end to get a string
$selected = array();
if (count($a) != count($types)) {
foreach ($a as $type) {
switch ($type) {
case 'Red':
$selected[] = 'winered';
break;
case 'blue':
$selected[] = 'royalblue';
break;
case 'yellow':
case 'lime':
// nothing here?!
break;
case 'beige':
$selected[] = 'bright';
break;
default:
$selected[] = get_option('my_option');
break;
}
}
} else {
$selected[] = get_option('my_option');
}
// Join the selected type with spaces
$type = implode(' ', $selected);
function name( $key, $value ) {
switch( $key ) {
case 'name':
break;
// this is where I would determine the key with this pattern 1-100
case 'promote-'.count++ :
break;
}
}
When I echo the $key sample outputs would be contact_number, card_name and the pattern I would like to determine promote-1, promote-2 and so on. The second option "case 'promote-'.count++" is the pattern I need to determine. That any 'promote-1' to 'promote-100' will fall on that option
Use of switch case is very restricted. Here is a working solution for you.
function name( $key, $value ) {
if(strpos($key,'-') > 0){
$key_arry = explode('-' , $key);
if($key_arry[0] == 'promote' && ($key_arry[1] > 0 || $key_arry[1] <= 100)){
echo 'Patern is promote-1, promote-2......promote-100';
}else{
echo 'anything else';
}
}
}
name('promote-2' , 1);
try with regex
case (preg_match('/promote-\d/', $key) ? true : false) :
// do stuff for people whose name is John, Johnny, ...
break;
'/promote-\d/'this will check for patterns - promote-<any digit>
I have an array of conditions :
$arrConditions = array ('>=2', '==1', '<=10');
...which I want to be able to use in an if...statement.
IE.
if (5 $arrConditions[0])
{
...do something
}
...which would be the same as :
if (5 >= 2)
{
...do something
}
Any help?
Thanks
Such a requirement is a sure sign of a bad design.
Most likely you can do that another, more usual way.
Nevertheless, never use eval for such things.
At least store each operator in pairs - an operator and operand.
$arrConditions = array (
array('>=',2),
array('==',1),
array('<=',10),
);
and then use switch:
list ($operator,$operand) = $arrConditions[0];
switch($operator) {
case '==':
$result = ($input == $operand);
break;
case '>=':
$result = ($input >= $operand);
break;
// and so on
}
But again - most likely you can solve it another, much easier way.
What about this ?
<?php
$arrConditions = array('==2', '==9', '==5', '==1', '==10', '==6', '==7');
$count = 0;
$myval = 0;
foreach ($arrConditions as $cond) {
$str = "if(5 $cond) { return $count;}";
$evalval = eval($str);
if (!empty($evalval)) {
$myval = $count;
}
$count++;
}
switch ($myval) {
case 0: echo '==2 satisfied';
break;
case 1: echo '==9 satisfied';
break;
case 2: echo '==5 satisfied';
break;
case 3: echo '==1 satisfied';
break;
case 4: echo '==10 satisfied';
break;
default : echo 'No condition satisfied';
}
?>
I need to do multiple checks for a variable. I've seen an "Equals" example, here: w3schools.
But they are two different variables. Right now I have:
if ($color == 'blue')
{
//do something
}
But I need to to multiple checks for $color. Eg if it equals red or green too. How is this written?
As simple as:
if ($color == 'blue' || $color == 'red' || $color == 'green') {
//do something
}
There are several other options. Using switch operator:
switch ($color) {
case 'blue':
case 'red':
case 'green':
//do something
}
Or more complex using in_array function:
$colors = array('blue', 'red', 'green');
if (in_array($color, $colors)) {
//do something
}
Use a switch-statement.
switch($color)
{
case "blue":
// do blue stuff
break;
case "yellow":
// do yellow stuff
break;
case "red":
// do red stuff
break;
default:
// if everything else fails...
}
In case you want to do the same thing on all colors, just use the || (boolean or) operator.
if ($color == "blue" || $color == "red" || $color == "yellow")
{
// do stuff
}
You can also go with preg_match on this one. This might be overkill, but I'm sure it's very fast!
$color = "blue";
$pattern = "/^red|blue|yellow$/";
if ( preg_match($pattern,$color) ) {
// Do something nice here!
}
Basically, I always use words 'and' & 'or' so, my scripts look like:
if ($value == '1' or $value == '2' or $value == '3') {
//do stuff
}
if ($value == '1' and $user_logged == 'Admin') {
//do stuff
}
It's possible get rid of of $value repeating?
Any benefit in using and and or instead of || and && ?
There is no way to shortcut expressions like
if ($value == 1 or 2 or 3) // invalid
But you can check the values in another way:
if (in_array($value, array(1, 2, 3))) {
// do stuff
}
The difference between or and || is operator precedence. Use || and &&, not or and and.
Answer 1.
$inArray = (1,2,3);
if (in_array($value,$inArray)) {
// do stuff
}
Answer 2.
Operator precedence
You can use first if as a switch statement:
switch($value) {
case "1":
echo "value is 1";
break;
case "2":
echo "value is 2";
break;
case "3":
echo "value is 3";
break;
}
Answer to your second question: Operator precedence