0

将对象数组映射到对象的键值数组的最简单方法是什么,其中键是对象的某些属性?

例如:

class Cartoon
{
  public $title;
  public function __construct($title)
  {
    $this->title = $title;
  }
}

$cartoons = array(new Cartoon('Tom and Jerry'), new Cartoon('Cheburashka'));

$mappedCartoons = array();

foreach ($cartoons as $cartoon)
{
  $mappedCartoons[$cartoon->title] = $cartoon;
}

print_r ($mappedCartoons);

PS。我想知道是否可以处理迭代和额外的变量$mappedCartoons

4

3 回答 3

4

你可以array_reduce用来实现你的目标。

<?php

$cartoons = array_reduce($cartoons, function($result, $cartoon) {
  $result[$cartoon->title] = $cartoon;
  return $result;
}, array());

?>

这个想法不是我的,在这里找到:http ://www.danielauener.com/howto-use-array_map-functionality-on-an-associative-array-changing-keys-and-values/

于 2012-11-21T14:25:34.270 回答
0

如果您必须使用基本数组,您可以在创建它们时简单地键入键:

$cartoons = array(
    'Tom and Jerry' => new Cartoon('Tom and Jerry'), 
    'Cheburashka' => new Cartoon('Cheburashka'),
);

或者,您可以创建实现各种数组/迭代相关接口并可以用作常规数组的容器对象:

class CartoonArray extends ArrayObject {

    public function __construct($input, $flags = 0, $iterator_class = "ArrayIterator") {
        parent::__construct(array(), $flags, $iterator_class);
        // since the original ArrayObject implemented in C and they don't it won't pick up our overriden ossetSet we have to type it out here explicitly
        foreach ($input as $value) {
            $this->offsetSet(null, $value);
        }
    }

    public function offsetSet($index, $value) {
        parent::offsetSet($value->title, $value);
    }
}

$cartoons = new CartoonArray(array(new Cartoon('Tom and Jerry'), new Cartoon('Cheburashka')));
$cartoons[] = new Cartoon('Sesame street');

print $cartoons['Tom and Jerry']->title;
unset($cartoons['Tom and Jerry']);

foreach ($cartoons as $k => $v) {
    print $k."\n";
}
于 2012-11-21T14:11:08.523 回答
0

我想不出不涉及迭代的方法。我可以想到一种不需要额外变量的方法:

$cartoons = array(new Cartoon('Tom and Jerry'), new Cartoon('Cheburashka'));
foreach( $cartoons as $key => $cartoon )
{
    $cartoons[ $cartoon->title ] = $cartoon;
    unset( $cartoons[ $key ] );
}

var_dump( $cartoons );

但是,注释的文档foreach

由于 foreach 依赖于内部数组指针,因此在循环内更改它可能会导致意外行为。

我不完全确定这也适用于我的示例。也许其他人可以在这里插话。

为了安全起见,也许这种替代方案更合适,因为它在迭代之前$len以 的初始长度开始$cartoons

$cartoons = array(new Cartoon('Tom and Jerry'), new Cartoon('Cheburashka'));
for( $i = 0, $len = count( $cartoons ); $i < $len; $i++ )
{
    $cartoon = $cartoons[ $i ];
    $cartoons[ $cartoon->title ] = $cartoon;
    unset( $cartoons[ $i ] );
}

var_dump( $cartoons );

此示例假定初始$cartoons具有“正确”的顺序编号键0,但从 开始。

于 2012-11-21T14:12:38.433 回答