Home > Backend Development > PHP Tutorial > 怎样在数组中插入一个值,该怎么处理

怎样在数组中插入一个值,该怎么处理

WBOY
Release: 2016-06-13 13:37:59
Original
1214 people have browsed it

怎样在数组中插入一个值
$arr1=array(1,4,5,7,9,15,18,20);
假设在5的后面插入一个6,并输出数组
1,4,5,6,7,9,15,18,20

------解决方案--------------------
你头像换得蛮快嘛...如果不是升序的话.... 这是刚写的函数,如果需要插入多个自己修改吧.

PHP code

$arr1 = array (1, 4, 5, 7, 9, 15, 18, 20 );
print_r ( input_array ( $arr1, 5, 6 ) );

function input_array($array, $value, $input) {
    $key = array_search ( $value, $array );
    if ($key !== false) {
        array_splice ( $array, $key, 1, array ($array [$key], $input ) );
        return $array;
    }
    return false;
} <div class="clear">
                 
              
              
        
            </div>
Copy after login
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