-1

i am trying to create a function to create a set of variables that are arrays like so

function breakDownPOSTArray($count, $fields) {
    $repeat = 1;
    $num = 0;   
    while ($repeat <= $count) {
        $a = "array" . $repeat;
        $$a = array_slice($_POST, $num, $fields);
        $repeat ++;
        $num += 4;
    }

i want to be able to use these variables outside of the function. as it stands i am declaring the variables outside the function and pulling them in globally but that feels like it is defeating the object and i am repeating myself too much.

is there a better way to achive what i want with out declearing the variables outside the function?

jaggysnake
  • 75
  • 1
  • 9

4 Answers4

1

What you can do is returning an array and extract() the return value of the function.

function breakDownPOSTArray($count, $fields) {
    $repeat = 1;
    $num = 0;
    $array = array();
    while ($repeat <= $count) {
        $array["array" . $repeat] = array_slice($_POST, $num, $fields);
        $repeat++;
        $num += 4;
    }
    return $array;
}

// To call it:
extract(breakDownPOSTArray(..., ...));
bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • @ bwoebi so take my array break it down to smaller arrays put it back as 1 big array and then break it down into indervidual variables????? thats a bit pointless and not what i need – jaggysnake Apr 18 '13 at 18:46
  • @jaggysnake this is the only way to do what you want accordingly to your question. As you cannot create variables in a function/method to share in another context (≠ main scope). – bwoebi Apr 18 '13 at 18:55
0

No. Variables defined in the function are created in the stack and they ceased to exist after the function returns.

Adam Szabo
  • 11,302
  • 18
  • 64
  • 100
0

Assuming you've got a valid reason to accomplish what you're looking for...

change

$a = "array" . $repeat;
$$a = array_slice($_POST, $num, $fields);

to

$GLOBALS["array" . $repeat] = array_slice($_POST, $num, $fields);

... it feels like me to a convoluted design, but I can't see enough of your code to say that for certain.

Jason
  • 13,606
  • 2
  • 29
  • 40
  • This only works in global scope, not in functions etc. – bwoebi Apr 18 '13 at 17:51
  • That's not my experience. Additionally, that would pretty much defeat the entire purpose of the $GLOBALS superglobal. – Jason Apr 18 '13 at 17:51
  • Maybe you've misuderstood me. The variables you're declaring here are only accessible directly in the global scope; to access them from another scope, you have to use there also $GLOBALS[...], what doesn't make any sense. – bwoebi Apr 18 '13 at 17:53
  • i have a large form that needs to update several colloms and rows at the same time im break down the '$_POST' into smaller arrays with a view to make it much more manageable. this would just make the code more reusable over the site – jaggysnake Apr 18 '13 at 17:59
  • Ah, I did misunderstand you, but there's only a few ways to make variables available between different scopes, and using global scope is one of them and appeared at first glance to be what the OP was looking for, in generating these values inside a function and have them be available in, presumably, any number of other scopes. If that was an incorrect presumption, then this probably wouldn't be the optimal approach. – Jason Apr 18 '13 at 18:00
0

You could declare the variables as global, ex :

$sxml = new SimpleXMLElement('<somexml/>');

function foo(){
    global $sxml;
    $child = $sxml->addChild('child');
}

foo();

source : Can't access global variable inside function

Community
  • 1
  • 1