3

I'm learning PHP and I'm struggling with arrays. I'm trying to merge two arrays together but combine their data together like so.

Array $instruction1 contains names of the recipe instructions.

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

The second array has the instructions as to what you do when prepping, cooking, and serving.

[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.

When I combine the two arrays I get

[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.

Here is the actual output.

    (
        [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!!
                    )
    
            )
    
    )

Here is what I hope to accomplish.

        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!!
            )
    
    )

My code

               $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);  
            }

Hope you guys can help. I have spent 12+ hours trying to figure this out. Reading and learning.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
Jacob
  • 245
  • 3
  • 9

1 Answers1

2

i think you have same length for all array for steps and instructions here you need to store that two array in another array with the particular elements of those array with same key

//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 Nayka
  • 336
  • 2
  • 7
  • Thanks Akshay! I will try this out tomorrow and let you know. I really appreciate you taking the time to help me with this. – Jacob Dec 21 '20 at 10:59
  • Askhay, you have my overwhelming gratitude! What you gave me worked like a charm. I really appreciate your help. – Jacob Dec 21 '20 at 21:56