php - 将数组从 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"
//}
解决方案
在 Kotlin 中,我们希望使用类型安全的数据结构。您可以将数据抽象到适当的类中,而不是使用类型不佳的关联数组:
data class WhatEver(val id: String, val position: Pair<Int, Int>)
WhatEver("123", Pair(23,5)) //WhatEver(id=123, position=(23, 5))
推荐阅读
- kotlin - 多个客户端可以订阅的 Kotlin observable
- javascript - 如何在 React 应用程序中运行 AWS 而不会出现调用尝试超出错误?
- php - Doctrine Array 类型字段未更新
- algorithm - 算法问题:找到运送所有包裹的最少旅行次数
- python - Flask-SQLAlchemy:更改 raspberryPie 的 sd 卡后出现“AttributeError:无法设置属性”
- linux - 将引用的命令管道作为函数参数传递(并执行)
- slack - 如何使用 slack web api users.profile.set 设置用户自定义配置文件的单个字段而不清除其他现有自定义字段值?
- android - 如何使用 Google Play In-App Review API 了解已完成应用评分的用户?
- python - 对已关闭文件的 I/O 操作。在 Python 3.7 中将数据写入 excel 文件时出错
- mysql - 只有当它具有NULL值时才将数据插入到列中,否则更新它