Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8637785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:33:28+00:00 2026-06-12T10:33:28+00:00

I have the following function: function array_duplicates($array) { $duplicates = array(); $unique = array_unique($array);

  • 0

I have the following function:

function array_duplicates($array)
{
    $duplicates = array();
    $unique = array_unique($array);
    for ($i = 0; $i < count($array); $i++) {
        if (!array_key_exists($i, $unique)) {
            $duplicates[] = $array[$i];
        }   
    }   
    return $duplicates;
}

This function obviously doesn’t apply to any certain model, and it’s not a template helper. Where would be the appropriate place to put this function? (Please don’t say “anywhere you want.”)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:33:29+00:00Added an answer on June 12, 2026 at 10:33 am

    This might be the type of thing you’d put into a service. Create a class like this:

    class ArrayUtils
    {
        function array_duplicates($array)
        {
            ... 
            return $duplicates;
        }
    }
    

    And then define it as a service. If you’re using YAML, you’d put something like this into your config.yml file:

    services:
        arrayUtils:
            class:        Full\Path\To\ArrayUtils
    

    Under this configuration, Symfony will create a single instance of your ArrayUtils, and give all your controllers access to it. Then you can call it like this:

    class SomeController
    {
        public function someAction()
        {
            ...
            $duplicates = $this->get("arrayUtils")->array_duplicates($array);
            ...
        }
    }
    

    This is all part of Symfony’s dependency injection framework. It’s pretty cool, and I recommend reading up on it here: http://symfony.com/doc/2.0/book/service_container.html

    Alternative Option

    Now, that might be a little overkill for such a small chunk of code. If you’re only going to be using this in a single bundle, then you might want to just put it into a base controller, and have all your other controllers extend that base controller.

    class BaseController
    {
        function array_duplicates($array)
        {
            ...
        }
    }
    
    class SomeController extends BaseController
    {
        function someAction()
        {
            $this->array_duplicates($array);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code function generate_pdf() { $fdf_data_strings = $this->get_hash_for_pdf(); #$fdf_data_names = array('49a'
I have this function for removing duplicates in arrays: Function remove_duplicates(array) Dim dup_count As
I have the following function: addBook : function() { _authors = new Array(); authors
I have the following code: function build_all_combinations(input_array){ array = [1,2,3] limit = array.length -
All, I have the following array and function, which will create a multidimensional array
I have the following Symfony2 form: public function buildForm(FormBuilder $builder, array $options) { $builder
I have the following code: <?php function foo($bar) { global $products; //$products = array();
have the following function on my collection : getFiltered: function (status, city) { return
i have following function where i have brands info in an array. i should
I have the following function to convert a byte array of pixels into an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.