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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:05:39+00:00 2026-05-24T05:05:39+00:00

I have a file in PHP with a large array and a function that

  • 0

I have a file in PHP with a large array and a function that searches the array.

Heres the code:

$details = array(

  array('fieldID'=>'0','fieldCaption'=>'Address 1','fieldType'=>'text','fieldName'=>'addr1','fieldRequired'=>'1'),
  array('fieldID'=>'1','fieldCaption'=>'Address 2','fieldType'=>'text','fieldName'=>'addr2','fieldRequired'=>'1'),
  .
  .
  .
);

if(!function_exists('find_detail'))
{
  function find_detail($fieldName)
  {
    global $details;
    var_dump($details); // NULL

    foreach ($details as $detail)
    {
      if($detail['fieldName'] == $fieldName)
      {
        return $detail['fieldID'];
      }
    }
    return false;
  }
}

This sits inside a single helper file and calling var_dump($details); is returning NULL. I suspect an issue with scoping.

Many Thanks

— edit —

This is bad practice and the best approach to handling this would be (as @Gordon stated) to pass the details array into the function. (Warning: There exist better ways of doing this, for example using array_filter)

if(!function_exists('find_detail_by_field_name'))
{
  function find_detail_by_field_name($details, $fieldName)
  {
    foreach ($details as $detail)
    {
      if($detail['fieldName'] == $fieldName)
      {
        return $detail['fieldID'];
      }
    }
    return false;
  }
}

And provide a way to get the details, wrapping details in a function would provide many benefits such as caching, change of source etc.

function get_details() 
{
   static $details = array(...)
   return $details;
}
  • 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-24T05:05:39+00:00Added an answer on May 24, 2026 at 5:05 am

    If your function operates on that $details array, it should ask for that array in the function signature. Anything else is just hiding dependencies and is hard to debug and maintain (as you can see by the need to ask your question). So change it to

    function find_detail($fieldName, array $details)
    

    and foreach over the passed in array then.

    Also, since the function really returns the fieldId and not some detail, you might want to rename it to

    function findFieldIdByFieldName($fieldName, array $details)
    

    But to answer your question: you likely get this effect because you created the $details array outside the global scope, the topmost scope of your script. If you defined $details in a function or a method or if it is included in the scope of another function, it is not in the global scope.

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

Sidebar

Related Questions

I have a large array in PHP. It contains strings that are split into
I have a file with .php extention www.example.com/thefile.php?name=123 that I want to direct the
I have problem with session in cakephp.I have one file chat.php that is in
I have a PHP file that contains a lot of if and else statements
I have a PHP file with one simple echo function: echo 'アクセスは撥ねりません。'; but when
Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5
I have a large PDF file that is a floor map for a building.
I have a large PHP loop that I'm re-using in multiple spots on multiple
I have a very large txt file javascript array - too big for me
i have some file test.php <?PHP $config_key_security = test; ?> and i have some

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.