3

My problem is this: I'm creating an Array in order to store these 2 types of 'refinement'. However, what is happening is as the information is collected from the database, each 'refinement' is assigned to it's own specific entry when the arrays are created within the while loop.

while($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
etc...
}

So for instance, the 1st array would be a reference to 'Die Hard' and the 2nd, 'Breaking Bad' and the 3rd, 'Greys Anatomy'. What i'm trying to achieve is to merge them into 1 single array.

Array 
      ( 
      [genreType] => Action
      [mediaType] => Film
      ) 

Array 
      ( 
      [genreType] => Action
      [mediaType] => TV
      )  

Array 
      ( 
      [genreType] => Drama
      [mediaType] => TV
      )

Thanks for any help.

2
  • have you tried creating a temp variable array outside of the loop and then using the loop to append the values into the array? Commented Apr 9, 2013 at 23:35
  • So you are trying to make something like this: Array([genretype1]=>Action [mediatype1]=>Film [genretype2]=>Action etc? Commented Apr 9, 2013 at 23:36

3 Answers 3

4

Try looking at this, http://php.net/manual/en/function.array-merge.php

<?php
    $array1 = array("color" => "red", 2, 4);
    $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
    $result = array_merge($array1, $array2);
    print_r($result);
?>

OUTPUT

Array (
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)
Sign up to request clarification or add additional context in comments.

Comments

2

Why can't you just use array_merge? from the php docs http://php.net/manual/en/function.array-merge.php

Comments

0
while{$row...
{
    $movies[] = $row;
    //OR
    $tmp['genreType'] = $row['genre'];
    //and the like.....
    $movies[] = $tmp;
}

Resulting in

$movies = array(
             array(
               "title"=>"Die Hard", 
               "genreType"=>"Action", 
               "mediaType"=>"Film"
             ),
             array(
               "title"=>"some other movie",
               "genreType"=>"Comedy",
               "mediaType"=>"TV"
             )
           );

like this?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.