0

I am having trouble returning a value from a multidimensional array using a key ONLY when I externalize to create a function.

To be specific, the following code will work when inline in a page:

<?php 
foreach ($uiStringArray as $key) {
  $keyVal = $key['uid'];        
  if($keyVal == 'global001') echo $key['uiString'];
}
?>

However, if I externalize the code as a function, like so:

function getUIString($myKey) {
   // step through the string array and find the key that matches the uid, 
   // then return uiString

   $myString = "-1";

   foreach ($uiStringArray as $key) {
      $keyVal = $key['uid'];

      if($keyVal == 'global001') { 
         $myString = $key['uiString'];

      }

   }

   return $myString;

}

And then call it like this:

<?php getUIString('global001'); ?>

It always returns -1, and will do so even if I use an explicit key in the function rather than a variable. I can't understand why this works inline, but fails as a function.

I'm a relative PHP noob, so please forgive me if this includes a glaring error on my part, but I've searched all over for discussion of this behavior and found none.

All help appreciated.

1
  • You have undefined var $uiStringArray in your function. Commented Mar 23, 2011 at 6:11

3 Answers 3

3

i think you need to take a look at PHP's Variable Scope. To problem is that PHP isn't typical of other languages where a variable defined outside of a function is visible within. You need to use something like the $GLOBALS variable or declare the variable global to access it.

To better illustrate, picture the following:

$foo = "bar";
function a(){
  // $foo is not visible
  echo $foo;
}
function b(){
  global $foo; // make $foo visible
  echo $foo;
}
function c(){
  // acccess foo within the global space
  echo $GLOBALS['foo'];
}

The same is basically holding true for your $uiStringArray variable in this scenario.

Sign up to request clarification or add additional context in comments.

Comments

1

This is a problem with variable scope, see Brad Christie's answer for more details on variable scope.

As for your example, you need to either pass the array to the function or create it inside the function. Try:

function getUIString($myKey, $uiStringArray = array()) {
   // step through the string array and find the key that matches the uid, 
   // then return uiString

   $myString = "-1";

   foreach ($uiStringArray as $key) {
      $keyVal = $key['uid'];

      if($keyVal == 'global001') { 
         $myString = $key['uiString'];
         break;
      }
   }

   return $myString;
}

And call the function using

<?php getUIString('global001', $uiStringArray); ?>

2 Comments

That is indeed the problem, but this answer doesn't offer guidance/direction and how to avoid the issue in the future. From the OPs perspective, "what do you mean the function doesn't have the variable defined? It's defined in the page itself."
@Brad Good point, still trying to improve at creating answers on SO, will definitely remember that in the future, thanks
0

You are having this problem because you are overriding you $mystring variable even if it matches. Send your array as your parameter. It is unknown to your function.You just use break if variable matches

function getUIString($myKey, $uiStringArray=array()) {
   // step through the string array and find the key that matches the uid, 
   // then return uiString

   $myString = "-1";

   foreach ($uiStringArray as $key) {
      $keyVal = $key['uid'];

      if($keyVal == 'global001') { 
         $myString = $key['uiString'];
          break;

      }

   }

   return $myString;

}

1 Comment

Variable scope is real the issue at-hand here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.