在 PHP 中将两个数组合并在一起?

时间:2020-12-21 01:47:38

标签: php arrays

我正在学习 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 多个小时试图弄清楚这一点。阅读和学习。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

$combined = array_merge($instruction1, $second);

https://www.php.net/manual/en/function.array-merge.php

相关问题