Solved

How can  I create multi dimensional array with key, value on this case?

Posted on 2015-02-19
4
76 Views
Last Modified: 2015-03-03
I am developing an application to create JSON formatted arrays as Web service. Attached is my generated format json array.

But I need to customize the array as  title , year included above the array key/value.

'title ' : 'Share'
- SHARE

'year' : '2014'
- 2014
0
Comment
Question by:Loganathan Natarajan
  • 3
4 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40619069
Can you provide the code you have? The JSON just shows us the result, so it's hard to tell where the generating code should change.
0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 40619250
following is the code that I have written:

foreach($resourcesCategories as $index=>$resource){
			if($resource['categoryType']=='Events'){
			
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['id'] = $resource['id'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['resourcesCategoryId'] = $resource['resourcesCategoryId'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['title'] = $resource['title'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['downloadURL'] = Yii::app()->getBaseUrl(true) . $downloadfolder . $resource['fileName'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['createdOn'] = $resource['createdOn'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['modifiedOn'] = $resource['modifiedOn'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['categoryType'] = $resource['categoryType'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['categoryTitle'] = $resource['categoryTitle'];
			}
			if($resource['categoryType']=='Policies'){
				
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['id'] = $resource['id'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['resourcesCategoryId'] = $resource['resourcesCategoryId'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['title'] = $resource['title'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['downloadURL'] = Yii::app()->getBaseUrl(true) . $downloadfolder . $resource['fileName'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['createdOn'] = $resource['createdOn'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['modifiedOn'] = $resource['modifiedOn'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['categoryType'] = $resource['categoryType'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['categoryTitle'] = $resource['categoryTitle'];
			}
			if($resource['categoryType']=='Both'){
				
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['id'] = $resource['id'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['resourcesCategoryId'] = $resource['resourcesCategoryId'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['title'] = $resource['title'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['downloadURL'] = Yii::app()->getBaseUrl(true) . $downloadfolder . $resource['fileName'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['createdOn'] = $resource['createdOn'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['modifiedOn'] = $resource['modifiedOn'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['categoryType'] = $resource['categoryType'];
				$resources['Events'][$resource['categoryTitle']][$resource['createdYear']][$index]['categoryTitle'] = $resource['categoryTitle'];
				
				
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['id'] = $resource['id'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['resourcesCategoryId'] = $resource['resourcesCategoryId'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['title'] = $resource['title'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['downloadURL'] = Yii::app()->getBaseUrl(true) . $downloadfolder . $resource['fileName'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['createdOn'] = $resource['createdOn'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['modifiedOn'] = $resource['modifiedOn'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['categoryType'] = $resource['categoryType'];
				$resources['Policies'][$resource['categoryTitle']][$resource['createdYear']][$index]['categoryTitle'] = $resource['categoryTitle'];
			}
		}
		
        if ($resources) {
            $response["result"] = true;
            $response["msg"] = "";
            $response["resources_data"] = $resources;
            echo CJSON::encode($response);
        } else {
            $response["result"] = false;
            $response["msg"] = RESOURCES_NOT_FOUND;
            $response["resources_data"] = "";
            echo CJSON::encode($response);
        }

Open in new window

0
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 0 total points
ID: 40627737
I found my own solution as created arrays to have like above structure.

$policyCategory = '';
		if($policiesCategories){
			foreach($policiesCategories as $policy)
			{
				$resource = $this->getCategoryYear($policy['id']);
				if($resource){
					$policyCategory[] = array(
							'id'=>$policy['id'],
							'categoryTitle'=>$policy['title'],
							'categoryData'=>$this->getCategoryYear($policy['id']),
					);
				} else {
					$policyCategory[] = array(
							'id'=>$policy['id'],
							'categoryTitle'=>$policy['title'],
					);
				}
				//print_r($category);
				/*$policyCategory[] = array(
						'id'=>$policy['id'],
						'categoryTitle'=>$policy['title'],
						'categoryData'=>$this->getCategoryYear($policy['id']),
				);*/
			}
		}

Open in new window

0
 
LVL 36

Author Closing Comment

by:Loganathan Natarajan
ID: 40641424
Found solution myself
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
$_SERVER Variable question 31 49
PHP: Filling Out/Creating a PDF 29 92
restriction of entering a a page 5 29
assigning javascript variable to php variable 8 31
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question