简体   繁体   English

如何更改数组键的一部分?

[英]How to change the part of array key?

I've following array titled $_FILES : 我正在跟踪名为$_FILES数组:

Array
(
    [document_file_name_2] => Array
        (
            [name] => FAQ.doc
            [type] => application/msword
            [tmp_name] => /tmp/phpDnXqMX
            [error] => 0
            [size] => 35840
        )

    [document_file_name_5] => Array
        (
            [name] => Pay Slip-Sushil Kadam-June 2013.pdf
            [type] => application/pdf
            [tmp_name] => /tmp/php97sAKI
            [error] => 0
            [size] => 39648
        )

)

As this array is generated dynamically it's length may vary. 由于此数组是动态生成的,因此其长度可能会有所不同。 Now what I want to achieve is change some part of existing array key. 现在,我要实现的是更改现有阵列键的某些部分。 i want to make the array key start from the key [document_file_name_0] and so on. 我想使阵列键从键[document_file_name_0] ,依此类推。 The following array should come after doing manipulation. 以下数组应在进行操作后出现。 Can anyone help me how should I achieve this? 谁能帮我实现这个目标? Following is the desired $_FILES array: 以下是所需的$_FILES数组:

Array
    (
        [document_file_name_0] => Array
            (
                [name] => FAQ.doc
                [type] => application/msword
                [tmp_name] => /tmp/phpDnXqMX
                [error] => 0
                [size] => 35840
            )

        [document_file_name_1] => Array
            (
                [name] => Pay Slip-Sushil Kadam-June 2013.pdf
                [type] => application/pdf
                [tmp_name] => /tmp/php97sAKI
                [error] => 0
                [size] => 39648
            )

    )

Thanks in advance. 提前致谢。

$newArray=array();
$fileNum=0;
foreach ($originalArray as $file) {
    $newArray["document_file_name".$fileNum++]=$file;
}

The content of $newArray should be what you're looking for $ newArray的内容应该是您想要的

Get the values in the array using array_values 使用array_values获取数组中的值

Create a new array and loop through the array_values and set in the new array your custom_key => value pair 创建一个新数组并遍历array_values并在新数组中设置custom_key => value对

$counter = 0;
foreach ($array as $key => $values) {
  if ($key != 'document_file_name_' . $counter) { // If by coincidence this one is correct already, no need to rename/delete the original
    $array['document_file_name_' . $counter] = $array[$key];
    unset($array[$key]);
  }
  $counter++;
}

I know the best (according to the OP) question is selected, but I'm still eager to say that you could use almost EVERY php loop for this: 我知道选择了最好的(根据OP)问题,但是我仍然很想说,您可以为此使用几乎所有的php循环:

foreach() by @RubenSerrate 由@RubenSerrate编写的foreach()

$newArray=array();
$fileNum=0;
foreach ($originalArray as $file) {
    $newArray["document_file_name".$fileNum++]=$file;
}

for() ( phpfiddle ) for()phpfiddle

$newArr = array();
$length = count($_FILES);
for ($i=0; $i<$length; $i++) {
    $newArr["document_file_name_".$i] = array_shift($_FILES);
}

while() 而()

$newArr = array();
$i=0;
while ($_FILES) {
    $newArr["document_file_name_".$i] = array_shift($_FILES);
    $i++;
}

+ all other examples + all other yet not mentioned methods. +所有其他示例+所有其他尚未提及的方法。

Just learn PHP and good luck! 只要学习PHP并祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM