Return to Snippet

Revision: 21645
at December 17, 2009 10:16 by iloveitaly


Initial Code
function flatten_array($arrayList, $key) {
	$newArray = array();

	foreach($arrayList as $array) {
		$index = $array[$key];
		unset($array[$key]);
		$newArray[$index] = $array;
	}

	return $newArray;
}

Initial URL

                                

Initial Description
Before:
`
Array
(
    [0] => Array
        (
            [total_sessions] => 24
            [study_type_id] => 1
        )

    [1] => Array
        (
            [total_sessions] => 24
            [study_type_id] => 2
        )

    [2] => Array
        (
            [total_sessions] => 20
            [study_type_id] => 3
        )

    [3] => Array
        (
            [total_sessions] => 10
            [study_type_id] => 4
        )

    [4] => Array
        (
            [total_sessions] => 10
            [study_type_id] => 5
        )

    [5] => Array
        (
            [total_sessions] => 10
            [study_type_id] => 6
        )

    [6] => Array
        (
            [total_sessions] => 10
            [study_type_id] => 7
        )

    [7] => Array
        (
            [total_sessions] => 8
            [study_type_id] => 10
        )

    [8] => Array
        (
            [total_sessions] => 8
            [study_type_id] => 12
        )

)
`
After:
`
Array
(
    [1] => Array
        (
            [total_sessions] => 24
        )

    [2] => Array
        (
            [total_sessions] => 24
        )

    [3] => Array
        (
            [total_sessions] => 20
        )

    [4] => Array
        (
            [total_sessions] => 10
        )

    [5] => Array
        (
            [total_sessions] => 10
        )

    [6] => Array
        (
            [total_sessions] => 10
        )

    [7] => Array
        (
            [total_sessions] => 10
        )

    [10] => Array
        (
            [total_sessions] => 8
        )

    [12] => Array
        (
            [total_sessions] => 8
        )
)
`

Initial Title
Flatten Array Using Index

Initial Tags
php, array

Initial Language
PHP