Loganathan Natarajan
asked on
How can I create multi dimensional array with key, value on this case?
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.
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Found solution myself