0
votes

I have added few values in Wordpress option table like below:

option_name = optionLists

option_values = Option1, Option2, Option3, Option4

I retrieved those values using PHP like below:

$option_lists = get_option('optionLists');
$myOption = explode("," , $option_lists );

So, here $myOption is an array and if I print $myOption using print_r($myOption), it will show result like below:

Array ( [0] => Option1 [1] => Option2 [2] => Option3 [3] => Option4 )

Now, I want to delete one array element from the above array, say it is Option3. I wrote that like below:

$remvOption = array('4' => 'Option3');
$new_option = array_diff($myOption, $remvOption);

The expected output should be like below:

Array ( [0] => Option1 [1] => Option2 [3] => Option4 )

But, it is still showing:

Array ( [0] => Option1 [1] => Option2 [2] => Option3 [3] => Option4 )

May I know where I am doing the mistake? Thanks in advance.

1

1 Answers

0
votes

Your code works for me.

But no need to have the ID of the item - just put the value itself

$myOption = array('Option1', 'Option2', 'Option3', 'Option4');
$remvOption = array('Option3');
$new_option = array_diff($myOption, $remvOption);

And then make sure you are printing the correct variable:

print_r($new_option);

This code gives me output:

 Array ( [0] => Option1 [1] => Option2 [3] => Option4 )