2

i have some array like this

$post_array = array("title","content","price","dt","cat");

i want to make variable for all of them like this :

$title = $_GET['title'];
$content = $_GET['content'];

is that possible?

1
  • extract(array_intersect_key(array_flip($post_array), $_GET)) Commented Feb 20, 2014 at 11:59

1 Answer 1

2

You can do simply using extract.

extract($_GET);

See doc.

If you want to make sure all of them exist, you can use a for loop this way:

foreach($post_array as $input) {
    $$input = $_GET[$input];
}

This works because in php if say $var is 'title', $$var refers to the variable $title.

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

1 Comment

@sebcap26: Right. Hence, the warning in doc.

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.