PHP: How to turn a string that contains an array expression in an actual array?

后端 未结 4 657
广开言路
广开言路 2021-01-25 09:04

I have an array of user inputs ($atts) as key=>value pairs. Some of the values could be written as an array expression, such as:

\'setting\' => \'array(50,25)         


        
4条回答
  •  天命终不由人
    2021-01-25 09:43

    You can use eval, but I would highly recommend not using it, and instead try to rethink your design on a better way to handle the settings. So for your example instead of storing

    'setting' => 'array(50,25)'
    

    Could you do something like

    'setting' => array('type'=>'array', 'value'=>'50, 25')
    

    then when you load the settings you can do

    switch($type)
    case 'array'
      $val = explode(', ', $value)
    

    Or something similar

    But like others suggested, I would try to save the settings using serialization

提交回复
热议问题