note 126148 added to reserved.variables.files

From: Date: Mon, 07 Jun 2021 19:50:18 +0000
Subject: note 126148 added to reserved.variables.files
Groups: php.notes 
Request: Send a blank email to php-notes+get-216488@lists.php.net to get a copy of this message
Comprehensive iterator for direct "flat" access for any-level arrayed <input>, in any combination: <?php foreach( new RecursiveIteratorIterator( new FILES_flattened ) as $key => $data ) { var_dump( $key, $data ); // Here's how to generate an automated name that incorporates the "node path", e.g. "picture[10][5]" echo $key.( empty( $data[ 'pos' ] ) ? '' : '['. implode( '][', $data[ 'pos' ] ) . ']' ); } class FILES_flattened implements RecursiveIterator { protected $list_array; protected $basename; protected $current; public function __construct() { $this->list_array = $_FILES; } public function rewind(): void { if ( false === $current = reset( $this->list_array ) or null === $this->basename = key( $this->list_array ) ) { $this->current = null; return; } $current[ 'pos' ] = array(); $this->current = $current; } public function next(): void { if ( false === $current = next( $this->list_array ) or null === $this->basename = key( $this->list_array ) ) { $this->current = null; return; } $current[ 'pos' ] = array(); $this->current = $current; } public function valid(): bool { return null !== key( $this->list_array ); // End of array (inc. empty array). } public function hasChildren(): bool { return is_array( $this->current[ 'name' ] ); } public function getChildren(): FILES_nested { return new FILES_nested( $this->basename, $this->current ); } public function current() { return $this->current; } public function key() { return $this->basename; } } class FILES_nested extends FILES_flattened implements RecursiveIterator { private $node_array; private $position; public function __construct( string $basename, array $node_array ) { $this->basename = $basename; $this->list_array = $node_array[ 'name' ]; $this->position = $node_array[ 'pos' ]; unset( $node_array[ 'pos' ] ); $this->node_array = $node_array; } public function rewind(): void { if ( false === reset( $this->list_array ) or null === $key = key( $this->list_array ) ) { $this->current = null; return; } $this->current = $this->normalize( $key ); } public function next(): void { if ( false === next( $this->list_array ) or null === $key = key( $this->list_array ) ) { $this->current = null; return; } $this->current = $this->normalize( $key ); } private function normalize( $key ): array { $normalized = array_combine( array_keys( $this->node_array ), array_column( $this->node_array, $key) ); $position = $this->position; $position[] = $key; $normalized[ 'pos' ] = $position; return $normalized; } } ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 206.128.157.247) ---- Manual Page -- https://php.net/manual/en/reserved.variables.files.php Edit -- https://main.php.net/note/edit/126148 Del: integrated -- https://main.php.net/note/delete/126148/integrated Del: useless -- https://main.php.net/note/delete/126148/useless Del: bad code -- https://main.php.net/note/delete/126148/bad+code Del: spam -- https://main.php.net/note/delete/126148/spam Del: non-english -- https://main.php.net/note/delete/126148/non-english Del: in docs -- https://main.php.net/note/delete/126148/in+docs Del: other reasons-- https://main.php.net/note/delete/126148 Reject -- https://main.php.net/note/reject/126148 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216488) next »