• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 85
  • Last Modified:

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

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
Loganathan Natarajan
Asked:
Loganathan Natarajan
  • 3
1 Solution
 
gr8gonzoConsultantCommented:
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
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
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
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
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
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
Found solution myself
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now