Foreach varaiable as array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
troshan
Forum Newbie
Posts: 5
Joined: Wed Jun 24, 2015 11:47 pm

Foreach varaiable as array

Post 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)
Attachments
city_dropdown.jpg
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Foreach varaiable as array

Post by Celauran »

Code: Select all

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