Home > Backend Development > PHP Tutorial > php数组如何保留数字索引

php数组如何保留数字索引

WBOY
Release: 2016-06-13 13:18:08
Original
1015 people have browsed it

php数组怎么保留数字索引?
php数组怎么保留数字索引?

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php $arr=array("4","5","6");

    $arr2=array();
    foreach($arr as $v){
        $arr2[$v]=$v;
    }
    print_r($arr2);
    echo "<hr>";
    $arr3=array_merge(array("1"=>"1","2"=>"..."),$arr2);
    print_r($arr3);
?>

Copy after login




------解决方案--------------------
print_r(array("1"=>"1","2"=>"...") + $arr2);

Array ( [1] => 1 [2] => ... [4] => 4 [5] => 5 [6] => 6 ) 


array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。 

如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。 

如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template