Php 如何向现有多个数组添加键

Php 如何向现有多个数组添加键,php,arrays,Php,Arrays,我有下面的多重数组: array:4 [▼ 0 => array:5 [▼ 0 => "some content" 1 => "some content" 2 => "some content" 3 => "some content" 4 => "some content" ] 1 => array:5 [▼ 0 =>

我有下面的多重数组:

 array:4 [▼
      0 => array:5 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
      ]
      1 => array:5 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
      ]
      2 => array:6 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
        5 => "some content"
      ]
      3 => array:5 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
      ]
    ]
如何向该数组添加键,使其看起来像:

array:4 [▼
  0 => array:5 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
  ]
  1 => array:5 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
  ]
  2 => array:6 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
    somekey4 => "some content"
  ]
  3 => array:5 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
  ]
]
没有for循环有什么方法可以做到吗

以前没有任何方法添加键,因为我不知道将创建多少个数组


非常感谢您的关注和参与。

我建议使用和的组合来迭代数组,并将预设的键列表与每个嵌套数组的值组合起来

数组\u walk-将用户提供的函数应用于数组的每个成员
array_combine-使用一个数组作为键,另一个数组作为值来创建数组

示例代码

$array=array(
数组('some content1'、'some content'、'some content'、'some content'、'some content'),
数组('some content2'、'some content'、'some content'、'some content'、'some content'),
数组('some content3'、'some content'、'some content'、'some content'、'some content'),
数组('some content4'、'some content'、'some content'、'some content'、'some content'、'some content')
);    
$keys=array('title','subtitle','somekey','somekey2','somekey3');
功能组合(&$v、$i、$keys){
$v=数组与组合($keys,$v);
}
数组_walk($array,'combine',$keys);
打印(数组);
输出

数组(
[0]=>阵列
(
[标题]=>一些内容1
[字幕]=>部分内容
[somekey]=>一些内容
[somekey2]=>一些内容
[somekey3]=>一些内容
)
[1] =>阵列
(
[标题]=>一些内容2
[字幕]=>部分内容
[somekey]=>一些内容
[somekey2]=>一些内容
[somekey3]=>一些内容
)
[2] =>阵列
(
[标题]=>一些内容3
[字幕]=>部分内容
[somekey]=>一些内容
[somekey2]=>一些内容
[somekey3]=>一些内容
)
[3] =>阵列
(
[标题]=>一些内容4
[字幕]=>部分内容
[somekey]=>一些内容
[somekey2]=>一些内容
[somekey3]=>一些内容
)   
)
这是一个例子

请注意,只有当嵌套数组都包含相同数量的元素,并且键值数组包含相同数量的元素(在本例中为五个)时,此方法才有效。否则,您可能会得到一个关于“两个参数的元素数应该相等”的PHP错误


如果愿意,可以使用匿名函数在一行中实现:

array_walk($array,function(&$v,$i,$keys){$v=array_combine($keys,$v);},$keys);

信息不足

function convertKeys($table) {
    foreach($table as $key => $value) {
        if (0 === $key) {
            $table['title'] = $value;
            unset($table[$key]);
        } else if (1 === $key) {
            $table['subtitle'] = $value;
            unset($table[$key]);
        } else //another mapping
    }
}

foreach($table as $val) {
    //$val is arrray
    convertKeys($val);
}

在foreach循环中使用array_combine函数。参见下面的示例

$keys_array = array("title","subtitle","somekey","somekey2","somekey3");
$array=array(
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content')
);
$new_array =array();
foreach($array as $key=>$value){
     $new_array[] = array_combine($keys_array, $value);
}
print_r($new_array);

对于每个循环,构建一个新阵列。每个子阵列中总是有5个项目吗?谢谢。我想也许有一种方法没有foreach循环。项目是5或6。唉,
foreach
循环的问题是什么?