use Eval to create multidimentional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
goyaldeepti
Forum Newbie
Posts: 5
Joined: Sun Nov 21, 2010 6:13 am

use Eval to create multidimentional array

Post by goyaldeepti »

Please help me out in this code :( :( . it is giving me
Fatal error: Cannot use string offset as an array

Code: Select all

        $abc = array(
                     '2' => '2-guest',
                     '5' => '5-a',
                     '6' => '6-b',
                     '2:3' => '3-member',
                     '2:8' => '8-d',
                     '2:9' => '9-e',
                     '5:7' => '7-c',
                     '2:3:1' => '1-admin'
                );
        foreach ($abc as $pKey => $pValue){
           $exp = explode(":", $pKey);
           $x = "\$out['".implode("']['",$exp)."'] = '$pValue';";
           eval($x);
        }
        echo '<pre>';
        print_r($out);
        echo '</pre>';
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: use Eval to create multidimentional array

Post by McInfo »

This topic belongs in PHP Code.

Problem:

PHP allows the use of array syntax to reference characters in a string. If $out[2] holds the string 'ABCDE', $out[2][3] will reference the character at offset 3, which is 'D'. Characters do not have offsets, so $out[2][3][1] (offset 1 of 'D') does not make sense.

Code: Select all

// Evaled Statement              // Result
$out['2'] = '2-guest';           // $out[2] == '2-guest'
$out['5'] = '5-a';               // $out[5] == '5-a'
$out['6'] = '6-b';               // $out[6] == '6-b'
$out['2']['3'] = '3-member';     // $out[2] == '2-g3est'
$out['2']['8'] = '8-d';          // $out[2] == '2-g3est 8'
$out['2']['9'] = '9-e';          // $out[2] == '2-g3est 89'
$out['5']['7'] = '7-c';          // $out[5] == '5-a    7'
$out['2']['3']['1'] = '1-admin'; // Fatal error
// $out[2][3] is a character ('3') and cannot have an offset 1
Solution:

Enforce a uniform depth of array elements.

Code: Select all

$abc = array (
    '2:0:0' => '2-guest',
    '5:0:0' => '5-a',
    '6:0:0' => '6-b',
    '2:3:0' => '3-member',
    '2:8:0' => '8-d',
    '2:9:0' => '9-e',
    '5:7:0' => '7-c',
    '2:3:1' => '1-admin'
);
goyaldeepti
Forum Newbie
Posts: 5
Joined: Sun Nov 21, 2010 6:13 am

Re: use Eval to create multidimentional array

Post by goyaldeepti »

Thanks for reply.

I understood the problem. Isn't there any other solution to this problem. as I am getting this '$abc' array dynamically. so enforce it to have uniform depth is a bit more complex.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: use Eval to create multidimentional array

Post by McInfo »

"Garbage in, garbage out." You can (1) reject user input that does not conform, (2) attempt to make it conform and risk having the application do something that the user does not expect, or (3) blindly process any input and hope nothing breaks. In some situations, the second option makes sense, but I hope you would usually choose the first. You may agree that the first option is a smaller burden than the second and certainly less risky than the third.

Computers don't like ambiguity. If you don't enforce uniform depth, what does the input mean?

Testing for uniformity is actually not too difficult. Loop though the input array and make sure the number of colons in each key is consistent. At the same time, you should probably test that the characters between the colons are safe to run though eval() (to prevent eval() injection). Perhaps limit them to digits of a reasonable length. Definitely reject square brackets and semicolons. Functions like preg_match() can be helpful.
goyaldeepti
Forum Newbie
Posts: 5
Joined: Sun Nov 21, 2010 6:13 am

Re: use Eval to create multidimentional array

Post by goyaldeepti »

Hey thanks a lot. I will go for uniformity. This approach sounds good.

Actually I am a newbie to php . I am unable to understand "Garbage in, garbage out.". If you have time could you tell me in brief what does this actually mean.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: use Eval to create multidimentional array

Post by McInfo »

Wikipedia wrote:Garbage In, Garbage Out is a phrase in the field of computer science or information and communication technology. It is used primarily to call attention to the fact that computers will unquestioningly process the most nonsensical of input data (garbage in) and produce nonsensical output (garbage out).
goyaldeepti
Forum Newbie
Posts: 5
Joined: Sun Nov 21, 2010 6:13 am

Re: use Eval to create multidimentional array

Post by goyaldeepti »

OK.Thanks.
Post Reply