Dear techie's,
We all know that there is no built array list class in PHP . Its not a mean that we cant implement Array list functionality in PHP . As of now PHP don't have array list class. But still we can have a array list functionality in PHP.
The Mike langue given good solution for array list. It does not work as java array list. but we can get good result from this array list class.
More details : http://www.icurtain.co.uk/161_PHP_ArrayList_Class
I have worked on this array list class and i wrote a standard class to display a database values using array list class. its works great for me. so here i contribute my works. please make it useful.
To Implement Array list in php. we need to download the array list class.
Download Link : http://www.icurtain.co.uk/161_PHP_ArrayList_Class
Or
Copy and paste below code in your application.
We all know that there is no built array list class in PHP . Its not a mean that we cant implement Array list functionality in PHP . As of now PHP don't have array list class. But still we can have a array list functionality in PHP.
The Mike langue given good solution for array list. It does not work as java array list. but we can get good result from this array list class.
More details : http://www.icurtain.co.uk/161_PHP_ArrayList_Class
I have worked on this array list class and i wrote a standard class to display a database values using array list class. its works great for me. so here i contribute my works. please make it useful.
To Implement Array list in php. we need to download the array list class.
Download Link : http://www.icurtain.co.uk/161_PHP_ArrayList_Class
Or
Copy and paste below code in your application.
arrayList[sizeof($this->arrayList)] = $item;
array_push($this->arrayList, $item);
}
public function addAtPos($position, $item) {
if ($position < count($this->arrayList) && $position >= 0) {
$this->add($item);
$this->shift(count($this->arrayList) - 1, $position);
} else {
throw new Exception('List out of bounds');
}
}
public function getList() {
return $this->arrayList;
}
public function hasValue() {
if (isset($this->arrayList[$this->pointer])) {
return true;
} else {
return false;
}
}
public function hasNext() {
if ($this->pointer <= count($this->arrayList) - 1) {
return true;
} else {
return false;
}
}
public function next() {
if (isset($this->arrayList[$this->pointer])) {
//return $this->arrayList[($this->pointer++)-1] = $value;
$this->pointer++;
return($this->arrayList[$this->pointer - 1]);
} else {
return null;
}
}
public function shift($origin, $dest) {
//wont shift from last element
if ($origin > count($this->arrayList) || $origin < 0 || $dest > count($this->arrayList) || $dest < 0) {
throw new Exception('List out of bounds');
}
if ($origin > $dest) {
$temp = $this->arrayList[$origin];
$this->shiftUp($origin, $dest);
$this->arrayList[$dest] = $temp;
} else {
$temp = $this->arrayList[$origin];
$this->shiftDown($origin, $dest);
$this->arrayList[$dest] = $temp;
}
}
private function shiftUp($origin, $dest) {
for ($i = $origin; $i > $dest; $i--) {
$this->arrayList[$i] = $this->arrayList[$i - 1];
}
}
private function shiftDown($origin, $dest) {
for ($i = $origin; $i < $dest; $i++) {
$this->arrayList[$i] = $this->arrayList[$i + 1];
}
}
public function remove($item) {
if (array_key_exists($item, $this->arrayList)) {
unset($this->arrayList[$item]);
} else {
throw new Exception('key not found');
}
}
public function addArray($array) {
foreach ($array as $item) {
$this->add($item);
}
}
public function reverse() {
$this->arrayList = array_reverse($this->arrayList);
}
public function size() {
return count($this->arrayList);
}
public function reset() {
$this->pointer = 0;
}
public function end() {
$this->pointer = count($this->arrayList) - 1;
}
}
?>







0 comments:
Post a Comment