jimfog Posted August 26, 2014 Share Posted August 26, 2014 I have created a function that fetches some data from the db and these populate an array...what I want to do is add some more data to it and create a multidimensional array,,,,these second data are also fetched from the db.Take a look at the code and I will explain further: { while ($appdetails = $result->fetch_object()){ $names[]=array('name'=>$appdetails->name,'apID'=>$appdetails->apID,'start'=>$appdetails->startDate,'end'=>$appdetails->endDate,'staffID'=>$appdetails->staffID ,'origin'=>$appdetails->apps_origin); } while($service = $result1->fetch_object()) { $names[]=array('service'=>$service->serviceID); } The second data are in the second while loop. &service can me more than one row.As the syntax is now an array is created where 3 arrays are contained in it Let us suppose $service is 2 rows-in this example. One array contains $appdetails data,a second contains the results of one $service row and a third array contains the result of another $service row The goal here is that the names array gets another key,named services, and in it we have the result of the 2 services rows. So far,by trying various syntax either I will achieve the above,either I will get the overwriting problem. What do you propose? Link to comment Share on other sites More sharing options...
JamesB Posted August 26, 2014 Share Posted August 26, 2014 if both result objects contain the same amount of rows and each row relates to the other with the same row index, you could use this: $index = 0;while($service = $result1->fetch_object()){$names[$index]['service'] =$service->serviceID;$index++;} Link to comment Share on other sites More sharing options...
JamesB Posted August 26, 2014 Share Posted August 26, 2014 Or infact if you are after a 3 dimensional array: $index = 0;while($service = $result1->fetch_object()){$names[$index]['services'][] = $service->serviceID;$index++;} Link to comment Share on other sites More sharing options...
thescientist Posted August 26, 2014 Share Posted August 26, 2014 If you can't do it within the query, you can build up the array up first $details; while ($appdetails = $result->fetch_object()){ $details = array( 'name' => $appdetails->name, 'apID'=>$appdetails->apID, 'start'=>$appdetails->startDate, 'end'=>$appdetails->endDate, 'staffID'=>$appdetails->staffID, 'origin'=>$appdetails->apps_origin );} while($service = $result1->fetch_object()){ $details['service'] = $service->serviceID;} $names[] = $detail; Link to comment Share on other sites More sharing options...
jimfog Posted August 26, 2014 Author Share Posted August 26, 2014 If you can't do it within the query, you can build up the array up first $details; while ($appdetails = $result->fetch_object()){ $details = array( 'name' => $appdetails->name, 'apID'=>$appdetails->apID, 'start'=>$appdetails->startDate, 'end'=>$appdetails->endDate, 'staffID'=>$appdetails->staffID, 'origin'=>$appdetails->apps_origin );} while($service = $result1->fetch_object()){ $details['service'] = $service->serviceID;} $names[] = $detail; I tried your code and the problem is that overwriting takes place. While result of the query related to the $service brings 2 rows from the db(with values "1" and "2")what gets passed to the array is only "2". Here is the result of var_dump to see for your self: array(1) { [0]=> array(7) { ["name"]=> string(7) "Kostas " ["apID"]=> string(2) "16" ["start"]=> string(19) "2014-08-27 12:30:00" ["end"]=> string(19) "2014-08-27 15:00:00" ["staffID"]=> string(1) "5" ["origin"]=> string(7) "backend" ["service"]=> string(1) "2" }} Take a look at the key service and its value.It is "2". The goal is that the key service contains another array that holds the values fetched from the db.In this example it is "1" and "2". It might as well be more values than two....but I think you have understood the nature of the problem. Thanks Link to comment Share on other sites More sharing options...
JamesB Posted August 26, 2014 Share Posted August 26, 2014 to make the 'service' key an array, change $details['service'] = $service->serviceID; to $details['service'][] = $service->serviceID; Link to comment Share on other sites More sharing options...
jimfog Posted August 27, 2014 Author Share Posted August 27, 2014 Υes,that worked thanks.... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now