简体   繁体   English

PHP将索引插入为数组中的值

[英]PHP insert index as value in array

How can I add to a multidimensional array also the index number of the current added item? 如何将当前添加项的索引号也添加到多维数组?

$data_array[] = array('data_array_index' => *the index number of the this array on $data_array*, 'stuff' => 'stuff', etc.)

So when I: 所以当我:

print_r($data_array);

Array(
  [0] => Array(
            data_array_index => 0
            stuff => stuff
         )
  [25] => Array(
            data_array_index => 25
            stuff => stuff
         )
  etc.

Thank you 谢谢

EDIT 编辑

Should this work? 应该行吗?

$data_array[] = array('data_array_index' => end($data_array)+1, 'stuff' => 'stuff', etc.)

You could do this: 您可以这样做:

$array = [
   0 => [ "data_array_index" => 0, "stuff" => "stuff" ],
   25 => [ "data_array_index" => 25, "stuff" => "stuff" ]
];

$array[] = array('data_array_index' => 0, 'stuff' => 'stuff')

end($array);
$last_id = key($array);

$array[$last_id]['data_array_index'] = $last_id;

I don't know why you want data_array_index in the array because if you put it in a foreach loop you can get the key without needing the variable. 我不知道为什么要在数组中使用data_array_index ,因为如果将其放在foreach循环中,则无需变量即可获取键。

Example: 例:

foreach($key => $data) {
        ^^^^ The same as `data_array_index`
}

Suppose you have this array: 假设您有以下数组:

$data_array = [
   0 => [ "data_array_index" => 0, "stuff" => "stuff" ],
   25 => [ "data_array_index" => 25, "stuff" => "stuff" ]
];

Now to set a key (note the $data_array[100] ): 现在设置一个键(注意$data_array[100] ):

$data_array[100] = [ "data_array_index" => 100, "stuff" => "stuff" ];

try this once 尝试一次

$arr=array(12=>array("stuff"=>"stuff1"),15=>array("stuff"=>"stuff2"));

foreach($arr as $key=>$val){
$arr[$key]['data_array_index']=$key;    
}

echo "<pre>"; print_r($arr);

Use array_walk 使用array_walk

array_walk($data_array,function($value,$key)use($new_array) {
    $value['data_array_index'] = $key;
    $new_array[$key]=$value;
});

working demo: http://phpfiddle.org/main/code/p937-7cue 工作演示: http : //phpfiddle.org/main/code/p937-7cue

For my solution see the code below. 对于我的解决方案,请参见下面的代码。 Beware that this is a very rudimentary function now. 当心,这是一个非常基本的功能。 It does not provide any fail safe or fallback. 它不提供任何故障保险或后备功能。 If you delete a key it will to fill the space etc. 如果删除键,它将填充空格等。

<?php

// data array
$data_array = [];

// additional info for the array
$info_a = "Lorem Ipsum";
$info_b = "Lorem Ipsum";

// call function
addElement($data_array, $info_a);
addElement($data_array, $info_b);

// display output
echo '<pre>';
print_r($data_array);
echo '</pre>';

function addElement(&$array, $info)
{
    // add info to the array
    $array[] = [
        'stuff'=>$info
    ];

    // get the key of the current array
    end($array);
    $key = key($array);

    // add this information to the array
    $array[$key]['data_array_index'] = $key;
}
?>

Output would be 输出将是

Array
(
    [0] => Array
        (
            [stuff] => Lorem Ipsum
            [data_array_index] => 0
        )

    [1] => Array
        (
            [stuff] => Lorem Ipsum
            [data_array_index] => 1
        )

)

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

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