如何将数组存储为字符串php?

时间:2022-04-14 15:41:24

I have a javascript that formats an HTML table that takes some parameters to configure it. I also have a reports table in mysql that contains definitions of my reports: a field for the report name, description, sql, footer, permissions, report status and some report specific parameters. I build up the parameters as an array so that I can set defaults that can then be overwritten by other processes before being passed to a process that turns that array into the javascript parameters.

我有一个javascript,它格式化一个HTML表,它需要一些参数来配置它。我还在mysql中有一个报告表,其中包含了我的报告的定义:报告名称、描述、sql、页脚、权限、报告状态和一些报告特定参数的字段。我将参数设置为一个数组,这样我就可以设置默认值,然后在传递到将该数组转换为javascript参数的进程之前,可以重写其他进程。

We use PHP to build reports based on the information in the table and it merges an array of "default parameters" with an array of "report specific parameters" . Before we put the report definitions into a database there was a function for each report that used array_merge that merged the default parameters array and the report specific parameter array.

我们使用PHP根据表中的信息构建报告,并将“默认参数”数组与“报告特定参数”数组合并。在将报表定义放入数据库之前,对于使用array_merge的每个报表都有一个函数,该函数合并了默认参数数组和报告特定的参数数组。

eg

$defaultparameters = array (
                    'base_path' => "'/inc/plugin/'",
                    'btn_reset' => "'true'",
                    'enable_default_theme' => "'true'");
$thisReportParameters  = array (
                    'extensions' => "[{ name: 'sort', types: ['String', 'String', 'String','String', 'String','String', 'Number']},'col_2'=> "'multiple'",'col_3'=> "'select'");
$parameters = array_merge ($defaultparameters,$thisReportParameters);

This array is then passed to a function that builds the javascript for the page. Now we've put all the reports into a table I'm having trouble pulling out the report specific parameters into an associative array. The default parameters are now set in the report class as a property of the report object. I want to pull the additional report specific parameters from the database into an array to combine the two. When I added 'col_2'=> "'multiple'",'col_3'=> "'select'" to the database and brought it back as an array it put the whole string in as element [0] in the array.
ie

然后将这个数组传递给一个函数,该函数为页面构建javascript。现在我们把所有的报告都放到一个表中,我很难把报告的具体参数提取到关联数组中。现在,报告类中的默认参数设置为报表对象的属性。我想把额外的报告特定参数从数据库中提取到一个数组中,以组合两者。当我添加'col_2'=> ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ',' ' ' ' ' ' [0] ' ' ' '即

Array ([0] => "'col_2'=> "'multiple'"'col_3'=> "'select'"") 

as opposed to

而不是

Array([col2] =>"'multiple'",[col_3]=> "'select'")

I considered just adding the string from the DB to the end of the javascript configuration but this would not overwrite any settings that were already defined in the default settings array so I need to add it to an array first so that when it merges it overwrites the any defaults where the keys are the same.

我认为刚从DB添加字符串的末尾javascript配置但这不会覆盖任何设置,已经默认设置中定义数组所以我需要先将其添加到一个数组,当合并它覆盖的任何违约密钥是相同的。

The report specific options are many and varied in their format so I can't feasibly create a field for each.

报告的具体选项很多,格式也各不相同,因此我无法为每个选项创建一个字段。

I thought about exploding the string on , then parsing the pairs to build the array but the value can have "," in it as well so that might mess up the exploding.

我考虑过将字符串炸开,然后解析成对来构建数组,但是值也可以有“,”,这样就会把爆炸弄得一团糟。

There's probably a really neat way of achieving this but at the moment it escapes me...

也许有一种很巧妙的方法来达到这个目的,但现在我想不起来了……

Any suggestions welcome

有什么建议欢迎

C

C

1 个解决方案

#1


0  

You could serialize your arrays.

您可以序列化您的数组。

There's also an option to encode them in JSON which may be better if your DB is postgres - it has some built-in support for things like that and it would be easier to work with your data using some other language if need ever arises.

还有一个可以用JSON编码的选项,如果你的DB是postgres,它可能会更好——它对类似的东西有一些内置的支持,如果需要的话,使用其他语言处理你的数据会更容易。

In any case, be sure to properly sanitize your data before trying to put it into the DB.

无论如何,在尝试将数据放入数据库之前,一定要对数据进行适当的消毒。

#1


0  

You could serialize your arrays.

您可以序列化您的数组。

There's also an option to encode them in JSON which may be better if your DB is postgres - it has some built-in support for things like that and it would be easier to work with your data using some other language if need ever arises.

还有一个可以用JSON编码的选项,如果你的DB是postgres,它可能会更好——它对类似的东西有一些内置的支持,如果需要的话,使用其他语言处理你的数据会更容易。

In any case, be sure to properly sanitize your data before trying to put it into the DB.

无论如何,在尝试将数据放入数据库之前,一定要对数据进行适当的消毒。