?
Solved

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

Posted on 2015-02-19
4
Medium Priority
?
80 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 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