在 PHP 中如何将两个数组合并?

3

我正在学习PHP,对于数组有些困惑。我正在尝试将两个数组合并在一起,但是要将它们的数据结合在一起,就像这样。

数组$instruction1包含食谱说明的名称。

[0] = Prep
[1] = Cook
[2] = Serve

第二个数组包含有关准备、烹饪和上菜时要做的说明。
[0] = In a large mixing bowl, crack 2 large eggs.
[1] = In a medium-sized frying pan or griddle, heat up a tablespoon of unsalted butter...
[2] = When done, transfer the french toast onto a plate.

当我合并这两个数组时,得到的结果为:
[0] = Prep
[1] = Cook
[2] = Serve
[3] = In a large mixing bowl, crack 2 large eggs.
[4] = In a medium-sized frying pan or griddle, heat up a tablespoon of unsalted butter...
[5] = When done, transfer the french toast onto a plate.

这是实际输出结果。

    (
        [0] => Array
            (
                [0] => Array
                    (
                        [name] => One
                    )
    
                [1] => Array
                    (
                        [name] =>  Two
                    )
    
                [2] => Array
                    (
                        [name] =>  Three
                    )
    
                [3] => Array
                    (
                        [text] => In a large mixing bowl, crack 2 large eggs. Season with a dash of salt. Whisk thoroughly. Pour 2 cups of fresh milk (2%, non-fat or whole milk) and a tablespoon of honey. Whisk again until well combined. 2. Slice or get a slice of bread and dip into the milk and egg mixture. Soak for 30seconds.
                    )
    
                [4] => Array
                    (
                        [text] => 
    In a medium-sized frying pan or griddle, heat up a tablespoon of unsalted butter over medium heat until it melts completely. Put the soaked bread and toast it for 3-5 minutes or until golden. Pour 1/4 cup of the mixture. Push the excess liquid to the center and onto the bread. Check the bread every 30-60 seconds. Flip the bread over and toast the other side for equal time
                    )
    
                [5] => Array
                    (
                        [text] =>  
    When done, transfer the french toast onto a plate. Top with your choice of fruits, with powdered sugar, butter, or Nutella. Then drizzle with your favorite maple syrup. Serve warm and enjoy!!
                    )
    
            )
    
    )

这是我希望实现的目标。
        Array
    (
        [0] => Array
            (
                [@type] => HowToStep
                [name] => prep
                [image] => some image link
                [text] => In a large mixing bowl, crack 2 large eggs. Season with a dash of salt. Whisk thoroughly. Pour 2 cups of fresh milk (2%, non-fat or whole milk) and a tablespoon of honey. Whisk again until well combined. 2. Slice or get a slice of bread and dip into the milk and egg mixture. Soak for 30seconds.
            )
    
        [1] => Array
            (
                [@type] => HowToStep
                [name] => cook
                [image] => some image link
                [text] => 
    In a medium-sized frying pan or griddle, heat up a tablespoon of unsalted butter over medium heat until it melts completely. Put the soaked bread and toast it for 3-5 minutes or until golden. Pour 1/4 cup of the mixture. Push the excess liquid to the center and onto the bread. Check the bread every 30-60 seconds. Flip the bread over and toast the other side for equal time
            )
    
        [2] => Array
            (
                [@type] => HowToStep
                [name] => serve
                [image] => some image link
                [text] =>  
    When done, transfer the french toast onto a plate. Top with your choice of fruits, with powdered sugar, butter, or Nutella. Then drizzle with your favorite maple syrup. Serve warm and enjoy!!
            )
    
    )

我的代码

               $explod1 = saswp_explod_by_semicolon($all_post_meta['saswp_recipe_instructions_name'.$schema_id][0]);{
                    foreach ($explod1 as $val1)
                $instruction1[] = array('name'=>$val1);     
                    // print_r ($instruction1);
                }
                $explod = saswp_explod_by_semicolon($all_post_meta['saswp_recipe_instructions_'.$schema_id][0]);
                


                // $test = array_push ($explod,$explod1);


                      foreach ($explod as $val)
                //    foreach ($explod1 as $val1)
                    {

                        $instruction[] = array('text'=>$val);
                                                //    array_unique ($instruction);  
                  }
                $test1[] = array_merge($instruction1,$instruction);
                print_r ($test1);  
            }

希望你们能帮忙。我已经花了12个小时以上来尝试解决这个问题。阅读和学习。

请展示您的 PHP 代码(以便我们知道哪些部分存在问题)。 - Ken Lee
1个回答

2

我认为你的步骤和指令数组长度应该相同。在这里,你需要将这两个数组存储在另一个数组中,并使用相同的键存储这些数组的特定元素。

//array that contain steps
$array1=array('Prep','Cook','Serve');
//array that contain instructions
$array2=array('In a large mixing bowl, crack 2 large eggs.','In a medium-sized frying pan or griddle, heat up a tablespoon of unsalted butter...','When done, transfer the french toast onto a plate.');
//calculate length of any one array because length is same
$length=count($array1);
//store that particular element in new array with keys 
$array3=array();
for($i=0;$i<$length;$i++){
    $array3[$i]['name']=$array1[$i];
    $array3[$i]['text']=$array2[$i];
    //you can add that another key and value here like $array3[$i]['image']='your image link';
}
//print the final array
echo '<pre>';
print_r($array3);

谢谢Akshay!我明天会试一下并告诉你结果。非常感谢你抽出时间来帮助我。 - Jacob
Askhay,你拥有我无尽的感激之情!你给我的东西非常好用。我真的很感谢你的帮助。 - Jacob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接