我需要将多个单值转换为数组,基本上我设法从源文件中获得字符串形式的名称,但它如下所示:
所有单个字符串:
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
)我需要做些什么才能让所有的事情都对齐?
我是个编程新手。
发布于 2020-05-28 04:15:12
只需在一对链括号中声明一个带有字符串的变量,您就拥有了一个关联数组。例如,$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"
);请注意,使用=> = 而不是=,而不是使用
发布于 2020-05-28 03:51:37
您可以使用变量的字符串值来定义数组中元素的键和值,如下所示:
$array = [];
$string1 = 'date';
$string2 = 'time';
$array[$string1] = $string1;
$array[$string2] = $string2;
var_dump($array);https://stackoverflow.com/questions/62051457
复制相似问题