Bu sınıf geliştirmekte olduğunuz sınıf için yol gösterici olabilir. Sormak istediğiniz sorular olursa çekinmeden sorabilirsiniz.
<?php
/*
* SAINTX > PHP Arr
*
* @author: SAINTX
* @web: http://saintx.net
* @mail: im@saintx.net
* @date: 22.07.2013
*/
class SAINTX {
const AUTHOR = 'SAINTX';
private $array = array();
public function __construct() { /* CODE IS POETRY. */ }
public function set($arr) {
$this->array = $arr;
return $this;
}
public function get() {
return $this->array;
}
public function is_assoc() {
return is_array($this->array) && array_diff_key($this->array, array_keys(array_keys($this->array)));
}
public function push($value, $key='', $target='begin') {
switch($target) {
case 'begin': {
$this->array = array_reverse($this->array);
if(empty($key))
$this->array[] = $value;
else
$this->array[$key] = $value;
$this->array = array_reverse($this->array);
} break;
case 'end': {
if(empty($key))
$this->array[] = $value;
else
$this->array[$key] = $value;
} break;
default: {
if(empty($key))
$this->array[] = $value;
else
$this->array[$key] = $value;
}
}
return $this;
}
public function push_element($value, $key, $target='begin') {
if($target == 'begin')
$this->array[$key] = sprintf('%s%s', $value, $this->array[$key]);
else if($target == 'end')
$this->array[$value] .= $value;
else
$this->array[$value] .= $value;
return $this;
}
public function delete($key) {
unset($this->array[$key]);
if(!$this->is_assoc())
$this->array = array_values($this->array);
return $this;
}
public function transfer($key1, $key2) {
$val1 = $this->array[$key1];
$val2 = $this->array[$key2];
$this->array[$key1] = $val2;
$this->array[$key2] = $val1;
return $this;
}
public function move($key, $target='begin') {
$value = $this->array[$key];
$this->delete($key);
$this->push($value, $key, $target);
return $this;
}
}Kullanımı;
<?php
$SAINTX = new SAINTX;
$SAINTX->set(array(
'elma',
'armut',
'kelmahmut',
'sebzeler' => array(
'roka',
'havuç',
'domates',
'biber'
)
));
$SAINTX->push('kivi', '', 'begin')->transfer(0, 1)->move('sebzeler', 'begin');
print_r($SAINTX->get());
/*
ÇIKTI:
-----------------------------
Array
(
[sebzeler] => Array
(
[0] => roka
[1] => havuç
[2] => domates
[3] => biber
)
[0] => elma
[1] => kivi
[2] => armut
[3] => kelmahmut
)
*/
?>GitHub adresi:
http://git.io/h5BBZA