Stack Exchange
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...
$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');
    }


Do you expect to limit the number of elements in $shortcodes or use apply_filters to filter down?  Can you explain which two you are expecting and why?  Unless I am missing something, $shortcodes is getting built with 16 elements.

These elements WPBUSDIRMANVIEWLISTINGS',                                                'WPBUSDIRMANMVIEWLISTINGS',                                                'businessdirectory-view_listings',                                                'businessdirectory-viewlistings',                                                'businessdirectory-listings' 
are all refering to the same thing. I can use one of them to display the directory listings. But I need to limit how many listings will be displayed, instead of displaying everything that is on the table.

For example:
I use WPBUSDIRMANVIEWLISTINGS in my code, so I get the output:
Listing 1 Listing 2 Listing 3 Listing 4 Listing 5 Listing 6 Listing 7 Listing 8 Listing 9 Listing 10
But what I actually need is:
Listing 1 Listing 2
Got it?

Not really.  Is there something wrong with this function or the code where you are getting the 10 listing output?  If the latter, maybe you should show that.  Also, is apply_filters a Wordpress function?  If so, you should probably add the Wordpress tag to your question to get help.

It's wordpress. WPBUSDIRMANVIEWLISTINGS is used like [WPBUSDIRMANVIEWLISTINGS] in the wordpress page html, the shortcode represents a whole a php code, it's like a compilation. [WPBUSDIRMANVIEWLISTINGS] should output the listings, but it's calling 10 listings at once, I only need it to call 2. I don't know I can express that for you, sorry.

Hopefully this will make sense to a Wordpress user.

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.

Do I really need to use both? Can't I just use one?
Like:
count($shortcodes);     if($count>=2)         array_shift($shortcodes);     $shortcodes[];

Ok, I'll make a test. But should I insert it before or after return apply_filters( 'wpbdp_shortcodes', $shortcodes );     }
???

Please see the edit. My value is 'WPBUSDIRMANMVIEWLISTINGS', that value extracts the table results, like Result 1, Result 2 and Result 3 at once. And the string getting the array is $shortcodes.
