如何从数组中提取特定键的值?
- 内容介绍
- 文章标签
- 相关推荐
本文共计77个文字,预计阅读时间需要1分钟。
pythondef get_key_values(arr, key): ret=[] for row in arr: if isinstance(row, dict) and key in row: ret.append(row[key]) elif isinstance(row, list) and key in row: ret.append(row[key]) return ret
getKeyValuesfunction getKeyValues($arr, $key) { $ret = []; foreach ($arr as $row) { if (is_array($row) && array_key_exists($key, $row)) { $ret[] = $row[$key]; } elseif ($row instanceof \ArrayObject) { $row->offsetExists($key) && $ret[] = $row->offsetGet($key); } } return $ret; }
本文共计77个文字,预计阅读时间需要1分钟。
pythondef get_key_values(arr, key): ret=[] for row in arr: if isinstance(row, dict) and key in row: ret.append(row[key]) elif isinstance(row, list) and key in row: ret.append(row[key]) return ret
getKeyValuesfunction getKeyValues($arr, $key) { $ret = []; foreach ($arr as $row) { if (is_array($row) && array_key_exists($key, $row)) { $ret[] = $row[$key]; } elseif ($row instanceof \ArrayObject) { $row->offsetExists($key) && $ret[] = $row->offsetGet($key); } } return $ret; }

