Jump to content

trying to create a 2d array on the fly


Go to solution Solved by Jacques1,

Recommended Posts

Hi

I have a data set that is coming from a db table like this

 

science, p1, some text

science, p2, some more text

maths, p1, some text

maths, p2, some more text

 

All together that are going to be around 200 entries. These are subject descriptions/levels that will be written out into documents for pupils class reports.

 

I am trying to create an array that looks like this:

 $descriptors = [
     'science' => [
        'p1' => 'text',
        'p2' => 'more text'
        ],
      'maths; => [
          'p1'=> 'text',
        'p2'=> 'more text'.
           ]
etc
etc
  ];

At the moment i have an array $subject_list which goes 'Speaking', 'Science', 'Maths' etc

 

I loop through the $subject_list and perform a query which returns the subject, the grade(the p1 bit) and the text

 

How do I turn this data in the 2d array shown above. I am trying to put an array with a name into an array but I can't figure it out.

 

This is as far as I could get

$desc[]=array('subject'=>$sub, 'grade'=>$row['grades'], $row['descriptor']);

but this is creating an array which has about 200 arrays in it - all with 3 items - not what I want

 

How can I do this please

 

 

Link to comment
https://forums.phpfreaks.com/topic/301122-trying-to-create-a-2d-array-on-the-fly/
Share on other sites

that seems to work!!! I have been fiddling around for ages, and getting marked down on stackoverflow for being thick!

 

I would like to understand how it works - So its doing $descriptors['Science' ]['P2']='text text text' - why doesn't it create a brand new 'Science array' for every level/descriptor each time - somehow it seems to cleverly know that the array science has already been created and add a new key=>value pair within the science array

 

BTW thanks so much!

When $descriptors already has a “Science” key, then $descriptors['Science'] simply refers to the corresponding value. In your case the value happens to be an array as well, so you can set the value of the “P2” key within that array.

 

In other words,

$descriptors['Science']['P2'] = 'text text text';

is roughly equivalent to

$science = $descriptors['Science'];
$science['P2'] = 'text text text';

Reality is a bit more complex, of course, because PHP automatically creates the subarrays when they don't exist yet.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.