Several small way to rebuild PHP array index

The PHP array is actually an ordered map. A mapping to values associated keys type. This type is optimized in many ways, so you can use it as a real array, or list (vector), hash table (an implementation is mapped), dictionary, collection, stack, queue and probably more. Since the array element can also be another array, multi-dimensional arrays, and the tree structure are allowed.

Types of

  1. Indexed array, from array from 0,1
  2. Associative array, i.e. custom key

Reconstruction

  1. array_column(arr,key,index);

    In the key pass NUll, which can rebuild a new array is an indexed array

    $a = array(
        'a' => 'baidu',
        'b' => 'qq',
        'c' => 'nho',
    );
    print_r(array_column($a,null)); //echo array('baidu','qq','nho')
  2. array_merge($arr1,$arr2);

    Method can be used not only key index of the array starts at 0, to the associated index is not valid, only passing a current index of the array, it will be the current array index rebuild

    $b = [
            1=>'baidu',
            '1123',
            'nho',
        ];
    print_r(array_merge($b));    //echo array('baidu','1123','nho')
  3. array_splice($arr,$start,$length,$replacement);

    This method returns the specified segment array, $ replacement will be set to true numerical index rebuild

    $b = array(
        '1' => 'baidu',
        '2' => 'qq',
        '3' => 'nho',
        '4' => 'vv'
    );
    $count = count($b);
    print_r(array_splice($b,0,$count,true)); //echo array('baidu','qq','nho','vv')

Guess you like

Origin www.cnblogs.com/Daneil/p/11605852.html