The main reason your second attempt does not work is because you are trying to solve another problem using the same solution.
In the first dataset, you have two numerical indexed arrays, where the keys have no meaning except perhaps the order in which they appear, and their values are really important. I interpreted this as meaning that you want the linear concatenation of these values to a new array with a new index that discards the previous keys but retains the original order of the elements, as well as the order in which you passed them.
In the second dataset, you have two associative indexed arrays where the keys are values and the values are really just placeholders. I noticed that you used numeric keys, which, if you want to use numeric indexed arrays, will allow you to keep both the order of the values and the order of the keys, assuming that you want the keys in ascending order ...
So, to solve these problems, I have 3 convenient functions that I wrote that use declare and eval to speed up the merging / merging of large arrays, rather than using loops to assign each of them. They also take a variable number of arrays as an argument, so you can join / merge / drop as many as you want.
NOTE. I changed the value / key “30” to “30 30” to demonstrate how the string will behave differently than the number in some cases.
join_arrays(){
declare -a join = '([0] = "5" [1] = "10" [2] = "15" [3] = "20" [4] = "25" [5] = "30 30" ) '
# this does exactly the same thing, mostly saves me from typos ;-) echo "joining array1+array2 using join_array():"; join_arrays array1 array2 joined; declare -p joined;
declare -a join = '([0] = "5" [1] = "10" [2] = "15" [3] = "20" [4] = "25" [5] = "30 30" ) '
# this merges them by key, which is inapropriate for this data set
declare -A merged = '([0] = "20" [1] = "25" [2] = "30 30")'
# Example of joining 2 associative arrays:
declare -a join = '([0] = "true" [1] = "true" [2] = "true" [3] = "true" [4] = "true" [5] = "true")
declare -a join = '([0] = "true" [1] = "true" [2] = "true" [3] = "true" [4] = "true" [5] = "true")
# NOW a merge is appropriate, because we want the keys! echo "merging array3+array4 using merge_array():" merge_arrays array3 array4 merged; declare -p merged;
declare -A merged = '([25] = "true" [20] = "true" ["30 30"] = "true" [10] = "true" [15] = "true" [5] = " true ") '
# Bonus points - another easy way to merge arrays (assoc or indexed) by key
bash: 30 30: syntax error in expression (error token is "30")
declare -p joined
declare -a join = '([0] = "5" [1] = "10" [2] = "15" [3] = "20" [4] = "25" [5] = "30 30" [20] = "true" [25] = "true") '
# Note: assoc arrays will not be sorted, even if keys are numeric! join_arrays array1 array2 joined; eval merged+=(`dump_arrays joined`); declare -p merged
declare -A merged = '([25] = "true" [20] = "true" ["30 30"] = "true" [10] = "true" [15] = "true" [0] = " 5 "[1] =" 10 "[2] =" 15 "[3] =" 20 "[4] =" 25 "[5] =" true30 30 ") '
Final note: above, you can see that the [5] key has the values of the two keys of the original [5] arrays associated, because I used the + = operator. If you just use it to merge flag lists, it is safe, but to merge lists of significant values with possible key conflicts, it is better to stick with the merge_array () function.