将PHP中的对象数组转换为关联数组以填充HTML select

时间:2016-03-17 13:08:53

标签: php php-5.3 php-5.5 php-5.4 php-5.6

假设我有一组具有以下结构的对象:

Array ( 
    [0] => stdClass Object ( 
        [category_id] => 5 
        [category_title] => Meetings 
        [category_slug] => meetings 
        [category_summary] => This is the meetings category 
        [category_user_id] => 1 
        [category_created] => 2016-03-17 12:41:42 
        [category_modified] => 2016-03-17 12:41:42 
        [category_status] => 1 
        [category_deleted] => NULL
    ),
    [1] => stdClass Object ( 
        [category_id] => 9 
        [category_title] => Seminars 
        [category_slug] => seminars
        [category_summary] => This is the seminars category 
        [category_user_id] => 1 
        [category_created] => 2016-03-17 12:41:42 
        [category_modified] => 2016-03-17 12:41:42 
        [category_status] => 1 
        [category_deleted] => NULL
    ),
    [2] => stdClass Object ( 
        [category_id] => 15 
        [category_title] => Sporting Events 
        [category_slug] => sporting-events
        [category_summary] => This is the sporting events category 
        [category_user_id] => 1 
        [category_created] => 2016-03-17 12:41:42 
        [category_modified] => 2016-03-17 12:41:42 
        [category_status] => 1 
        [category_deleted] => NULL
    )
)

我想将其转换为以下结构,用以下内容填充HTML选项:

Array ( 
    [5] => Meetings,
    [9] => Seminars,
    [15] => Sporting Events
)

我总是这样做是循环遍历原始数组并构建一个新的数组:

// $categories contains the original array of objects
$select_categories = array();
foreach($categories as $category) {
    $select_categories[$category->category_id] = $category->category_title;
}

是否有更好/更简洁的方法来执行此操作,以便数组键是属性category_id,值是category_title

1 个答案:

答案 0 :(得分:0)

如果您使用的是PHP 5.5+,则可以使用专为此设计的强大的新array_column()功能:

php > $array = [
php >     0 => [
php >         'category_id' => 5,
php >         'category_title' => 'Meetings',
php >         'category_slug' => 'meetings',
php >         'category_summary' => 'This is the meetings category',
php >     ],
php >     1 => [
php >         'category_id' => 9,
php >         'category_title' => 'Seminars',
php >         'category_slug' => 'seminars',
php >         'category_summary' => 'This is the seminars category',
php >     ],
php >     2 => [
php >         'category_id' => 15,
php >         'category_title' => 'Sporting Events',
php >         'category_slug' => 'sporting-events',
php >         'category_summary' => 'This is the sporting events category',
php >     ],
php > ];
php >

php > var_dump(array_column($array, 'category_title', 'category_id'));
array(3) {
  [5]=>
  string(8) "Meetings"
  [9]=>
  string(8) "Seminars"
  [15]=>
  string(15) "Sporting Events"
}
php >

对于低于5.5的PHP,someone on PHP.net在纯PHP中实现它:

/**
 * Provides functionality for array_column() to projects using PHP earlier than
 * version 5.5.
 * @copyright (c) 2015 WinterSilence (http://github.com/WinterSilence)
 * @license MIT
 */
if (!function_exists('array_column')) {
    /**
     * Returns an array of values representing a single column from the input
     * array.
     * @param array $array A multi-dimensional array from which to pull a
     *     column of values.
     * @param mixed $columnKey The column of values to return. This value may
     *     be the integer key of the column you wish to retrieve, or it may be
     *     the string key name for an associative array. It may also be NULL to
     *     return complete arrays (useful together with index_key to reindex
     *     the array).
     * @param mixed $indexKey The column to use as the index/keys for the
     *     returned array. This value may be the integer key of the column, or
     *     it may be the string key name.
     * @return array
     */
    function array_column(array $array, $columnKey, $indexKey = null)
    {
        $result = array();
        foreach ($array as $subArray) {
            if (!is_array($subArray)) {
                continue;
            } elseif (is_null($indexKey) && array_key_exists($columnKey, $subArray)) {
                $result[] = $subArray[$columnKey];
            } elseif (array_key_exists($indexKey, $subArray)) {
                if (is_null($columnKey)) {
                    $result[$subArray[$indexKey]] = $subArray;
                } elseif (array_key_exists($columnKey, $subArray)) {
                    $result[$subArray[$indexKey]] = $subArray[$columnKey];
                }
            }
        }
        return $result;
    }
}