0

I need to convert a multiple single values into an array, basically I managed to get names from the source as a string, but it's as the following:

All single strings:


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

I would like to convert it into an array so I can merge with another set of data to generate a file. The key of the array would be exactly the same as the value, so I'm looking to get it like this:

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
)

How would I do that in PHP? I have been trying to convert string to array and repeat the value but it returns also as single ones: $array = array($names => $names);

DBG --> [array] Array
(
    [date] => date
)

DBG --> [array] Array
(
    [conversion_time] => conversion_time
)

DBG --> [array] Array
(
    [conversion_ref] => conversion_ref
)

What do I need to do to get everything aligned?

I'm a bit of a newbie with coding.

2 Answers 2

1

Just declare a variable with a string in a pair of chain bracket and you have yourself an associative array. e.g. $array["name1"]="value1"; add more values... $array["name2"]="value2";

Therefore use this to set values individually:

$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";

Or use the following if you need to set all values at once:

$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"
);

Notice the use of => instead of = and , instead of ;

Sign up to request clarification or add additional context in comments.

Comments

0

You can define both the key and the value of an element in an array using the string value of a variable, like so:

$array = [];
$string1 = 'date';
$string2 = 'time';

$array[$string1] = $string1;
$array[$string2] = $string2;

var_dump($array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.