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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:37:30+00:00 2026-05-29T06:37:30+00:00

$arr = array(‘a’ => 1, ‘b’ => 2); $xxx = &$arr[‘a’]; unset($xxx); print_r($arr); //

  • 0
$arr = array('a' => 1, 'b' => 2);

$xxx = &$arr['a'];

unset($xxx);

print_r($arr);  // still there :(

so unset only breaks the reference…

Do you know a way to unset the element in the referenced array?

Yes, I know I could just use unset($arr['a']) in the code above, but this is only possible when I know exactly how many items has the array, and unfortunately I don’t.

This question is kind of related to this one (this is the reason why that solution doesn’t work)

  • 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-29T06:37:31+00:00Added an answer on May 29, 2026 at 6:37 am

    I may be wrong but I think the only way to unset the element in the array would be to look up the index that matches the value referenced by the variable you have, then unsetting that element.

     $arr = array('a' => 1, 'b' => 2);
     $xxx = &$arr['a'];
    
     $keyToUnset = null;
     foreach($arr as $key => $value)
     {
          if($value === $xxx)
          {
              $keyToUnset = $key;
              break;
          }
     }
     if($keyToUnset !== null)
         unset($arr[$keyToUnset]);
     $unset($xxx);
    

    Well, anyway, something along those lines. However, keep in mind that this is not super efficient because each time you need to unset an element you have to iterate over the full array looking for it.

    Assuming you have control over how $xxx is used, you may want to consider using it to hold the key in the array, instead of a reference to the element at the key. That way you wouldn’t need to search the array when you wanted to unset the element. But you would have to replace all sites that use $xxx with an array dereference:

    $arr = array('a' => 1, 'b' => 2);
    $xxx = 'a';
    
    // instead of $xxx, use:
    $arr[$xxx];
    
    // to unset, simply
    unset($arr[$xxx]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function get_arr($arr) { unset($arr[0]); } $arr1 = array(1,2); $arr2 = array(1,2); get_arr(&$arr1); get_arr($arr2); echo
Is there a better shorter way than iterating over the array? int[] arr =
There is array (in JavaScript) of items written in the following way: var arr
Given an array: arr = [['a', '1'], ['b','2'], ['c', '3']] Whats the best way
class ArrayApp{ public static void main(final String[] args){ long[] arr; // reference to array
Here is my array: arr[0]='A'; arr[1]='B'; .... I tried to post it this way:
Hi when I have an array in actionscript var arr : Array = new
I have an array ( arr ) of elements, and a function ( f
You can initialize an array like this: int [ ] arr = { 1,
I have an array of integers in string form: var arr = new string[]

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.