Page 1 of 1

Foreach varaiable as array

Posted: Thu Jun 25, 2015 12:14 am
by troshan
Following code generate a select dropdown field

$fields['shipping']['shipping_city'] = array(
'label' => __('City', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'clear' => true,
'type' => 'select',
'class' => array('own-css-name'),
'options' => array(
'New_York_City' => __('New York City', 'woocommerce' ),
'Chicago' => __('Chicago', 'woocommerce' ),
'Dallas' => __('Dallas', 'woocommerce' )
)
);

I want to get the "option =>" values from category array. I tried the following
$categories = get_categories( $args );
foreach ($categories as $category) {
$cityArray[] = "'".$category->slug."' => __('".$category->name."', 'woocommerce' )";
}
$fields['shipping']['shipping_city2'] = array(
'label' => __('City', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'clear' => true,
'type' => 'select',
'class' => array('own-css-name'),
'options' => array(implode( ', ', $cityArray ) )
);
but It return the following (screenshot attached)

Re: Foreach varaiable as array

Posted: Thu Jun 25, 2015 6:03 am
by Celauran

Code: Select all

foreach ($categories as $category) {
    $cityArray[$category->slug] = __($category->name, 'woocommerce');
}