Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中合并具有相同id的两个数组?_Php_Arrays_Merge - Fatal编程技术网

如何在PHP中合并具有相同id的两个数组?

如何在PHP中合并具有相同id的两个数组?,php,arrays,merge,Php,Arrays,Merge,可能重复: 这是aray我的数组我是如何将数组与他的“panid”合并的。 相同的“panid”请参见数组和所需输出 在数组下方显示2个数组包含相同的“panid”,但其成分不同。 所以我将把这两个数组合并成一个数组,并合并他的成分 Array ( [0] => stdClass Object ( [userid] => 62 [panid] => 5 [recipeid] => 13 [i

可能重复:

这是aray我的数组我是如何将数组与他的“panid”合并的。 相同的“panid”请参见数组和所需输出

在数组下方显示2个数组包含相同的“panid”,但其成分不同。 所以我将把这两个数组合并成一个数组,并合并他的成分

Array
(
    [0] => stdClass Object
    (
        [userid] => 62
        [panid] => 5
        [recipeid] => 13
        [ingredients] => 10 Kilos,1 Gram
        [panname] => XYZ
    )

    [1] => stdClass Object
    (
        [userid] => 62
        [panid] => 5
        [recipeid] => 12
        [ingredients] => 150 Gram,15 Pcs
        [panname] => XYZ
    )

    [2] => stdClass Object
    (
        [userid] => 62
        [panid] => 3
        [recipeid] => 15
        [ingredients] => 100 Gram,10 Pcs
        [panname] => ABC
    )
)
要求输出:

Array
(
    [0] => stdClass Object
    (
        [userid] => 62
        [panid] => 5            
        [ingredients] => 10 Kilos,1 Gram,150 Gram,15 Pcs
        [panname] => XYZ
    )

    [1] => stdClass Object
    (
        [userid] => 62
        [panid] => 3          
        [ingredients] => 100 Gram,10 Pcs
        [panname] => ABC
    )
)

PHP有一些很好的数据结构类可用于此目的。扩展类以覆盖attach方法,您可以随意更新食谱列表。您可能需要比我做更多的健全性检查,但这里有一个相当简单的示例:

class RecipeStorage extends SplObjectStorage
{
    /**
     * Attach a recipe to the stack
     * @param object $recipe
     * @return void
     */
    public function attach(object $recipe)
    {
        $found = false;
        foreach ($this as $stored => $panid) {
            if ($recipe->panid === $panid) {
                $found = true;
                break;
            }
        }

        // Either add new recipe or update an existing one
        if ($found) {
            $stored->ingredients .= ', ' . $recipe->ingredients
        } else {
            parent::attach($recipe, $recipe->panid);
        }
    }
}
您可以使用中提供的所有方法并添加新配方,而无需考虑合并

$recipeBook = new RecipeStorage;
$recipeBook->attach($recipe1);
$recipeBook->attach($recipe2);

foreach ($recipeBook as $recipe => $id) {
    echo 'Pan Name: ' . $recipe->panname;
}

这是完全未经测试的,但它应该让您知道如何继续。

但它引发了错误:可捕获致命错误:传递给RecipeStorage::attach()的参数1必须是object的实例,给定stdClass的实例,在第63行的/home6/panchsof/public_html/dinnerrush/recipe.php中调用,并在第63行的/home6/panchsof/public_html/dinnerrush/recipe.php中定义10@GKumar00我说它没有经过测试。你必须让它适合你的数据;我不能一直握着你的手…很抱歉。可以再见,谢谢。