我有一个具有以下结构的多维数组:
[
{
"id": 1,
"parentID": 0,
"view": "App",
"name": "view",
"children": [
{
"id": 2,
"parentID": 1,
"view": "Analytics",
"name": "view",
"children": [
{
"id": 3,
"parentID": 2,
"view": "Summary",
"name": "view",
"children": [
{
"id": 11,
"parentID": 3,
"view": "Apps",
"name": "view"
},
{
"id": 12,
"parentID": 3,
"view": "Devices",
"name": "view"
}
]
},
{
"id": 4,
"parentID": 2,
"view": "Overview",
"name": "view"
},
{
"id": 5,
"parentID": 2,
"view": "Insights",
"name": "view"
},
{
"id": 7,
"parentID": 2,
"view": "DeviceData",
"name": "view"
},
{
"id": 8,
"parentID": 2,
"view": "Geolocation",
"name": "view"
},
{
"id": 6,
"parentID": 2,
"view": "NetworkInsights",
"name": "view"
}
]
},
{
"id": 9,
"parentID": 1,
"view": "VirtualAppInstaller",
"name": "view",
"children": [
{
"id": 10,
"parentID": 9,
"view": "AppManager",
"name": "view"
}
]
}
]
}
]
但是我需要构建一个包含 view 键的每个元素的字符串数组。
赞:
[
"App.Analytics.Summary.Apps",
"App.Analytics.Summary.Devices",
"App.Analytics.Overview",
"App.Analytics.Insights",
... ,
... ,
"App.VirtualAppInstaller.AppManager"
]
我在考虑创建一个递归函数以提取包含标签 view
的每个对象但是我不知道什么是最好的方法。
更新
我能够实现递归功能
function recursion($array) {
foreach ($array as $key => $value) {
if($key==='view'){
var_dump($value);
}
if(is_array($value)){
$this->recursion($value);
}
}
}
结果:
string(3)"App"string(9)"Analytics"string(7)"Summary"string(4)"Apps"string(7)"Devices"string(8)"Overview"string(8)"Insights"string(10)"DeviceData"string(11)"Geolocation"string(15)"NetworkInsights"string(19)"VirtualAppInstaller"string(10)"AppManager"{}
但是我现在仍然知道该函数何时到达孩子的末端,并以此来创建第一行
答案 0 :(得分:1)
此递归函数将执行您想要的操作。它遍历数组中的每个元素及其子元素,并通过 Stores metadata of a struct.
## State
The state of the schema is stored in the `:state`
field and allows following values:
* `:built` - the struct was constructed in
memory and is not persisted
to database yet;
* `:loaded` - the struct was loaded from database
and represents persisted data;
* `:deleted` - the struct was deleted and no longer
represents persisted data.
## Source
The `:source` tracks the (table or collection) where
the struct is or should be persisted to.
## Prefix
Tracks the source prefix in the data storage.
## Context
The `:context` field represents additional state some
databases require for proper updates of data. It is
not used by the built-in adapters of `Ecto.Adapters.Postres`
and `Ecto.Adapters.MySQL`.
## Schema
The `:schema` field refers the module name for the
schema this metadata belongs to.
键构建字符串:
An Ecto schema is used to map any data source into an Elixir struct.
The definition of the schema is possible through two main APIs:
`schema/2` and `embedded_schema/1`.
`schema/2` is typically used to map data from a persisted source,
usually a database table, into Elixir structs and vice-versa. For
this reason, the first argument of `schema/2` is the source (table)
name. Structs defined with `schema/2` also contain a `__meta__` field
with metadata holding the status of the struct, for example, if it
has been built, loaded or deleted.
On the other hand, `embedded_schema/1` is used for defining schemas
that are embedded in other schemas or only exist in-memory. For example,
you can use such schemas to receive data from a command line interface
and validate it, without ever persisting it elsewhere. Such structs
do not contain a `__meta__` field, as they are never persisted.
对于您的示例数组,输出为:
数组
view