Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WP Plugin – Array

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #30362
    i0nyy
    Member

    Hello. I want to create a WP Plugin and I have a problem.

    The people that will use the plugin can configure the plugin from the dashboard. In the dashboard they can enter id`s of some posts. e.g: 1, 324, 34

    i.e:

    $post_id = 1;
    $posts_for_plugin = “1,324.34”;

    I need something like


    if ($post_id is somewhere in $posts_for_plugin)
    {
    return something;
    }else{
    return something else;
    }

    If it was a single value it would be something like


    if ($post_id ==$posts_for_plugin )

    but with more than one value, I dont`t know what to do.

    I tried using

    array($posts_for_plugins)

    but the array is something like

    array[0] = 1, 324, 34

    so it`s useless

    Hope anyone understood an can help me.
    Thanks!

    #79019
    Bob
    Member

    You can use the in_array() function for that. Here’s an example:

    
    
    $post_id = 1;
    $posts_for_plugin = array(1,324,34);

    if ( in_array($post_id, $posts_for_plugin) ) {
    echo "Yep, it's in the array";
    }
    else {
    echo "Sorry, not in array";
    }

    ?>

    The in_array function has 2 parameters here (you can also set a third). The first parameter is the string you’re looking for in the array, which is $post_id here. The second parameter is the array in which the first parameter has to be found, here that is $posts_for_plugin. The second parameter ofcourse has to be an array.

    #79012
    i0nyy
    Member

    The $posts_for_plugins values will be set from the dashboard, so

    $posts_for_plugin will be something like: e>$posts_for_plugin = "1,324.34";

    if i try to insert $posts_for_plugin in an array it won`t work…

    How can i make this:

    $posts_for_plugin = "1,324.34";

    to be an array?

    #79011
    Bob
    Member

    Can’t you just do this, just add array(…) to it? Like this:

    $posts_for_plugin = array(1,324,34);

    I don’t know for sure though, I don’t know a whole lot about PHP.

    Also, how exactly are they set from the dashboard? Perhaps you can make it so that it will be inserted in an array immediately?

    #78968
    willB
    Member

    If you use explode():


    $string = "1,324,34";
    $posts_for_plugin = explode(',', $string);

    You can pretty much leave your code that is getting the string from the dashboard the same.
    So I guess in full if would be something like:


    $post_id = 1;
    $string = "1,324,34";
    $posts_for_plugin = explode(',', $string);

    if ( in_array($post_id, $posts_for_plugin) )
    {
    echo "Yep, it's in the array";
    }
    else
    {
    echo "Sorry, not in array";
    }

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Back End’ is closed to new topics and replies.