Link to home
Start Free TrialLog in
Avatar of Loganathan Natarajan
Loganathan NatarajanFlag for India

asked on

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 User generated image.

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

'title ' : 'Share'
- SHARE

'year' : '2014'
- 2014
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

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.
Avatar of Loganathan Natarajan

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Found solution myself