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 6789479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:36:32+00:00 2026-05-26T17:36:32+00:00

I have an array like so: Array ( [740073] => Leetee Cat 1 [720102]

  • 0

I have an array like so:

Array ( [740073] => Leetee Cat 1 [720102] => cat 1 subcat 1 [730106]
=> subsubcat [740107] => and another [730109] => test cat )

I want to remove all elements of the array that come after the element with a key of ‘720102’. So the array would become:

Array ( [740073] => Leetee Cat 1 [720102] => cat 1 subcat 1 )

How would I achieve this? I only have the belw so far…

foreach ($category as  $cat_id => $cat){
    if ($cat_id == $cat_parent_id){
    //remove this element in array and all elements that come after it 
    }
}

[EDIT] The 1st answer seems to work in most cases but not all. If there are only two items in the original array, it only removes the first element but not the element after it. If there are only two elements

Array ( [740073] => Leetee Cat 1 [740102] => cat 1 subcat 1 )

becomes

Array ( [740073] => [740102] => cat 1 subcat 1 )

Why is this? It seems to be whenever $position is 0.

  • 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-26T17:36:33+00:00Added an answer on May 26, 2026 at 5:36 pm

    Personally, I would use array_keys, array_search, and array_splice. By retrieving a list of keys using array_keys, you get all of the keys as values in an array that starts with a key of 0. You then use array_search to find the key’s key (if that makes any sense) which will become the position of the key in the original array. Finally array_splice is used to remove any of the array values that are after that position.

    PHP:

    $categories = array(
        740073 => 'Leetee Cat 1',
        720102 => 'cat 1 subcat 1',
        730106 => 'subsubcat',
        740107 => 'and another',
        730109 => 'test cat'
    );
    
    // Find the position of the key you're looking for.
    $position = array_search(720102, array_keys($categories));
    
    // If a position is found, splice the array.
    if ($position !== false) {
        array_splice($categories, ($position + 1));
    }
    
    var_dump($categories);
    

    Outputs:

    array(2) {
      [0]=>
      string(12) "Leetee Cat 1"
      [1]=>
      string(14) "cat 1 subcat 1"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array like this: $test = array(20, 30, 40); And I want to
I have array like this: $test = array(20, 30, 40); And I want to
I have an array of array like below with all numeric values. I want
I have an array like this: [234, 235 , 343, 445] I want to
I have an array like this. What i want is to get the value
I have an array like: [1,2,3,4,5,6,7,8,9] And I want to get items in duets
I have an array like Array ( [select_value_2_1] => 7 ) I want to
I have an array like so: @switch_ports = () and then want to add
I have an array like $keywords = array('apple'=>10,'orange'=>2,'grape'=>12); I want to randomly pick one
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>

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.