在特定位置的多维数组中添加多维数组

时间:2015-11-10 10:16:11

标签: php arrays multidimensional-array

我有一个像这样构建的多维数组(不介意字符串长度,我修改了值)

array(27) {
  [0]=>
  array(2) {
    ["name"]=>
    string(16) "Arsenal FC"
    ["id"]=>
    string(2) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(8) "Liverpool FC"
    ["id"]=>
    string(3) "2"
  }
[2]=>
  array(2) {
    ["name"]=>
    string(18) "Manchester United"
    ["id"]=>
    string(3) "3"
  }
}

现在我想添加一个像这样构建的数组

array(2) {
  ["name"]=>
  string(18) "Chelsea FC"
  ["id"]=>
  string(3) "4"

在第二个和第三个数组元素之间。

我这样做了

$this->clubs = array_slice($this->clubs, 0, 2, true) +
['name' => 'Chelsea FC', 'id' => 4] +
array_slice($this->clubs, 2, sizeof($this->clubs), true);

但是,这将返回以下数组

array(27) {
  [0]=>
  array(2) {
    ["name"]=>
    string(16) "Arsenal FC"
    ["id"]=>
    string(2) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(8) "Liverpool FC"
    ["id"]=>
    string(3) "2"
  }
  ["name"]=>
  string(20) "Chelsea FC"
  ["id"]=>
  int(4)
  [2]=>
  array(2) {
    ["name"]=>
    string(18) "Manchester United"
    ["id"]=>
    string(3) "3"
  }

如您所见,数组被添加到正确的位置,但格式不正确(不是多维)。我现在需要做的是在该位置添加数组并增加所有其他索引的计数。

我试图像这样实现它

if ($otherClubs->count() > 0) {

    array_walk($this->clubs, function($item, $key) use($index, $otherClubs) {
        if ($key > $index) {
            $this->clubs[$key + $otherClubs->count()] = $item;
            unset($this->otherClubs[$key]);
        }
    });
}

但这完全破坏了阵列

array(36) {
  [0]=>
  array(2) {
    ["name"]=>
    string(16) "Arsenal FC"
    ["id"]=>
    string(2) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(8) "Liverpool FC"
    ["id"]=>
    string(3) "2"
  }
  ["name"]=>
  string(20) "Chelsea FC"
  ["id"]=>
  int(3)
  [2]=>
  array(1) {
    ["id"]=>
    string(3) "4"
  }

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:4)

更新:尝试 array_splice :OPTION 1更好,更不凌乱,它会保留密钥。

<强> 1):

$arr=array (
    '0'=>
    array(
        "name"=>"Arsenal FC",
        "id"=>"1"
    ),
    '1'=>
    array(
        "name"=>"Liverpool FC",
        "id"=>"2"
    ),
    '2'=>
    array(
        "name"=>"Manchester United",
        "id"=>"3"
));

array_splice($arr, 2, 0, array(array('name' => 'Chelsea FC', 'id' => 4)));
echo "<pre>";
var_dump($arr);
  

<强>输出:

array(4) { [0]=> array(2) { ["name"]=> string(10) "Arsenal FC" ["id"]=> string(1) "1" } [1]=> array(2) { ["name"]=> string(12) "Liverpool FC" ["id"]=> string(1) "2" } [2]=> array(2) { ["name"]=> string(10) "Chelsea FC" ["id"]=> int(4) } [3]=> array(2) { ["name"]=> string(17) "Manchester United" ["id"]=> string(1) "3" } }

您的尝试方式。

<强> 2):

<?php 
$arr=array (
    '0'=>
    array(
        "name"=>"Arsenal FC",
        "id"=>"1"
    ),
    '1'=>
    array(
        "name"=>"Liverpool FC",
        "id"=>"2"
    ),
    '2'=>
    array(
        "name"=>"Manchester United",
        "id"=>"3"
));


$arr = array_values(array_slice($arr, 0, 2, true) + array(sizeof($arr)=>array('name' => 'Chelsea FC', 'id' => 4)) + array_slice($arr, 2, sizeof($arr), true));
echo "<pre>";
var_dump($arr);
?>

答案 1 :(得分:2)

+尝试合并数组。因此,您尝试添加的数组也应该是多维的

$this->clubs = array_slice($this->clubs, 0, 2, true) +
[1 => ['name' => 'Chelsea FC', 'id' => 4]] +
array_slice($this->clubs, 2, sizeof($this->clubs), true);

缺点是您应该为要添加的阵列提供密钥。

或者您可以使用array_merge

$this->clubs = array_merge(
    array_slice($this->clubs, 0, 2, true),
    [['name' => 'Chelsea FC', 'id' => 4]],
    array_slice($this->clubs, 2, sizeof($this->clubs), true)
);