Wednesday 15 July 2015

php - Mixing Associative Arrays -


I have a function that comes from DB, which is all data from users of different tables. I am using PDO and returns the returns for each SELECT a associative array .

What I want to do is return only one array with all the information taken from the DB function, then with a large array with all user data.

The problem is that I'm getting errors because I can not just merge more arrays or I ' string conversion error .

How do I combine all the in a coordinated array ? / P>

  function userInfo ($ uID, $ pdo) {$ user = PDOselect ('user', '*', array ('id_user' = & gt; $ uID), $ pdo); $ User_registration = PDOselect ('user_registration', '*', array ('id_user' = & gt; $ uID), $ pdo); $ User_addfields_anagraphic = PDOselect ('user_addfields_anagraphic', '*', array ('id_user' = & gt; $ uID), $ pdo); $ User_addfields_legal = PDOselect ('user_addfields_legal', '*', array ('id_user' => $ uID), $ pdo); $ User_addfields_public = PDOselect ('user_addfields_public', '*', array ('id_user' = & gt; $ uID), $ pdo); Returns .....}    

You use + array_merge () to insert operators or two arrays.

+ operator: adds elements of the second array to the first array. For the keys present in both arrays, the elements that match the second array will not be taken care of.

array_merge (): Add elements of the second array to the first array. If there are elements in both arrays with the same key, the behavior of this function depends on it:

  1. The key is numeric: element will be added and the key Nominated
  2. The key is a string: The latter values ​​will change the first one. Example:
      // array ([color] => red, [0] = & gt; 2, [1] = & Gt; 4) $ Array1 = array ("color" = & gt; "red", 2, 4); // array ([0] => A, [1] => B, [color] => green, [size] => cycle, [2] => 4 $ array2 = Using the array = array ("A", "b", "color" => gt; green "," shape "= & gt;" circle ", 4);   

    (+): $ result = $ array1 + $ array2; // $ result: array ([color] => red, [0] = & gt; 2, [ 1] = & gt; 4, [shape] = & gt circle; [2] = & gt; 4)

    Use (array_merge):

     < Code> $ result = array_merge ($ array1, $ array2); / / $ result: array ([color] = & gt; green, [0] = & gt; 2, [1] => 4, [2] ] => One, [3] => B, [shape] = cycle, [4] = & gt; 4)   

    I hope this Useful for you Ga.

No comments:

Post a Comment