다른 뜻에 대해서는 함수 collection_dot() 문서를 참조하십시오.
개요
- 함수 dot()
- 함수 dict_dot()
- 함수 array_dot()
PHP
<?php
function array_dot($array, $prepend = '') {
$results = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$results = array_merge($results, array_dot($value, $prepend . $key . '.'));
} else {
$results[$prepend . $key] = $value;
}
}
return $results;
}
print_r( array_dot(['foo'=>['bar'=>'baz']]) );
# Array
# (
# [foo.bar] => baz
# )
print_r( array_dot(["name"=>["first"=>"Yonezawa","last"=>"Akinori"],"tel"=>"1234-5678"]) );
# Array
# (
# [name.first] => Yonezawa
# [name.last] => Akinori
# [tel] => 1234-5678
# )
Python
Python 3
def dot( d, prepend='' ):
results = {}
for key in d:
value = d[key]
if isinstance(value, dict): results.update( dot(value, prepend + key + '.') )
else: results[prepend + key] = value
return results
print( dot({'foo':{'bar':'baz'}}) )
# {'foo.bar': 'baz'}
print( dot({'name':{'first':'Yonezawa','last':'Akinori'},'tel':'1234-5678'}) )
# {'name.last': 'Akinori', 'name.first': 'Yonezawa', 'tel': '1234-5678'}
Python 2
import types
def dot( d, prepend='' ):
results = {}
for key in d:
value = d[key]
if isinstance(value, types.DictType): results.update( dot(value, prepend + key + '.') )
else: results[prepend + key] = value
return results
print( dot({'foo':{'bar':'baz'}}) )
# {'foo.bar': 'baz'}
print( dot({'name':{'first':'Yonezawa','last':'Akinori'},'tel':'1234-5678'}) )
# {'name.last': 'Akinori', 'name.first': 'Yonezawa', 'tel': '1234-5678'}