1

This is an example of my original code which seems to work:

$MainArray =  array('Part1'=>array(Foo, 4, 2, Cat), 'Part2'=>array(Bar, 3, 1, Dog));
print_r ($MainArray);

Returns:

Array ( [Part1] => Array ( [0] => Foo [1] => 4 [2] => 2 [3] => Cat ) [Part2] => Array ( [0] => Bar [1] => 3 [2] => 1 [3] => Dog ) ) 

What I would like to do is something like this (the pieces are included based on if statements):

$ArrayPieces.="'Part1'=>array(Foo, 4, 2, Cat), ";
$ArrayPieces.="'Part2'=>array(Bar, 3, 1, Dog)";
$MainArray =  array($ArrayPieces);
print_r ($MainArray);

But this returns:

Array ( [0] => 'Part1'=>array(Foo, 4, 2, Cat), 'Part2'=>array(Bar, 3, 1, Dog))

Any suggestions on how to make this work? Thanks!

Rob
  • 50
  • 1
  • 1
  • 10

3 Answers3

1

The only way to do this (as you want) is to use eval():

<?php

$MainArray =  array('Part1'=>array('Foo', 4, 2, 'Cat'), 'Part2'=>array('Ba$
print_r ($MainArray);

$ArrayPieces = "return array(";
$ArrayPieces .= "'Part1'=>array('Foo', 4, 2, 'Cat'), ";
$ArrayPieces .= "'Part2'=>array('Bar', 3, 1, 'Dog')";
$ArrayPieces .= ");";

$MainArray =  eval($ArrayPieces);

print_r ($MainArray);

?>

As for why there is a return in the array string, this is because eval only accepts statements, not expressions.

The above code will give you what you want:

Array
(
    [Part1] => Array
        (
            [0] => Foo
            [1] => 4
            [2] => 2
            [3] => Cat
        )

    [Part2] => Array
        (
            [0] => Bar
            [1] => 3
            [2] => 1
            [3] => Dog
        )

)

However, this is generally not recommended. At all! It is very unsafe, (especially when evaluating user input - which you should avoid doing under most, if not ALL circumstances) and in certain situations, it is more trouble than it's worth.

For more on this matter, refer to this:

When is eval evil in php?

Community
  • 1
  • 1
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • This worked perfectly. Thanks for the extra info on the "evil eval". All my inputs are fixed by me, so I think I'm ok there, but I will consider taking more time to approach from a different direction. Thanks! – Rob Mar 11 '13 at 04:39
0

Given that your question is based on a pretend situation, it's hard to know exactly what you're looking for, but I think something like this should achieve what you're after:

$MainArry = array();
if (foo)
    $MainArray['Part1'] = array(Foo, 4, 2, Cat);
if (bar)
    $MainArray['Part2'] = array(Bar, 3, 1, Dog);
Mark Parnell
  • 9,175
  • 9
  • 31
  • 36
0

It sounds like your trying to deserialize an array. According to your example, you're looking to construct PHP syntax in a string and then execute it later.

If this is what you absolutely must do, look at the eval PHP function

Personally, I don't like "eval".

Is there an opportunity to use JavaScript Object Notation? If so, express your above example as:

$json_pieces = '{';
$json_pieces .= '"Part1": ["Foo", 4, 2, "Cat"],';
$json_pieces .= '"Part2": ["Bar", 3, 1, "Dog"]';
$json_pieces .= '}'
$MainArray = json_decode( $json_pieces, TRUE );
print_r( $main_array );
J.D. Pace
  • 586
  • 1
  • 3
  • 17
  • Thanks for this alternative- I'll check into this more. – Rob Mar 11 '13 at 14:20
  • Let me know what you decide. I'm also interested to know more about what you're doing and how you arrived at the need to do piecewise code generation. – J.D. Pace Mar 11 '13 at 14:29