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

  • SEARCH
  • Home
  • 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 7539897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:30:11+00:00 2026-05-30T07:30:11+00:00

I want to remove rows from my array so that my result is an

  • 0

I want to remove rows from my array so that my result is an array that contains rows with unique first and last elements. If two (or more) rows have the same first and last value, I want to preserve the row with the lowest element count.

Say I have the following array:

$var = [
    [1, 2, 3],
    [1, 3],
    [1, 2, 4, 3],
    [1, 3, 4]
];

What I want is to remove all arrays from $var that have the first and last element the same as another array from $var but have more elements.

Because the first three rows all start with 1 and end with 3, only the second row containing [1, 3] should be kept.

The fourth row ([1, 3, 4]) uniquely starts with 1 and ends with 4, so it should also be kept.

The output should be:

[
    [1, 3],
    [1, 3, 4]
]

I am looking for the most efficient way of doing this, both in terms of memory and time. $var may have up to 100 arrays, and each individual array may have up to 10 elements in it. I thought of using some kind of comparison between all two elements (for(i=0;...) for(j=i+1;...) complexCompareFunction();), but I believe this isn’t very efficient.

  • 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-05-30T07:30:12+00:00Added an answer on May 30, 2026 at 7:30 am

    In general, yes, you are too worried about efficiency (as you wondered in another comment). Though PHP is not the most blisteringly-fast language, I would suggest building the most straightforward solution, and only worry about optimizing it or streamlining it if there is a noticeable issue with the end result.

    Here is what I would do, off the top of my head. It is based off of ajreal’s answer but hopefully will be easier to follow, and catch some edge cases which that answer missed:

    // Assume $var is the array specified in your question
    
    function removeRedundantRoutes( $var ){
    
        // This line sorts $var by the length of each route
        usort( $var, function( $x, $y ){ return count( $x ) - count( $y ); } );
    
        // Create an empty array to store the result in
        $results = array();
    
        // Check each member of $var
        foreach( $var as $route ){
            $first = $route[0];
            $last = $route[ count( $route ) - 1 ];
            if( !array_key_exists( "$first-$last", $results ) ){
                // If we have not seen a route with this pair of endpoints already,
                // it must be the shortest such route, so place it in the results array
                $results[ "$first-$last" ] = $route;
            }
        }
    
        // Strictly speaking this call to array_values is unnecessary, but
        // it would eliminate the unusual indexes from the result array
        return array_values( $results );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Ajax form that lets me dynamically remove rows from a table
I want to remove duplicate rows return from a SELECT Query in Postgres I
I want to remove digits from a float to have a fixed number of
I want to remove duplicate rows from a matrix. I read How can I
I want to remove rows from Excel with Delphi7 program. This throws an exception:
I want to remove all rows apart from the row with id 'row0' from
I have this piece of code. I want to remove the rows created with
Hello everone I'm removing rows from a dataset. All rows that I want to
I want remove "Language" querystring from my url. How can I do this? (using
I want to remove a UIView from a superview and add it again at

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.