简体   繁体   中英

Values From Multi-Row MySQL To Single Array

Fetching multi-row data from MySQL database and trying to convert the results into a single array. Since the data is coming from a custom function where modifying it will break many other things, I can't change the way the way it is fetched so must process after retrieval using PHP. This is a sample:

Array
(
    [0] => Array
        (
            [FieldLabel] => 
            [FieldName] => ID
            [FieldValue] => $ID
            [FieldType] => 0
        )

    [1] => Array
        (
            [FieldLabel] => Name
            [FieldName] => Name
            [FieldValue] => $FieldName
            [FieldType] => 1
        )

)

Looking for something like this with only all the values in a single array but with the variables populated:

Array('','ID',$ID,0,'Name','Name',$FieldName,1)

I found this little function in another post that seemed would at lease create the array it but unfortunately it does not and I don't know enough about array manipulation to be able to sort it out. Any ideas?

function array2Recursive($array)  {
    $lst = array();
    foreach( array_keys($array) as $k ) :
        $v = $array[$k];
        if (is_scalar($v)) :
            $lst[] = $v;
        elseif (is_array($v)) :
            $lst = array_merge($lst,array2Recursive($v));
        endif;
    endforeach;
    return array_values(array_unique($lst));
}

On way to solve this could be to use a foreach to loop through the items and add them to an array:

$result = [];
foreach ($array as $a) {
    foreach($a as $item) {
        $result[] = $item;
    }
}

print_r($result);

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM