Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access values from multidimen array in lua?

Tags:

javascript

lua

k = {
  messageCode = 200,
  result = {
    data = [
      {id=7,language="Hindi"},
      {id=8,language="Tamil"}
    ]
  }
}

How do I access language here?

I have been trying this way

print(k.result.data.language)
like image 662
krish Avatar asked Jan 20 '26 13:01

krish


1 Answers

Your attempt to access the table is almost right, but your table is malformed.

k = {
  messageCode = 200,
  result = {
    data = {
      {
        id = 7,
        language = "Hindi"
      },
      {
        id = 8,
        language = "Tamil"
      }
    }
  }
}
print(k.result.data[1].language)
print(k.result.data[2].language)

k.result.data is an array (numeric lua table), so you have to iterate or access them by number.

like image 155
csaar Avatar answered Jan 23 '26 02:01

csaar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!