[PHP] Returning reference problem
Starting over since my last thread had too little effort put in it. I have this script that should create a multi-demensional array that would resemble a directory structure. The problem is that after adding one element to a folder it appears to be creating a new object instead of adding another item to the current object. So there can only be one file or folder in the parent folder with the exception of the top level. My entire script is below. Thanks, Jon ? class dir { var $name; var $subdirs; var $files; var $num; var $prio; function dir($name,$num,$prio) { $this-name = $name; $this-num = $num; $this-prio = $prio; $this-files = array(); $this-subdirs = array(); } function addFile($file) { $this-files[] = $file; return $file; } function addDir($dir) { $this-subdirs[] = $dir; return $dir; } function findDir($name) { foreach($this-subdirs as $v){ if($v-name == $name) return $v; } return false; } function draw($parent) { echo('d.add('.$this-num.','.$parent.','.$this-name.\,.$this-prio.);\n); foreach($this-subdirs as $v) { $v-draw($this-num); } foreach($this-files as $v) if(is_object($v)) { echo(d.add(.$v-num.,.$this-num., \.$v-name.\,.$v-prio.);\n); } } } class file { var $name; var $prio; var $size; var $num; function file($name,$num,$size,$prio) { $this-name = $name; $this-num = $num; $this-size = $size; $this-prio = $prio; } } $arFiles = array ( 0 = array ( 'path' = array ( 0 = 'folder1', 1 = 'subfolder1', 2 = 'file1.ext' ), 'length' = 5464, 'size' = 8765 ), 1 = array ( 'path' = array ( 0 = 'folder1', 1 = 'subfolder1', 2 = 'file2.ext' ), 'length' = 5464, 'size' = 8765 ), 2 = array ( 'path' = array ( 0 = 'folder1', 1 = 'subfolder2', 2 = 'file1.ext' ), 'length' = 5464, 'size' = 8765 ), 3 = array ( 'path' = array ( 0 = 'folder2', 1 = 'subfolder1', 2 = 'file1.ext' ), 'length' = 5464, 'size' = 8765 ) ); $prio = array(); for($i=0;$icount($arFiles);$i++) $prio[$i] = -1; $dirnum = count($arFiles); $tree = new dir(/,$dirnum,isset($prio[$dirnum])?$prio[$dirnum]:-1); foreach( $arFiles as $filenum = $file) { $depth = count($file['path']); $branch = $tree; for($i=0; $i $depth; $i++){ if ($i != $depth-1){ $d = $branch-findDir($file['path'][$i]); echo BRBTree after find dir:/B; print_r($tree); if($d) $branch = $d; else{ $dirnum++; $d = $branch-addDir(new dir($file['path'][$i], $dirnum, (isset($prio[$dirnum])?$prio[$dirnum]:-1))); echo BRBTree after add dir:/B; print_r($tree); $branch = $d; } }else{ $branch-addFile(new file($file['path'][$i]. (.$file['length'].),$filenum,$file['size'], $prio[$filenum])); echo BRBTree after add file:/B; print_r($tree);} } } $tree-draw(-1); ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Returning reference problem
Jon wrote: Starting over since my last thread had too little effort put in it. I have this script that should create a multi-demensional array that would resemble a directory structure. The problem is that after adding one element to a folder it appears to be creating a new object instead of adding another item to the current object. So there can only be one file or folder in the parent folder with the exception of the top level. My entire script is below. Thanks, Jon ? class dir { var $name; var $subdirs; var $files; var $num; var $prio; function dir($name,$num,$prio) { $this-name = $name; $this-num = $num; $this-prio = $prio; $this-files = array(); $this-subdirs = array(); } function addFile($file) { $this-files[] = $file; return $file; } a missing ampersand above might being causing you problems... function addFile($file) { ^--- function addDir($dir) { $this-subdirs[] = $dir; return $dir; } function findDir($name) { foreach($this-subdirs as $v){ if($v-name == $name) return $v; } return false; } function draw($parent) { echo('d.add('.$this-num.','.$parent.','.$this-name.\,.$this-prio.);\n); foreach($this-subdirs as $v) { $v-draw($this-num); } foreach($this-files as $v) if(is_object($v)) { echo(d.add(.$v-num.,.$this-num., \.$v-name.\,.$v-prio.);\n); } } } class file { var $name; var $prio; var $size; var $num; function file($name,$num,$size,$prio) { $this-name = $name; $this-num = $num; $this-size = $size; $this-prio = $prio; } } $arFiles = array ( 0 = array ( 'path' = array ( 0 = 'folder1', 1 = 'subfolder1', 2 = 'file1.ext' ), 'length' = 5464, 'size' = 8765 ), 1 = array ( 'path' = array ( 0 = 'folder1', 1 = 'subfolder1', 2 = 'file2.ext' ), 'length' = 5464, 'size' = 8765 ), 2 = array ( 'path' = array ( 0 = 'folder1', 1 = 'subfolder2', 2 = 'file1.ext' ), 'length' = 5464, 'size' = 8765 ), 3 = array ( 'path' = array ( 0 = 'folder2', 1 = 'subfolder1', 2 = 'file1.ext' ), 'length' = 5464, 'size' = 8765 ) ); $prio = array(); for($i=0;$icount($arFiles);$i++) $prio[$i] = -1; $dirnum = count($arFiles); $tree = new dir(/,$dirnum,isset($prio[$dirnum])?$prio[$dirnum]:-1); foreach( $arFiles as $filenum = $file) { $depth = count($file['path']); $branch = $tree; for($i=0; $i $depth; $i++){ if ($i != $depth-1){ $d = $branch-findDir($file['path'][$i]); echo BRBTree after find dir:/B; print_r($tree); if($d) $branch = $d; else{ $dirnum++; $d = $branch-addDir(new dir($file['path'][$i], $dirnum, (isset($prio[$dirnum])?$prio[$dirnum]:-1))); echo BRBTree after add dir:/B; print_r($tree); $branch = $d; } }else{ $branch-addFile(new file($file['path'][$i]. (.$file['length'].),$filenum,$file['size'], $prio[$filenum])); echo BRBTree after add file:/B; print_r($tree);} } } $tree-draw(-1); ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php