# Cumbersome fetching the key in an array and duplicating arrays

**URL:** <https://forum.mikrotik.com/t/cumbersome-fetching-the-key-in-an-array-and-duplicating-arrays/144373>\
**Category:** Scripting\
**Created:** [November 4, 2020, 12:57am UTC](https://forum.mikrotik.com/t/cumbersome-fetching-the-key-in-an-array-and-duplicating-arrays/144373 "2020-11-04T00:57:25Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![msatter](https://forum.mikrotik.com/letter_avatar/msatter/32/6_449c533f586939b6e5f246632a46421f.png) [@msatter](https://forum.mikrotik.com/u/msatter)\
**Post date:** [November 6, 2020, 6:46pm UTC](https://forum.mikrotik.com/t/cumbersome-fetching-the-key-in-an-array-and-duplicating-arrays/144373/3 "2020-11-06T18:46:05Z")

</div>

I had a look at 2D array. They are not easy and I stepped in a few beartraps and maybe I even need some new functions to handle them easier.

Some things are easier to archive but it is a lot to learn and test.

**Update:** I have converted it from array with a key to a 2D array. Work great and it was a few hours checking and finding out how to use it. Thanks for pointing me in the right direction.

I had one problem of appending to the 2D array but thanks to this posting that is not tackled also:

[http://forum.mikrotik.com/t/array-push-function/78069/12](http://forum.mikrotik.com/t/array-push-function/78069/12)

Example transfering specific item from one 2D to an other 2D:

```plaintext
:foreach label,v in=$arrayLabel2D do={:set $arrayDisplay2D ($arrayDisplay2D , {($arrayLabel2D->$label->1),""}) };

```

This appends a item and adds also a empty item so that later it can be used. I use :foreach to have a stop at the end of the source array and $label is then just a counter. This because there are not keys in the array.

:foreach has become my favorite script command by now, it is so flexible. The $v contains the two items in a array part separated by “;” it is just bonus that can be used in other ways like moving two item in one go, to the destination array.

```plaintext
:global arrayDisplay2D [:toarray ""]; :foreach label,v in=$arrayLabel2D do={:set $arrayDisplay2D ($arrayDisplay2D , {$v}) }

```

The Wiki could contain so much more examples and it saves users a lot of time trying and searching.

Nice to find these **gems**.

---

_[View the full topic](https://forum.mikrotik.com/t/cumbersome-fetching-the-key-in-an-array-and-duplicating-arrays/144373)._
