array_splice() :将数组插入索引为 [PHP] 的二维数组中
array_splice() : Insert array into 2-dimensional array at index [PHP]
我正在尝试将一个数组插入二维数组的特定位置。
根据手册,我应该可以使用 array_splice() 执行此操作,但它只会删除我的接收数组的内容而不会插入。
我想得到一个包含 $receivingArray 的所有值(数组)加上新值(数组)的数组。
我做错了什么??
手册信息:
array array_splice ( array &$input , int $offset [, int $length [, mixed $replacement = array() ]] )
If length is specified and is zero, no elements will be removed.
If replacement is just one element it is not necessary to put array() around > it, unless the element is an array itself, an object or NULL.
输入:
$newArray = array_splice($receivingArray, 0, 0, array($value));
结果:$newArray 是一个空数组
输入:
$newArray = array_splice($receivingArray, 1, 0, array($value));
结果:$newArray 是一个空数组
这个输入:
print_r($receivingArray);
print_r(array($value));
$newArray = array_splice($receivingArray, 1, 1, array($value));
print_r($newArray);
给出:(有趣的)
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
Array
(
[0] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
)
Array
(
[0] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
的文档
Return Values
Returns the array consisting of the extracted elements.
$newArray = array_splice($receivingArray, 0, 0, array($value));
array_splice 修改其输入,因此您要查找的结果在 $receivingArray
而不是 $newArray
我错过了 array_slice() 实际上并不 return 它的输出,而是作用于通过引用传递的接收数组本身这一事实。没注意到手册规范中第一个参数前面有一个符号。
这个输入:
print_r($receivingArray);
print_r(array($value));
array_splice($receivingArray, 0, 0, array($value));
print_r($receivingArray);
给出正确的结果:
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
Array
(
[0] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
)
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
[2] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
我正在尝试将一个数组插入二维数组的特定位置。 根据手册,我应该可以使用 array_splice() 执行此操作,但它只会删除我的接收数组的内容而不会插入。
我想得到一个包含 $receivingArray 的所有值(数组)加上新值(数组)的数组。
我做错了什么??
手册信息:
array array_splice ( array &$input , int $offset [, int $length [, mixed $replacement = array() ]] )
If length is specified and is zero, no elements will be removed. If replacement is just one element it is not necessary to put array() around > it, unless the element is an array itself, an object or NULL.
输入:
$newArray = array_splice($receivingArray, 0, 0, array($value));
结果:$newArray 是一个空数组
输入:
$newArray = array_splice($receivingArray, 1, 0, array($value));
结果:$newArray 是一个空数组
这个输入:
print_r($receivingArray);
print_r(array($value));
$newArray = array_splice($receivingArray, 1, 1, array($value));
print_r($newArray);
给出:(有趣的)
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
Array
(
[0] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
)
Array
(
[0] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
Return Values
Returns the array consisting of the extracted elements.
$newArray = array_splice($receivingArray, 0, 0, array($value));
array_splice 修改其输入,因此您要查找的结果在 $receivingArray
而不是 $newArray
我错过了 array_slice() 实际上并不 return 它的输出,而是作用于通过引用传递的接收数组本身这一事实。没注意到手册规范中第一个参数前面有一个符号。
这个输入:
print_r($receivingArray);
print_r(array($value));
array_splice($receivingArray, 0, 0, array($value));
print_r($receivingArray);
给出正确的结果:
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
Array
(
[0] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
)
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
[2] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)