0

我有一个对象数组示例:

更大的套装:

Array(
    [0] stdClass Object(
                        [ID] => 1,
                        [name] => monkey,
                        [sub] => help
                       ),
    [1] stdClass Object(
                        [ID] => 1,
                        [name] => tooth,
                        [sub] => tip
                       ),
    [2] stdClass Object(
                        [ID] => 1,
                        [name] => who,
                        [sub] => knows
                       ),
    )

较小的集

Array(
    [0] stdClass Object(
                        [ID] => 1,
                        [name] => monkey,
                        [sub] => help
    )

期望的结果:

Array(
    [0] stdClass Object(
                        [ID] => 1,
                        [name] => monkey,
                        [sub] => help,
                        [selected] => yes
                       ),
    [1] stdClass Object(
                        [ID] => 1,
                        [name] => tooth,
                        [sub] => tip,
                        [selected] => no
                       ),
    [2] stdClass Object(
                        [ID] => 1,
                        [name] => who,
                        [sub] => knows,
                        [selected] => no
                       ),
    )

我试图玩的似乎不起作用的是

    foreach($result_static as $stock)
    {
        foreach($result_memb as $memb_choice)
        {
            $stock->selected = "false";
            //echo $stock->name .' == '. $memb_choice->name.'<br>';
            if($stock->name == $memb_choice->name)
            {
                $stock->selected = "yes";
            }
        }
        $output[] = $stock;
    }

这似乎与任何实际结果都不匹配,并且从我收集的内容来看,我的 foreach 逻辑是错误的,因为它重新循环了第二个循环很多次,或者第一个循环以这种方式与另一个循环在一起,每个人的名字永远不会像我希望的那样分别匹配。因此,我正在尝试寻找有关如何更好地解决此问题的想法,并希望一些更优化的东西会很好,但我会采取我目前能得到的。

4

2 回答 2

1

尝试这个

foreach($result_static as $stock)
    {
        $stock->selected = "false";
        foreach($result_memb as $memb_choice)
        {
            if($stock->name == $memb_choice->name)
            {
                $stock->selected = "yes";
            }
        }
        $output[] = $stock;
    }
于 2013-05-04T07:01:58.570 回答
0

我无法在您的代码中找到错误,也许它在其他地方?

我试图重新创建它,下面的代码运行并给出了所需的输出:

class Item
{
    public $Id;
    public $Name;
    public $Sub;
    public $Selected;

    public function __construct($id, $name, $sub)
    {
        $this->Id = $id;
        $this->Name = $name;
        $this->Sub = $sub;
    }
}

$result_static  = array(
    new Item(1, 'monkey', 'help'),
    new Item(1, 'tooth', 'tip'),
    new Item(1, 'who', 'known')
);

$result_memb  = array(new Item(1, 'monkey', 'help'));

$output = array();

foreach($result_memb  as $memb_choice)
{
    foreach($result_static  as $stock)
    {
        $stock->Selected = "false";

        if($stock->Name == $memb_choice->Name)
        {
            $stock->Selected = "yes";
        }
        $output[] = $stock;
     }
 }

 print_r($output);

这给出了输出:

Array
(
    [0] => Item Object
        (
            [Id] => 1
            [Name] => monkey
            [Sub] => help
            [Selected] => yes
        )

    [1] => Item Object
        (
            [Id] => 1
            [Name] => tooth
            [Sub] => tip
            [Selected] => false
        )

    [2] => Item Object
        (
            [Id] => 1
            [Name] => who
            [Sub] => known
            [Selected] => false
        )
)
于 2013-05-04T07:05:54.727 回答