note 77849 modified in function.array-splice by thiago
| From: | thiago@php.net | Date: | Thu, 24 Jul 2008 17:24:19 +0000 |
| Subject: | note 77849 modified in function.array-splice by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142672@lists.php.net to get a copy of this message | ||
for inserting array in 2-d array, according x position, y position
<?php
function add_module_xy($x_loc, $y_loc, $module) {
//identify the column of the modules
switch ($x_loc) {
case 1:
$x = 'left';
break;
case 2:
$x = 'middle';
break;
case 3:
$x = 'right';
break;
default:
throw new Exception("", "Invalid horizontal position $x_loc");
}
$max_y_loc = count($this->module_arrays[$x]) + 1;
if ($y_loc > $max_y_loc) {
//if y location is greater then max array index then add module to last
$y_loc = $max_y_loc;
}
$left = array_slice ($this->module_arrays[$x], 0, $y_loc-1);
$right = array_slice ($this->module_arrays[$x], $y_loc-1);
$insert[0] = $module;
$array = array_merge ($left, $insert, $right);
$this->module_arrays[$x] = $array;
}
?>
--was--
for inserting array in 2-d array, according x position, y position
function add_module_xy($x_loc, $y_loc, $module) {
//identify the column of the modules
switch ($x_loc) {
case 1:
$x = 'left';
break;
case 2:
$x = 'middle';
break;
case 3:
$x = 'right';
break;
default:
throw new Exception("", "Invalid horizontal position $x_loc");
}
$max_y_loc = count($this->module_arrays[$x]) + 1;
if ($y_loc > $max_y_loc) {
//if y location is greater then max array index then add module to last
$y_loc = $max_y_loc;
}
$left = array_slice ($this->module_arrays[$x], 0, $y_loc-1);
$right = array_slice ($this->module_arrays[$x], $y_loc-1);
$insert[0] = $module;
$array = array_merge ($left, $insert, $right);
$this->module_arrays[$x] = $array;
}
http://php.net/manual/en/function.array-splice.php