Jump to content

Populate array from another array


Go to solution Solved by Barand,

Recommended Posts

Hi,

 

Am hoping someone can help/point me in the right direction!

 

I have the following code:

	$checkout_fields['order']['categories'] = array(
		'type'        => 'select',
		'class'         => array('my-field-class form-row-wide'),	
		'label'       => __('Food options', 'woocommerce'),
		'placeholder' => _x('', 'placeholder', 'woocommerce'),
		'required'    => false,
		'clear'       => false,
		'options'     => array(
			'eat-meat' => __('I eat maet', 'woocommerce' ),
			'not-meat' => __('Meat is gross', 'woocommerce' )
		)
	);	

However, I'd like to make the options part dynamic and therefore grab the values from elsewhere. So I then have this code:

$categories = get_field( 'categories', 'options' );
$choices = array();
if ( is_array( $categories ) ) {

	$category_str = array();
	foreach ( $categories as $category ) {
		
		#echo $category['value'];
		#echo $category['label'];
		
		$category['value'];
		$category['label'];
		$category_str[] = $category['value'].$category['label'];
	
	}
	
	$result = implode(",",$category_str);
	echo $result;
}

My issue is I'm not sure on the best way to amalgamate the two?

 

Can someone help?

Link to comment
https://forums.phpfreaks.com/topic/303014-populate-array-from-another-array/
Share on other sites

Ok, I've managed to get somewhere but struggling with one part now. So I now have:

$categories = get_field( 'categories', 'options' );
$choices = array();
if ( is_array( $categories ) ) {

	$category_str = array();
	foreach ( $categories as $category ) {
		
		$category['value'];
		$category['label'];
		$category_str[$category['value']] = $category['label'];
	
	}
	
}

	$checkout_fields['order']['categories'] = array(
		'type'        => 'select',
		'class'         => array('my-field-class form-row-wide'),	
		'label'       => __('Food options', 'woocommerce'),
		'placeholder' => _x('', 'placeholder', 'woocommerce'),
		'required'    => false,
		'clear'       => false,
		'options' 	  => array_values($category_str)	
	);	

If I use print_r($category_str); I see the following:

Array
(
    [a] => A
    [b] => B
    [c] => C
)

Yet if I view the drop down it shows:

<option value="0">A</option>
<option value="1">B</option>
<option value="2">C</option>

So how do I get the value to be the value from the first array? So it's like this:

<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>

Thanks

Hi @cyberRobot

 

It's Wordpress/WooCommerce so it's simply a function/hook:

function prg_woocommerce_checkout_fields( $checkout_fields = array() ) {

	$checkout_fields['order']['categories'] = array(
		'type'			=> 'select',
		'class'			=> array('my-field-class form-row-wide'),	
		'label'			=> __('Categories', 'woocommerce'),
		'placeholder'	=> _x('', 'placeholder', 'woocommerce'),
		'required'		=> false,
		'clear'			=> false,
		'options'		=> array_values($category_str)

	);			

	return $checkout_fields;

}

If I manually add the option:

		'options'     => array(
			'eat-meat' => __('I eat meat', 'woocommerce' ),
			'not-meat' => __('Meat is gross', 'woocommerce' )
		)

Then the drop down creates correctly:

<option value="eat-meat">I eat meat</option>
<option value="not-meat">Meat is gross</option>
						

But once I try to make it dynamic by passing values from elsewhere, it just doesn't quite display the right output

 

Does that help?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.