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

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

WBOY
WBOYOriginal
2016-06-13 13:37:591197browse

怎样在数组中插入一个值
$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>
Statement:
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