Return an array from get_terms and store as JavaScript array for search autocomplete

I am trying to use wp_list_pluck to return an array of taxonomy names from get_terms. I'm not sure what I'm doing wrong, but this only echo's out "Array":

$terms = get_terms(array(
        'taxonomy' = 'state',
        'hide_empty' = false,
    ));

    $term_ids = wp_list_pluck( $terms, 'name' );

    echo $term_ids;

Do you know how I can echo out an array of taxonomy names?

The reason I am doing this is because I am wanting to get an array to convert it to a Javascript array of names using json_encode.

Basically I'm using jQuery Autocomplete and would like to get my taxonomy names listed as suggestions to search for:

var availableTags = ?php echo json_encode( $term_ids ) ?

Perhaps there is possibly a better way to achieve what I am trying to do? Thank-you.

Topic advanced-taxonomy-queries jquery-ui ajax jquery autocomplete Wordpress

Category Web


Solved it simply using:

$json = array();

    $terms = get_terms( 'state' );

    foreach ( $terms as $term ) {
        $json[]=array( 'value'=> $term->name );
    }

    echo json_encode($json)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.