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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:20:47+00:00 2026-05-24T06:20:47+00:00

function outer_function() { $nid = 3; function exists($var) { print $nid; return $var->nid ==

  • 0
function outer_function() {
  $nid = 3;

    function exists($var) {
    print $nid;
    return $var->nid == $nid;
    }   

    $a_filtered_array = array_filter($an_array, "exists");
}

I’m trying to filter this array using a variable that’s defined in the outer function, but the variable is not defined. This would work in JS. What am I doing wrong here? How would I accomplish this in PHP?

  • 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-24T06:20:47+00:00Added an answer on May 24, 2026 at 6:20 am

    You could write this (correctly) as:

    function outer_function() {
      $nid = 3
    
        $a_filtered_array = array_filter($an_array, function ($var) use ($nid) {
            print $nid;
            return $var->nid == $nid;
        });
    }
    

    You can’t just pass a string containing the function’s name. You could also write

    function outer_function() {
      $nid = 3
      $exists =  function ($var) use ($nid) {
            print $nid;
            return $var->nid == $nid;
        }
        $a_filtered_array = array_filter($an_array, $exists);
    }
    

    See http://www.php.net/manual/en/functions.anonymous.php for in-depth on the syntax and semantics.

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

Sidebar

Related Questions

when I code: var a = function() { alert(44) return function(){alert(33)} }()(); is this
I defined two functions for array: Array.prototype.remove = function(obj) { var i = this.length;
function returnsAnArray () { return array ('test'); } echo returnsAnArray ()[0]; generates a syntax
I'm using the navigator.geolocation.getCurrentPosition(function) api in firefox 3.6. When i try to call this
In this example var a = 1; ( function(x) { function inner() { alert(a);
I know the following code can access outer this. var o = function() {
function Submit_click() { if (!bValidateFields()) return; } function bValidateFields() { /// <summary>Validation rules</summary> ///
function register_contact ($person = array()) { $nogood = false; foreach ($person as $val) {
function Obj1(param) { this.test1 = param || 1; } function Obj2(param, par) { this.test2
Let's say we have this code (forget about prototypes for a moment): function A(){

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.