将PHP中的对象数组转换为关联数组以填充HTML选择的更好方法
假设我有一个具有以下结构的对象数组:将PHP中的对象数组转换为关联数组以填充HTML选择的更好方法,php,php-5.3,php-5.5,php-5.4,php-5.6,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 [cate
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 select:
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;
}
是否有更好/更简洁的方法来实现这一点,以便数组键是property
category\u id
,值是propertycategory\u title
?如果您使用的是PHP 5.5+,则可以使用专门为此设计的全新功能:
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,使用纯PHP实现:
数组($categories,function(&$value){$value=(数组)$value;})`试试这个
array\u映射(函数($yourArray){return$array->category\u title;},$yourArray)代码>对于新数组,您必须再次使用循环来迭代项,以了解为什么要这样做;虽然您可以在发布的循环中填充html;为什么要分两步来做?应该做得很简单。(尽管在您的示例中,如果不使用PHP7,您可能需要将对象转换为数组)我可能应该澄清,数组中的索引需要是category_id属性
/**
* 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;
}
}