php - How can I limit the amount of array items -
i've got 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 part...
$shortcodes += array_fill_keys( array( 'wpbusdirmanviewlistings', 'wpbusdirmanmviewlistings', 'businessdirectory-view_listings', 'businessdirectory-viewlistings', 'businessdirectory-listings' ), array( &$this, '_listings_shortcode' ) );
...is supposed display directory listings in website. works. retrieving 10 listings according plugin settings. need retrieve 2. in other words, need limit it.
i've tried using array_slice
, array_splice array_unique
retrieve few values, didn't work.
what doing wrong?
for example, tried using 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, php manual logic correct, it's still not working, not getting errors though, tried these 2 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'); }
you can obtaining array length , if length less or equal desired amout , add element end of array.
here simple example that, don't know if want... :)
if (!in_array($expectedarray, $yourarray)){ if(count($yourarray)>=2) array_shift($yourarray); $yourarray[] = $expectedarray; }
for more information : $expectedarray
not an array, it's value expect $yourarray
please check this in_array example.
Comments
Post a Comment