$cfields = array_filter(explode(",", get_option('CComment_fields')));
$Ctlds = array_filter(explode(",", get_option('Ctlds')));
Sorry, I know this is a mess. Basically I end up with two arrays: $cVal
& $Ctlds
The arrays:
$Cval = Array ( [0] => Fun Co [1] => 555-555-5555 [2] => test@test.com [3] => Tom [4] => Smith )
$Ctlds = Array ( [0] => user_company [1] => user_phone [2] => user_email [3] => user_firstname [4] => user_lastname )
I want the matching [NUMBER]
value in $Ctlds
to be the variable with the matching [NUMBER]
in $Cval
to be the value.
Example: user_company will be assigned the value Fun Co user_phone will be assigned the value 555-555-5555 etc..
This is the code I tried - bad, I know.
foreach ($Ctlds as $Ctlds){
$cVal[] = $Ctlds[];
}
I have tried code I know is waaaayy off. Any help would be appreciated. And for search reference this is USING TO ARRAYS TO CREATE VARIABLE (KEYS) AND VALUES IN PHP. Thank you so much.
Use array_combine
. That's exactly what it's for.
$result = array_combine($Ctlds, $Cval);
If you want them to be variables, you can then just extract
that array.
I'm not sure of what you want, but you should try this :
$result = array();
foreach ($Ctlds as $key => $Ctld) {
$result[$Ctld] = $Cval[$key];
}
You will get an array like :
Array (
[user_company] => 'Fun Co',
[user_phone] => '555-555-5555',
[user_email] => 'test@test.com',
[user_firstname] => 'Tom',
[user_lastname] => 'Smith'
)
Try delimiting your arrays with commas and escaping your strings. Ex:
Ex:
$Cval = array (
[0] => "Fun Co",
[1] => "555-555-5555",
[2] => "test@test.com",
[3] => "Tom",
[4] => "Smith" );
This code:
foreach ($Ctlds as $key => $Ctld) {
$$Ctld = $Cval[$key];
}
will result in :
$user_company = "Fun Co";
$user_phone = "555-555-5555";
etc