views:

27

answers:

1

I want to change the format of this array to match another one. One was pulled from a database, and the other was made using space delimted user input and then explode();

So here are the arrays I want to change, this is from the database (mysql_fetch_array). It grabs the single field from all rows.

Array ( [name] => daniel )

Array ( [name] => alex )

Array ( [name] => richard )

And here is what I want it to look like. This was the output of the user submitted values, space delimted and using PHPs explode() function.

Array ( [0] => daniel [1] => alex [2] => david )

What I want to be able to do is have these in the same format so that I can compare the two. The end goal is to be able to compare the two arrays (user input and database results), and create a final array containing only values that the user has inputted that the database doesn't already contain.

So using the data above this would be my final array:

Array ( [0] => david )

I would really appreciate help with the first bit, and if anyone else has a better way to achieve the end goal that would be a great extra bonus! (I get the feeling it might be easier to do this with SQL queries but I'm really confusing myself with these arrays)

Thanks!

+1  A: 
array_diff($user_array, $database_array);

You can construct the $database_array like this:

$database_array = array();
//assuming each db record has only one value
//while there are still results in the db, do:
    $database_array[] = reset($fetched_record);

See array_diff.

Artefacto
Thanks alot! Did exactly what I wanted!
Danny