0

There is a way to find all the arrays stored into a PHP document?

If I have:

$one = array();
$two = array();
$foo = array();

How can I get a list of them into an array like:

$arrays = array($one, $two, $foo);

?

6
  • 2
    Is there an example of a practical application where this solves a specific problem? Commented Oct 9, 2013 at 8:22
  • I have a list of arrays that populate a list in a website and I need to get the same list but for different purposes in another page without know which one are the defined arrays Commented Oct 9, 2013 at 8:26
  • Did you not define the arrays you intend to use? Commented Oct 9, 2013 at 8:27
  • Yes I did, but in an included doc Commented Oct 9, 2013 at 8:27
  • 1
    Then separate your code into three parts: one where you generate those arrays, preferably a function, and two other parts that use that function. Commented Oct 9, 2013 at 8:27

1 Answer 1

2

You can use get_defined_vars():

array get_defined_vars ( void )

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

As it will show up all the environment, server and your variables, you need to "grep" those you want.

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

7 Comments

Thanks but what if the document is not the one where I'm working but it's just included? If I include it them will be mixed with the ones of the document where I'm working and I've no ways to know which one is from the external document...
didn't know this function existed. but if you're grepping for the variables you want, this means that you know what variables you want. Wouldn't it make more sense to just work with them directly in that case?
Good point, @kennypu. Maybe a grep -v way: delete all HTTP variables we know are always set, so the ones that stay are the ones we define.
I may get the defined vars before included the document and then after and make a difference between them?
Well, you can fetch get_defined_vars() before and after including the document, so the difference will be the new ones.
|

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.