How can I limit the amount of array items

I’ve got the following portion of code:

private function get_shortcodes() {
        $shortcodes = array();
        $shortcodes += array_fill_keys( array( 'WPBUSDIRMANADDLISTING',
                                               'businessdirectory-submitlisting' ),
                                        array( &$this->controller, 'submit_listing' ) );
        $shortcodes += array_fill_keys( array( 'WPBUSDIRMANMANAGELISTING',
                                               'businessdirectory-managelistings',
                                               'businessdirectory-manage_listings' ),
                                        array( &$this->controller, 'manage_listings' ) );
        $shortcodes += array_fill_keys( array( 'WPBUSDIRMANVIEWLISTINGS',
                                               'WPBUSDIRMANMVIEWLISTINGS',
                                               'businessdirectory-view_listings',
                                               'businessdirectory-viewlistings',
                                               'businessdirectory-listings' ),
                                        array( &$this, '_listings_shortcode' ) );




        $shortcodes += array_fill_keys( array( 'WPBUSDIRMANUI',
                                               'businessdirectory',
                                               'business-directory' ),
                                        array( &$this->controller, 'dispatch' ) );
        $shortcodes += array_fill_keys( array( 'businessdirectory-search',
                                               'businessdirectory_search' ),
                                        array( &$this->controller, 'search' ) );
        $shortcodes['businessdirectory-featuredlistings'] = array( &$this, '_featured_listings_shortcode' );

        return apply_filters( 'wpbdp_shortcodes', $shortcodes );
    }

Where the part…

Read More
$shortcodes += array_fill_keys( array( 'WPBUSDIRMANVIEWLISTINGS',
                                               'WPBUSDIRMANMVIEWLISTINGS',
                                               'businessdirectory-view_listings',
                                               'businessdirectory-viewlistings',
                                               'businessdirectory-listings' ),
                                        array( &$this, '_listings_shortcode' ) );

…is supposed to display some directory listings in my website.
It works. But it is retrieving 10 listings according to the plugin settings. I need it to retrieve only 2. In other words, I need to limit it.

I’ve tried using array_slice and array_splice array_unique to retrieve only a few values, but it didn’t work.

What am I doing wrong?

FOr example, I tried using it like this:

array_slice( ( &$this, '_listings_shortcode' ),0,2)

No can do.

EDIT

Still trying…

if (!in_array('WPBUSDIRMANMVIEWLISTINGS', $shortcodes)){
    if(count($shortcodes)>=2)
        array_shift($shortcodes);
    $shortcodes[] = 'WPBUSDIRMANMVIEWLISTINGS';
}

EDIT 2

Well, by the php manual logic it is correct, but it’s still not working, not getting any errors though, I tried these two methods:

This

if (!in_array('_listings_shortcode', $shortcodes)){
        if(count($shortcodes)>=1)
            array_shift($shortcodes);
        $shortcodes[] = ('_listings_shortcode');
    }   

And this

if (!in_array('_listings_shortcode', $shortcodes)){
        $count = count($shortcodes);
        if($count>=1)
            array_shift($shortcodes);
        $shortcodes[] = ('_listings_shortcode');
    }

Related posts

Leave a Reply

1 comment

  1. You can do that by obtaining your array length and if the length less or equal to your desired amout and add the element to the end of array.

    Here is the simple example to do that, I don’t know if this what you want… 🙂

    if (!in_array($ExpectedArray, $yourArray)){
        if(count($yourArray)>=2)
            array_shift($yourArray);
        $yourArray[] = $ExpectedArray;
    }
    

    For more information :
    $ExpectedArray is not an Array, it’s a value you expect it to $yourArray Please check this about in_array example.