Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In order to pass an array in the code of create_function(), passing it to the parameter works,

$array = array('a', 'b', 'c');

$func = create_function('$arr', '
            foreach($arr as $var) echo "$var<br />";
        ');

$func($array);

But I'd like to embed an array in the code directly like the eval() capability. Like this,

$array = array('a', 'b', 'c');
eval( 'foreach($array as $var) echo "$var<br />";');

However, this doesn't work.

$array = array('a', 'b', 'c');

$func = create_function('', '
            foreach(' . $array . ' as $var) echo "$var<br />";
        ');

$func();

So how do I do it? Thanks for your info.

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

In case you insist on create_function instead of lambda functions

$func = create_function('', '
  foreach(' . var_export($array, true) . ' as $var) echo "$var<br />";
');

You need a (valid) string representation of the array for the php parser. var_export provides that.
But Berry Langerak's answer is the way to go.

share|improve this answer
    
Thanks. This answers my question. –  Teno Sep 20 '12 at 11:00
add comment

When using PHP >= 5.3, you can use an anonymous function instead of create_function. The anonymous function can "use" a variable from the outer scope.

<?php
$array = array('a', 'b', 'c');

$func = function( ) use( $array ) {
    foreach( $array as $value ) {
        echo $value;
    }
};

$func( );
share|improve this answer
    
Thanks, it works beautifully on my test server. Some of the servers I'm using are below PHP v5.3 unfortunately. –  Teno Sep 20 '12 at 10:56
2  
@Teno I'm sorry to tell you, but in that case some of the servers you're using are using software that's not supported and should be upgraded. PHP 5.2 end of support was in December 2010. ;) –  Berry Langerak Sep 20 '12 at 11:30
    
Oh really... They are free shared hosts so I cannot do anything about it. Thanks for telling me that. –  Teno Sep 20 '12 at 11:35
add comment

Use a closure:

$function = function($array){

    foreach($array as $var){

        // do your stuff

    }

};

$function($yourArray);
share|improve this answer
    
Thanks for your input. –  Teno Sep 20 '12 at 10:57
add comment

I'm posting this only for educational purposes (it's very bad practice):

<?php

$array = array('a', 'b', 'c');

$func = create_function('', '
            global $array;
            foreach($array as $var) echo "$var<br />";
        ');

$func();

Why is it so awfull?

  • global variables
  • ugly way to create a function

If your version of PHP is at least 5.3 then use lambda function. See @Berry Langerak answer

share|improve this answer
    
I'm not a big fan of global variables either. Thank for your answer though. –  Teno Sep 20 '12 at 10:54
    
why the downvotes? it does the job and i explained the drawbacks. –  Vlad Balmos Oct 9 '12 at 8:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.