vivar Posted August 3, 2009 Share Posted August 3, 2009 Hi, I am using php 5.2.6 with MySQL 5. I want to encode the results of the code below with JSON. I know that I need to use json_encode but I am having a problem setting up an array for this: <?php // see if our query returned any data before processing (query is taken care of in the model) if (count($adminUnit)){ // $myLetter is an array returned from my Model (connecting to MySQL) // it holds the following data: A,C,G,I,O // loop through each letter that has a section in the database foreach ($myLetter as $id => $letter){ echo $letter['section']; echo '<br/>'; // $adminUnit is an array returned from my Model (connecting to MySQL) // it holds the following data: "A, Academic Dean", "A, Academic Systems", "G,General Administration" etc. // loop through $adminUnit foreach ($adminUnit as $key => $list){ // now print out the data IF the section matches the outer loops section (ie. 'A' =='A') if (strtoupper( $list['section']) == $letter['section']){ echo $list['description']; echo '<br/>'; } } } } ?> This gives me the following output: A Academic Dean Academic Systems Administrative Services C Continuing Education Creative & Applied Arts G General Administration I Information Systems and Technology Instructional Development O Other .... What I want to do is to take the data shown above and place it in an multidimensional array so that for example 'A' is the key with the values: Academic Dean,Academic Systems,Administrative Services etc. then json_encode it. So, I think it would look something like this: [arrayresults] => Array ( [A] => Array ( [item1] => 'Academic Dean' [item2] => 'Academic Systems' [item3] => 'Administrative Services' ) [C] => Array ( .......etc. ) I can't figure out how to place my results into a multidimensional array. Any help would be appreciated. Regards, vivar Link to comment https://forums.phpfreaks.com/topic/168720-php-key-value-json/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.