Hàm array_splice[] loại bỏ các phần tử đã chọn khỏi một mảng và thay thế nó bằng các phần tử mới. Hàm cũng trả về một mảng với các phần tử đã bị xóa
Mẹo. Nếu hàm không loại bỏ phần tử nào [độ dài=0] thì mảng được thay thế sẽ được chèn vào từ vị trí của tham số bắt đầu [Xem ví dụ 2]
Một mảng có 4 đối tượng được khai báo và gán cho biến ‘objectarray’. Ở đây, chúng tôi muốn xóa đối tượng khỏi chỉ mục 2, đối tượng này cũng được khai báo với biến có tên là 'index'. Vòng lặp foreach được sử dụng để duyệt qua mảng và khi giá trị chỉ mục trong quá trình duyệt khớp với chỉ mục từ nơi giá trị cần được xóa, hàm 'unset' được gọi trên phần tử đó và các phần tử còn lại được trả về dưới dạng đầu ra
Since unset[] is a language construct, it cannot be passed anything other than a variable. It's sole purpose is to "unset" this variable, ie. to remove it from the current scope and destroy it's associated data. This is true especially for reference variables, where not the actual value is destroyed but the reference to that value. This is why you can't wrap 'unset[]' in a user defined function: You would either unset a copy of the data if the parameter is passed by value, or you would just unset the reference variable within the functions scope if the parameter is passed by reference. There is no workaround for that, as you cannot pass 'scope' to a function in PHP. Such a function can only work for variables that exist in a common or global scope [compare 'unset[$_GLOBALS[variable]]'].
I don't know how PHP handles garbage collection internally, but I guess this behavior can result in a huge memory leak: if a value variable goes out of scope with a second variable still holding a reference to the in-memory value, then unsetting that reference would still hold the value in memory but potentially unset the last reference to that in-memory data, hence: occupied memory that is rendered useless as you cannot reference it anymore.
Prompted by dire need, and inspired by some of the existing notes, I came up with this:
/* Like array_splice[], but preserves the key[s] of the replacement array. */
function array_splice_assoc[&$input, $offset, $length = 0, $replacement = array[]] {
$tail = array_splice[$input, $offset];
$extracted = array_splice[$tail, 0, $length];
$input += $replacement + $tail;
return $extracted;
};
Apart from preserving the keys, it behaves just like the regular array_splice[] for all cases I could think of.
So for example the regular array_splice[]
$input = array['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' =>6];
print_r[array_splice[$input, -4, 3, array['foo1' => 'bar', 'foo2' => 'baz']]];
print_r[$input];
will give:
________số 8
But with array_splice_assoc[]
I don't know how PHP handles garbage collection internally, but I guess this behavior can result in a huge memory leak: if a value variable goes out of scope with a second variable still holding a reference to the in-memory value, then unsetting that reference would still hold the value in memory but potentially unset the last reference to that in-memory data, hence: occupied memory that is rendered useless as you cannot reference it anymore.
0
I don't know how PHP handles garbage collection internally, but I guess this behavior can result in a huge memory leak: if a value variable goes out of scope with a second variable still holding a reference to the in-memory value, then unsetting that reference would still hold the value in memory but potentially unset the last reference to that in-memory data, hence: occupied memory that is rendered useless as you cannot reference it anymore.
1
/* Like array_splice[], but preserves the key[s] of the replacement array. */
0
function array_splice_assoc[&$input, $offset, $length = 0, $replacement = array[]] {
$tail = array_splice[$input, $offset];
$extracted = array_splice[$tail, 0, $length];
$input += $replacement + $tail;
return $extracted;
};
/* Like array_splice[], but preserves the key[s] of the replacement array. */
1
function array_splice_assoc[&$input, $offset, $length = 0, $replacement = array[]] {
$tail = array_splice[$input, $offset];
$extracted = array_splice[$tail, 0, $length];
$input += $replacement + $tail;
return $extracted;
};
/* Like array_splice[], but preserves the key[s] of the replacement array. */
2
function array_splice_assoc[&$input, $offset, $length = 0, $replacement = array[]] {
$tail = array_splice[$input, $offset];
$extracted = array_splice[$tail, 0, $length];
$input += $replacement + $tail;
return $extracted;
};
/* Like array_splice[], but preserves the key[s] of the replacement array. */
3
function array_splice_assoc[&$input, $offset, $length = 0, $replacement = array[]] {
$tail = array_splice[$input, $offset];
$extracted = array_splice[$tail, 0, $length];
$input += $replacement + $tail;
return $extracted;
};
/* Like array_splice[], but preserves the key[s] of the replacement array. */
4
function array_splice_assoc[&$input, $offset, $length = 0, $replacement = array[]] {
$tail = array_splice[$input, $offset];
$extracted = array_splice[$tail, 0, $length];
$input += $replacement + $tail;
return $extracted;
};