获取索引,同时将一个元素插入到php数字索引数组中[关闭]
问题描述:
The example code like here.
<?php
$new_room_result = array();
$rooms = array('single room' , 'delux room' , 'president room' , 'seavie room');
foreach($rooms as $room){
$new_room_result[] = $room;
// the next step - i want to get the index of after last inserment's index.
}
?>
Does anyone have any ideas?
答
foreach($rooms as $room){
$new_room_result[] = $room;
end($new_room_result); // move the internal pointer to the end of the array
$key = key($new_room_result);
var_dump($key);
}
答
All you need to do is to get the length of the array:
$index = count($new_room_result);
after inserting, into $new_room_result its content will be
array (size=4)
0 => string 'single room' (length=11)
1 => string 'delux room' (length=10)
2 => string 'president room' (length=14)
3 => string 'seavie room' (length=11)
Last index is 3, and the index after is 4 which is the size of the array
答
I have find it out by array_keys() and array_pop() functions. Whether there is a more easy way to reach what i want.