Reputation: 5023
I am extracting variables from an array and works fine , but just happens that in same file I have news items from different category with array that has same key names so they get mixed up and first set loaded takes over, what I need is a prefix or suffix for the variable so I can differentiate one from another
function get_extra_fields($item){
$item->extra_fields = K2ModelItem::getItemExtraFields($item->extra_fields);
foreach ( $item->extra_fields as $key => $extraField ){
$getkey = strtolower($extraField->name);
$getkey = str_replace(' ', '', $getkey);
global $$getkey;
$$getkey = $extraField->value;
}
}
and it is called within foreach for 3 different categories
foreach($get_gcat1 as $row => $item){
get_extra_fields($item);
echo $newstitle;
}
foreach($get_cat2 as $row => $item){
get_extra_fields($item);
echo $newstitle;
}
foreach($get_cat3 as $row => $item){
get_extra_fields($item);
echo $newstitle;
}
any help is appreciated. thank you!
Upvotes: 0
Views: 462
Reputation: 5552
Strange way for creating global variables, but here is something that may do what you want:
function get_extra_fields($item, $prefix){
$item->extra_fields = K2ModelItem::getItemExtraFields($item->extra_fields);
foreach ( $item->extra_fields as $key => $extraField ){
$getkey = strtolower($extraField->name);
$getkey = str_replace(' ', '', $getkey);
$GLOBALS[$prefix.$getkey] = $extraField->value;
}
}
foreach($get_gcat1 as $row => $item){
get_extra_fields($item, 'cat1_');
echo $cat1_newstitle;
}
foreach($get_cat2 as $row => $item){
get_extra_fields($item, 'cat2_');
echo $cat2_newstitle;
}
foreach($get_cat3 as $row => $item){
get_extra_fields($item, 'cat3_');
echo $cat3_newstitle;
}
Upvotes: 1