-1

I want to check if JSON array in the databse has a value. Every value corresponds to a WordPress category ID. (For example, "elevator" = 120)
If it does exist, I need to push it to an array called $features so that I can later run a script that creates WordPress post where this array of exsisting numbers (IDs) are inserted to a post, like this on this example:

$my_post = array(
'post_title'    => $post_title,
'tax_input'    => array(
               'property_category' => array( $features )

The main code (I know I might be far off):

$options = explode(",",$db->options); 
$features = array();

        switch ($features) {
    case in_array("basement", $options) && "basement" != false:
        array_push($features,145);
        break;
    case (in_array("bathtub", $options) && "bathtub" != false):
        array_push($features,167);
        break;
    case (in_array("boiler", $options) && "boiler" != false):
        array_push($features,105);
        break;
}
  • 1
    What are you trying to do with `"basement" != false`? Also can you add an example value of `$db->options` so we can see what you are processing. – Nigel Ren Feb 07 '20 at 17:51

3 Answers3

1

You can achieve your goal using if instead of switch

if (in_array("basement", $options)) {
    array_push($features,145);
}
if (in_array("bathtub", $options)) {
    array_push($features,167);
}
if(in_array("boiler", $options)) {
    array_push($features,105);
}

because the switch statement evaluates, for example, to

switch ([]) {
   case true: ...
   case false: ...
   case true: ...

which does not do what you intend

Nicolas Heimann
  • 2,561
  • 16
  • 27
1

Assuming that you have a list of values for the options, you can create an array with the option and value combination. Then use array_intersect_key() to match only the options selected (using array_flip() to make the option values the keys instead. Finally use array_values() to remove the keys...

$options = ['basement', 'bathtub'];
$optionValues = ['basement' => 145, 'bathtub' => 167, 'boiler' => 105];

$features = array_values(array_intersect_key($optionValues, array_flip($options)));

print_r($features);

gives...

Array
(
    [0] => 145
    [1] => 167
)

This has the advantage of when/if you need to change the mapping, you only need to update the $optionValues array.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

You have a lot going on here that needs to be fixed. First, let's suppose that $options is a fairly standard array:

$options = ['basement', 'bathtub'];

Now you set up $features as an array to hold the result:

$features = array();

You cannot do a lot of evaluation in a switch statement. In the statement in_array("basement", $options) && "basement" != false: you're trying to do two tests for "basement" when all you need to do is let the switch decide if the information is there. Just test each value buy looping through the options and testing the value

foreach($options as $option) {
    switch ($option) {
        case "basement":
            array_push($features,145);
            break;
        case "bathtub":
            array_push($features,167);
            break;
        case "boiler":
            array_push($features,105);
            break;
    }
}

Here are the results:

print_r($features);

Which returns

Array
(
    [0] => 145
    [1] => 167
)
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119