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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:29:00+00:00 2026-06-17T02:29:00+00:00

I have the following script for a Google maps application. The information about a

  • 0

I have the following script for a Google maps application. The information about a specific place is sent via ajax to the $array array which is then added to the $SESSION[‘infomarker’]. This part works fine. But the user should also be able to delete a place (If they regret the input).

This is handled by the removeElementWithValue() function, which takes the places lat and lng as arguments ($val1 and $val2 in this sample script). The array which contains the ‘val1’ and ‘val2’ values then disappears, like they should. But when I change the $val1 and $val2 variables to ‘val3’ and ‘val4’, the array which contained ‘val1’ and ‘val2’ returns while the array which contains ‘val3’ and ‘val4’ is unset.

I thought that unset() ‘unset’ the array in question for good, or do I need to do something else?

<?php
session_start();


if(isset($_POST['lat']) && !empty($_POST['lat']) ) {
$array = array(
                'titel' => $_POST['titel'], 
                'comment' => $_POST['comment'], 
                'lat' => $_POST['lat'], 
                'lng' => $_POST['lng'] 
                );


$_SESSION['infomarker'][] = $array;


   }    

  $val1 = 'val1';

  $val2 = 'val2';


  $newarray = removeElementWithValue($_SESSION['infomarker'], "titel", "comment", $val1, $val2);

 function removeElementWithValue($array, $key1, $key2, $value1, $value2){
 foreach($array as $subKey => $subArray){
      if($subArray[$key1] == $value1) {
      if($subArray[$key2] == $value2) 

         unset($array[$subKey]);

      }

   }
return $array;

}

print_r($newarray);
  • 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-17T02:29:01+00:00Added an answer on June 17, 2026 at 2:29 am

    You are modifying the $array that is being passed to your removeElementWithValue method. You are passing the $_SESSION array into the method.

    unset is doing what it’s supposed to, by removing the value from $array. But, your method is actually receiving a copy of the $_SESSION variable since PHP will pass arrays by-value not by-reference.

    You can prefix the $array variable in the function declaration with an amperstand (&) character, which will inform PHP to pass that variable to the function by-reference, and will cause any modifications made to the array inside of the function to change the source array ($_SESSION).

    Clarification:

    Calling a function creates a scope in the “stack” for that method. PHP, by default, passes everything by-value (for those who want to argue that objects are passed by reference – they’re not. The reference to the object is passed by-value).

    What does that mean? Well, it’s easier than it seems. Take the code below:

    function myfunc($value) {
      $value = $value + 1;
    }
    
    $testing = 1;
    myfunc($testing);
    var_dump($testing);
    

    This will output int(1). Notice how, even though we increment the value inside of the function – the result doesn’t change the $testing variable. This is because the $testing variable was declared in a different scope inside of the stack. As far as PHP saw, when we called the method, we actually called this myfunc(1) (notice the number instead of a variable). The variable’s value was grabbed from memory, and that value was passed to the method.

    This is where passing by-reference comes in handy. You can instruct PHP to pass by-reference by prefixing any argument in the function definition with an amperstand, so you get this:

    function myfunc(&$value) {
      $value = $value + 1;
    }
    
    $testing = 1;
    myfunc($testing);
    var_dump($testing);
    

    The addition of the simple ampersand will now make this code print int(2).

    So what’s the difference? Well, you’ll remember that I said the first time we are basically calling myfunc(1). The second time, we are effectively calling myfunc(0xff940ac4) where 0xff940ac4 is the memory address to the $testing variable. Now, any changes we make to $value inside of the method will change the data at memory address 0xff940ac4, instead of the value that was “copied” into the scope for this method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script that creates a div, adds a Google Maps DOM
So, i have the following script to use the google maps API, its all
I have the following script which randomly assigns different colours (from an array) to
I have the following script which first shows only the first para of the
I have the following script which appears multiple times on my page. I have
I have the following code in a Google map script... <a class='btn btn-modal' data-target='popup.html'>Go</a>
I have the following code which creates a map on a content page. <script
I have the following Javascript where I modified the standard Google Maps API initialize()
I have developed the following user script which will show the html element under
I have the following snippet which works as expected: <a href=http://google.com onclick=return false;>Will go

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.