1

I've been trying to work with two for-loops to iterate through a multidimensional array and to assign the values to individual variables. However, it doesn't work, because it seems that I can't get the variables assigned to the data.

    $onepage = array
  (
  array("navbar",22,28,23,25), 
  array("presentation",15,13,23,23), 
  array("content",5,2,13,10), 
  array("footer",17,15,23,26) 
  );

      function presetter($selection) { //selection = onepage
          for($n=0;$n<=3;$n++){
              for($i=1;$i<=4;$i++){

                        $rwcl =  $selection[$n][0];
                        $numb = $i -1;
                        ${$rwcl . $numb} = $selection[$n][$i]; //outputs navbar1 
                        $l = $rwcl . $numb;
                        $$l = $selection[$n][$i];

                        echo ${$rwcl . $numb}; //outputs variable navbarx


                        echo $rwcl;//output column name

                        echo $selection[$n][$i];
                        echo "<br>";
              }
          }

      } //<-- works!

      presetter($onepage);
echo $navbar1;

The output is always Type 8 - undefined variable. I've tried several options, also the proposed solution here: PHP: Create Unique Variables In A For Loop, but without a positive result.

Probably some will say that what I do is not the sense of arrays. And yes, you are right. But would like solve it.

Maybe somebody knows how to get it done. Thanks in advance.

0

2 Answers 2

0

You are creating statics variables inside you function. When the function is done, the variables will be useless.

If you want to use them outside your function, you should create global variables or make this function return an output with all variables created (maybe an array?).

Here is a example of a global variable inside a function:

<?php
    function printIt() {
        global $x;
        $x = array(1,2,3);
    }

    printIt();
    var_dump($x);

You can read more about it here

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

2 Comments

It works, Clyff. Thank you very much. As it should. I'd like to upvote your answer, but unfortunately my account is too young. So thanks again.
No worries, I'm glad to help :)
0

you are creating variables inside a function that is why there are not accessible outside a function. You need to create global variables to access them. Here is the code

$onepage = array
(
array("navbar", 22, 28, 23, 25),
array("presentation", 15, 13, 23, 23),
array("content", 5, 2, 13, 10),
array("footer", 17, 15, 23, 26));

function presetter($selection) { //selection = onepage
for ($n = 0; $n <= 3; $n++) {
    for ($i = 1; $i <= 4; $i++) {

        $rwcl = $selection[$n][0];
        $numb = $i - 1;
        $var = $rwcl . $numb;
        global $$var;
        $$var = $selection[$n][$i]; //outputs navbar1 
    }
}}
presetter($onepage);echo $navbar1;

1 Comment

Like Clyff's answer, works well. Thank you, Kittu. :-)

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.