Page 1 of 1
use Eval to create multidimentional array
Posted: Sun Nov 21, 2010 6:19 am
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>';
Re: use Eval to create multidimentional array
Posted: Sun Nov 21, 2010 4:29 pm
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'
);
Re: use Eval to create multidimentional array
Posted: Sun Nov 21, 2010 10:42 pm
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.
Re: use Eval to create multidimentional array
Posted: Sun Nov 21, 2010 11:52 pm
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.
Re: use Eval to create multidimentional array
Posted: Mon Nov 22, 2010 12:08 am
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.
Re: use Eval to create multidimentional array
Posted: Mon Nov 22, 2010 12:19 am
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).
Re: use Eval to create multidimentional array
Posted: Mon Nov 22, 2010 12:22 am
by goyaldeepti
OK.Thanks.