maxelct Posted March 31, 2016 Share Posted March 31, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301122-trying-to-create-a-2d-array-on-the-fly/ Share on other sites More sharing options...
Solution Jacques1 Posted March 31, 2016 Solution Share Posted March 31, 2016 Assuming the subject/grade combinations are unique: $descriptors[$row['subject']][$row['grades']] = $row['descriptor']; You need better variable names than "descriptors" and "row", though. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301122-trying-to-create-a-2d-array-on-the-fly/#findComment-1532640 Share on other sites More sharing options...
maxelct Posted March 31, 2016 Author Share Posted March 31, 2016 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! Quote Link to comment https://forums.phpfreaks.com/topic/301122-trying-to-create-a-2d-array-on-the-fly/#findComment-1532644 Share on other sites More sharing options...
Jacques1 Posted March 31, 2016 Share Posted March 31, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301122-trying-to-create-a-2d-array-on-the-fly/#findComment-1532646 Share on other sites More sharing options...
maxelct Posted March 31, 2016 Author Share Posted March 31, 2016 thanks again - I get it! Quote Link to comment https://forums.phpfreaks.com/topic/301122-trying-to-create-a-2d-array-on-the-fly/#findComment-1532649 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.