Arrays: help with creating, iterating, and displaying individual values

Hello everybody! I'm trying to create a macro where I would need to work with arrays.

In PHP (just for example) I can do it like this:

Code:
<?php
$contacts = array('name' => ['Maxmiler', 'Freitas'], 'phone' => ['21 91234-5678', '21 94321-8765']);
print $contacts['name'][0]; // this would return me the value 'Maxmiler'
?>

I would like to do something similar in macrodroid.

My actions: Set 'name' variable -> User Prompt Set variable 'phone' -> User Prompt Populate the 'contacts_data' array with the value of these variables.

Iterate through the 'contacts_data' array and display all values formatted as follows: Name: Maxmiler, Phone: 21 91234-5678

Is there any way to do this in macrodroid?

Can you help me?

Thank you very much in advance.
🇧🇷
 

RSF

Well-known member
Arrays and dictionaries (like arrays, but with non-integer indices) are new in MacroDroid. Multi-dimensional versions can be a bit tricky (and a bit laborious, especially with a variable number of elements).
See the attached macro for how you could set up a contacts dictionary containing name and phone arrays, each with two elements.
Screenshot 2022-11-25 9.27.05 AM.png

But to create an array of contacts, each element of which is a dictionary containing a name array and a phone array, would be trickier. You'd need iteration loops which can't be nested, I don't believe, so you'd have to simulate that.
 

Attachments

  • Multi-dimensional_dict+array.macro
    8.8 KB · Views: 32
Arrays and dictionaries (like arrays, but with non-integer indices) are new in MacroDroid. Multi-dimensional versions can be a bit tricky (and a bit laborious, especially with a variable number of elements).
See the attached macro for how you could set up a contacts dictionary containing name and phone arrays, each with two elements.
View attachment 3993

But to create an array of contacts, each element of which is a dictionary containing a name array and a phone array, would be trickier. You'd need iteration loops which can't be nested, I don't believe, so you'd have to simulate that.

Hello RSF! Thanks for your feedback.

The way you did it overwrites whenever I add new names and numbers, that is, it only saves the new ones. I know how to work with arrays, but I don't know how to work with arrays in macrodroid.

Another point is about the iteration of the arrays. I need to return the values like this:
contact[name][0], contact[phone][0], contact[name][1], contact[phone][1] and so on.
 
Last edited:

RSF

Well-known member
Yes, understood (re: overwriting), that was an illustration only of how to define an array.

I recommend against trying to create a three-dimensional array structure (array of contacts, each of which is a dictionary, each entry of which (name and phone) is an array)... MacroDroid doesn't support arrays quite as natively as Javascript, PHP, or other languages.

Instead, try the attached, in which contacts is a 2-dimensional array. It's simpler to work with in MacroDroid. Each array entry (each contact) is a dictionary containing first_name, last_name, phone1, phone2, phone3, instead of having a name array and phone array.

Note that to set the value in a contact entry, for example the
Screenshot 2022-11-26 10.00.52 AM.png
line, the steps are:
  1. Add a Set Variable action
  2. Choose the contacts variable to set
  3. Choose the Add array index option. This is a bit counter-intuitive, as it feels like it'll add a new contact when really you're just adding the last name to the existing contact, but it's the only way I found to get it to work.
  4. For the array index value, use the [...] button to choose the num_contacts local variable. It'll display as {lv=num_contacts}
  5. Choose the Add key option
  6. Type in the key name; in this case last_name, and choose the string type
  7. Choose the User Prompt option
  8. Fill in the prompt options
 

Attachments

  • 2-d_contacts_array_of_dictionaries.macro
    16.5 KB · Views: 24
Yes, understood (re: overwriting), that was an illustration only of how to define an array.

I recommend against trying to create a three-dimensional array structure (array of contacts, each of which is a dictionary, each entry of which (name and phone) is an array)... MacroDroid doesn't support arrays quite as natively as Javascript, PHP, or other languages.

Instead, try the attached, in which contacts is a 2-dimensional array. It's simpler to work with in MacroDroid. Each array entry (each contact) is a dictionary containing first_name, last_name, phone1, phone2, phone3, instead of having a name array and phone array.

Note that to set the value in a contact entry, for example the
View attachment 3995
line, the steps are:
  1. Add a Set Variable action
  2. Choose the contacts variable to set
  3. Choose the Add array index option. This is a bit counter-intuitive, as it feels like it'll add a new contact when really you're just adding the last name to the existing contact, but it's the only way I found to get it to work.
  4. For the array index value, use the [...] button to choose the num_contacts local variable. It'll display as {lv=num_contacts}
  5. Choose the Add key option
  6. Type in the key name; in this case last_name, and choose the string type
  7. Choose the User Prompt option
  8. Fill in the prompt options
Hi! Sorry for the delay in replying, I've been busy the last few days.

Well, I managed to do what I needed using your macro as an example.

Now I can store contact information and display that information in a list.

I also set up two security questions for Macro execution.

Follow the macro for your evaluation.
 

Attachments

  • hide_contacts_new.macro
    42 KB · Views: 34
Top