Title: Making an array
Last modified: September 21, 2024

---

# Making an array

 *  [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/)
 * I need to make a code similar to this one: [https://pastebin.com/pCRG7vze](https://pastebin.com/pCRG7vze)
 * But the elements in the array come from custom taxonomies, each element of the
   taxonomy has already its custom field for german and english, so that I dont 
   need to gettext them
 * each taxonomy element is actually done this way: [https://pastebin.com/uHRFk3XR](https://pastebin.com/uHRFk3XR)
 * How can I “mix” the 2 codes?
 * The purpose is having each term separated by a comma, but the final element has
   a dot
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fmaking-an-array%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18028875)
 * Move the term code to somewhere above the “variante” code. Insert the following
   where ever you want the term name to appear in the variante list (somewhere above
   the implode() line):
    `$output_distanze[] = $term_name17_str;`
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18029169)
 * I’m doing something wrong because I’m getting as output only the last term ($
   term_name18_str) and both with a comma and a dot: [https://pastebin.com/G5hkNYfF](https://pastebin.com/G5hkNYfF)
 * I’m trying to concatenate 2 terms: $term_name17_str and $term_name18_str , after
   the first, a comma, after the second and last one, a dot.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18029310)
 * Probably I need just `$term_name17` and not `$term_name17_str` because the comma
   is written by the “implode”, but I still need to correct something
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18029698)
 * Yes, use `$term_name17`. With implode(), every element in the passed array is
   separated by the first arg, `', '` in this case. Then the terminal dot is concatenated
   to the imploded string.
 * If you’re only getting one value as a result but with an extra comma, the other
   value is blank or an empty string. In the case of missing or empty values, you
   should not add them to the array at all. Maybe do something similar to the following
   for every array element:
    `if ( ! empty( $term_name17 )) $output_piano_sup[] 
   = $term_name17;`
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18030253)
 * I have 2 problem: the first one is little, I have a blank space before the final
   dot in the array output: [https://test.sacconicase.com/forte-dei-marmi-villa-con-giardino-disposta-su-2-piani-per-5-persone/](https://test.sacconicase.com/forte-dei-marmi-villa-con-giardino-disposta-su-2-piani-per-5-persone/)
 * If you see” Al 1° piano” (at the first floor), in italian there are 3 elements
   of the array. The second problem is that in the other languages I have just 2
   elements in the array, even if I have correcty created the specific meta fields
   for english and german, maybe can you inspect the code for the field in enlglish?
   [https://pastebin.com/dc14gNzE](https://pastebin.com/dc14gNzE)
 * it is connected with this
 *     ```wp-block-code
       //bagno sup$lang = substr( get_locale(), 0, 2 );$terms = get_the_terms( get_the_ID(), 'bagno_sup');if ( is_array( $terms )) {	$term = $terms[0];		// get the zeroth element in array, a WP_Term object	if ( $lang == 'it' ){$term_name19 = $term->name . ' ' ;			} else {$term_name = get_term_meta( $term->term_id , 'bagno_sup_'.$lang, true ). ' '  ;			} } else {   $term_name19 = '';}//fine bagno sup
       ```
   
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18030422)
 * Another thing about the implode: when there isnt any term I should have a dot
   as output
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18031651)
 * The reason German and English term meta are not appearing is because you have
   inconsistent variable names in your code. You use `$term_name19` in a number 
   of places but `$term_name` for non-Italian term meta. It too should be `$term_name19`.
 * The reason a space is appearing after phrases is because you’re concatenating
   the space in your code. For example: `$term_name19 = $term->name . ' ' ;`
    You
   don’t need the `. ' '` part.
 * Let implode() handle what occurs between phrases. Let what happens at the end
   be handled after the implode() call. For example:
    `echo implode(', ', $output_distanze).'.';`
 * I’m unclear about when you want just a dot to appear. When any single term is
   missing, or when there are no terms in the entire array to implode? For any single
   missing term you could do something like:
    `$output_piano_sup[] = empty( $term_name17)?'.':
   $term_name17;` This could result in output similar to:
 * > Al 1° piano: ., ., 1 bagno con vasca.
 * Seems odd to me. If this is not your intention, then what sort of output do you
   intend?
    -  This reply was modified 1 year, 8 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
      Reason: code formatting
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18031916)
 * First 2 problems have been solutioned.
 * About the implode() call: If there is at least one term my code is working properly,
   the problem arises when none of the terms are used. Because the dot at the end
   always appears.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18033986)
 * Ah, I see what you mean now! Do something like this:
    `if ( ! empty( $output_distanze))
   echo implode(', ', $output_distanze ).'. ';`
    -  This reply was modified 1 year, 8 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
      Reason: code format fixed
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18034032)
 * At the moment it’s not working, I have tryed a lot of positions but maybe the
   code isnt in the good one: [https://pastebin.com/g4NYgzqP](https://pastebin.com/g4NYgzqP)
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18036635)
 * My earlier suggestion was generic and did not reflect your specific context. 
   Specifically what I suggest is you remove this part of the `return` statement:`
   implode(', ', $output_piano_sup ).'.' . ' '`
    and move it to above the `return`
   statement. Replace where it was with a unique variable not used elsewhere, maybe`
   $piano_sup` perhaps?
 * Then with the implode() code that’s now above the `return` statement, alter it
   to be like this:
    `$piano_sup = empty( $output_distanze )) ? '' : implode(', ',
   $output_distanze ).'. ';`

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Making an array’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 11 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [1 year, 8 months ago](https://wordpress.org/support/topic/making-an-array/#post-18036635)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
