php - 将单个字符串转换为单个数组组
问题描述
我需要将多个单个值转换为一个数组,基本上我设法从源中获取名称作为字符串,但如下所示:
所有单个字符串:</p>
DBG --> [string] date
DBG --> [string] conversion_time
DBG --> [string] conversion_ref
DBG --> [string] cookie_id
DBG --> [string] customer_id
DBG --> [string] browser
DBG --> [string] operating_system
DBG --> [string] site_search_string
DBG --> [string] page_url
DBG --> [string] store_viewed
DBG --> [string] store_search_string
DBG --> [string] product_id
DBG --> [string] category_id
DBG --> [string] basket_product_ids
我想把它转换成一个数组,这样我就可以与另一组数据合并生成一个文件。数组的键将与值完全相同,所以我希望像这样得到它:
DBG --> [array] Array
(
[date] => date
[conversion_time] => conversion_time
[conversion_ref] => conversion_ref
[cookie_id] => cookie_id
[customer_id] => customer_id
[browser] => browser
[operating_system] => operating_system
[site_search_string] => site_search_string
[page_url] => page_url
[store_viewed] => store_viewed
[store_search_string] => store_search_string
[product_id] => product_id
[category_id] => category_id
[basket_product_ids] => basket_product_ids
)
我将如何在 PHP 中做到这一点?我一直在尝试将字符串转换为数组并重复该值,但它也作为单个值返回:$array = array($names => $names);
DBG --> [array] Array
(
[date] => date
)
DBG --> [array] Array
(
[conversion_time] => conversion_time
)
DBG --> [array] Array
(
[conversion_ref] => conversion_ref
)
我需要做什么才能使所有内容对齐?
我是一个编码新手。
解决方案
只需在一对链括号中声明一个带有字符串的变量,您就拥有了一个关联数组。例如$array["name1"]="value1";
添加更多值...$array["name2"]="value2";
因此使用它来单独设置值:
$DBG["date_key"] = "date_value";
$DBG["conversion_time_key"] = "conversion_time_value";
$DBG["conversion_ref_key"] = "conversion_ref_value";
$DBG["cookie_id_key"] = "cookie_id_value";
$DBG["customer_id_key"] = "customer_id_value";
$DBG["browser_key"] = "browser_value";
$DBG["operating_system_key"] = "operating_system_value";
$DBG["site_search_string_key"] = "site_search_string_value";
$DBG["page_url_key"] = "page_url_value";
$DBG["store_viewed_key"] = "store_viewed_value";
$DBG["store_search_string_key"] = "store_search_string_value";
$DBG["product_id_key"] = "product_id_value";
$DBG["category_id_key"] = "category_id_value";
$DBG["basket_product_ids_key"] = "basket_product_ids_value";
或者如果您需要一次设置所有值,请使用以下内容:
$DBG=array(
["date_key"] => "date_value",
["conversion_time_key"] => "conversion_time_value",
["conversion_ref_key"] => "conversion_ref_value",
["cookie_id_key"] => "cookie_id_value",
["customer_id_key"] => "customer_id_value",
["browser_key"] => "browser_value",
["operating_system_key"] => "operating_system_value",
["site_search_string_key"] => "site_search_string_value",
["page_url_key"] => "page_url_value",
["store_viewed_key"] => "store_viewed_value",
["store_search_string_key"] => "store_search_string_value",
["product_id_key"] => "product_id_value",
["category_id_key"] => "category_id_value",
["basket_product_ids_key"] => "basket_product_ids_value"
);
注意使用=>
代替=
和,
代替;
推荐阅读
- reactjs - TypeError: Object(...) is not a function when using react-toastify
- c# - 在 SAP HANA Studio 中重命名架构
- c++ - 对于黑客等级中的“两个字符串”问题,我在三个测试用例中遇到分段错误。是什么原因?
- c++ - 为什么我的 while 函数没有终止?
- javascript - 来自 .env 项的链接在 Vue.js 中被销毁
- angular - Cytoscape.js 并在订阅中加载数据
- c# - 更新 c# 项目安装程序
- python - TypeError:“ReadFile”Op 的输入“文件名”的类型为 float32,与预期的字符串类型不匹配
- javascript - Babel 没有在 Expo 中转换 TypeScript
- typescript - webgl 项目中的打字稿配置。意外的标记:在 typescript lint 错误中标记类型时