首页 > 解决方案 > 将数组从 Php 转换为 kotlin

问题描述

我在 PHP 中有这个数组,我想将它转换为 kotlin。在 kotlin 中有设置、列表和地图,但没有一个不符合我的目的。我怎样才能做到这一点?(有时我的值是 Int 有时是一个包含两个 Int 的数组)

$tes = array("position"=>[23,5], "id"=>"123");
var_dump($tes);

//array(2) {
//  ["position"]=>
//  array(2) {
//    [0]=>
//    int(23)
//   [1]=>
//    int(5)
//  }
//  ["id"]=>
//  string(3) "123"
//}

标签: phparrayskotlin

解决方案


在 Kotlin 中,我们希望使用类型安全的数据结构。您可以将数据抽象到适当的类中,而不是使用类型不佳的关联数组:

data class WhatEver(val id: String, val position: Pair<Int, Int>)

WhatEver("123", Pair(23,5)) //WhatEver(id=123, position=(23, 5))

推荐阅读