I've got array data which outputs as:
'TextField3283' => string 'A'
'TextField3287' => string 'B'
'TextField3288' => string 'C'
'Custom_TextField3283' => string 'customfield_10202'
'Custom_TextField3287' => string 'customfield_10216'
'Custom_TextField3288' => string 'customfield_10212'
What I need to do is shuffle the key and values to create the following output:
'customfield_10202' => 'A'
'customfield_10216' => 'B'
'customfield_10212' => 'C'
So the value of the last three entries become the keys of a new array, with the correct values. As you can see, the last three entries are the same as the first three but prefixed with Custom_
With this in mind, I've attempted to use:
$customfields = array();
foreach ($data as $key => $value) {
if (preg_match("/_TextField/", $key)) {
array_push($customfields, array($value => $key));
}
}
Which outputs:
'customfield_10202' => 'Custom_TextField3283'
'customfield_10216' => 'Custom_TextField3287'
'customfield_10212' => 'Custom_TextField3288'
But now I'm not sure the best way to lookup the values as keys in the first array and return their values...
I hope this makes sense
答案 0 :(得分:1)
你的尝试非常接近。如果您的阵列保持原样,您需要做的就是:
stop
注意我们foreach ($array as $key => $value) {
if (preg_match("/_TextField/", $key)) {
list($unused, $findkey) = explode("_", $key);
array_push($customfields, array($value => $array[$findkey]));
}
}
上的explode()
怎么样?这是因为该值的键被设置为数组索引。