Convert array into simple 1 level array in PHP

后端 未结 4 750
遇见更好的自我
遇见更好的自我 2021-01-14 20:16

A simple thing to do, but I forgot how to convert

Array
(
    [0] => Array
        (
            [hashcode] => 952316176c1266c7ef1674e790375419
                


        
相关标签:
4条回答
  • 2021-01-14 20:57

    Try this :

    $array  = array(array("test"=>"xcxccx"),array("test"=>"sdfsdfds"));
    
    $result = call_user_func_array('array_merge', array_map("array_values",$array));
    
    echo "<pre>";
    print_r($result);
    

    Output:

    Array
    (
        [0] => xcxccx
        [1] => sdfsdfds
    )
    
    0 讨论(0)
  • 2021-01-14 20:58
    $source = array(
        array(
            'hashcode' => '952316176c1266c7ef1674e790375419'
        ),
        array(
            'hashcode' => '5b821a14c98302ac40de3bdd77a37ceq'
        )
    );
    
    $result = array();
    array_walk($source, function($element) use(&$result){
        $result[] = $element['hashcode'];
    });
    
    echo '<pre>';
    var_dump($result);
    
    0 讨论(0)
  • 2021-01-14 20:59

    I know this is premature but since this is coming soon I figured I throw this out there. As of (the not yet released) PHP 5.5 you can use array_column():

     $hashcodes = array_column($array, 'hashcode');
    
    0 讨论(0)
  • 2021-01-14 21:01

    A good ol' loop solves :)

    <?php
    
    $array = array(
        array( 'hashcode' => 'hash' ),  
        array( 'hashcode' => 'hash2' ), 
    );
    
    $flat = array();
    
    foreach ( $array as $arr ) {
        $flat[] = $arr['hashcode'];
    }
    
    echo "<pre>";
    
    print_r( $flat );
    
    ?>
    
    0 讨论(0)
提交回复
热议问题