Jump to content

creating a multidimensional array


jimfog

Recommended Posts

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

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

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

 

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

Υes,that worked thanks....

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...