I have a following ModulePermissions Array
Array
(
[0] => Array
(
[module_permission_index_id] => 347
[module_id] => 1
[user_id] => 29
[can_view] => 1
[can_edit] => 1
[can_add] => 1
)
[1] => Array
(
[module_permission_index_id] => 348
[module_id] => 2
[user_id] => 29
[can_view] => 1
[can_edit] => 1
[can_add] => 1
)
... ... ...
)
Target:
Now I want to get Array to be sorted based on their module (module Id has multiple permissions)
something like below (please ignore syntax and get the idea)
[1] => array(
array(
[module_permission_index_id] => 347
[user_id] => 29
[can_view] => 1
[can_edit] => 1
[can_add] => 1
)
)
[2] => array(
array(
[module_permission_index_id] => 348
[user_id] => 29
[can_view] => 1
[can_edit] => 1
[can_add] => 1
)
)
... ... ...
)
Is there any way PHP helps do this?
Simplest way is:
$newData = [];
foreach ($yourData as $item) {
$newData[$item['module_id']][] = $item;
}
Related
I have an array that looks like this:
Array
(
[0] => stdClass Object
(
[quiz_id] => 1033
[quiz_venue] => 6
[quiz_host] => 46
[quiz_golden_question] => 100
[quiz_golden_question_outcome] => 0
[quiz_running] => 1
[quiz_status] => 100
[quiz_trainee] => 0
)
[1] => stdClass Object
(
[quiz_id] => 985
[quiz_venue] => 57
[quiz_host] => 21
[quiz_golden_question] => 0
[quiz_golden_question_outcome] => 0
[quiz_running] => 1
[quiz_status] => 310
[quiz_trainee] => 0
)
I want to go through each array, and insert a new value (quiz_venue_name), this is what I have;
$quizzes = $wpdb->get_results( $prepared );
foreach ($quizzes as $quiz => $item) {
$venuetitle = get_the_title($item->quiz_venue);
$quizzes['quiz_venue_name'] = $venuetitle;
}
return $quizzes;
However, all it does is add the new values to the very end of the multidimensional array as new arrays - rather than adding them into each!
I feel like I'm doing something obvious wrong, so any help is much appreciated!
The end result I need would be;
Array
(
[0] => stdClass Object
(
[quiz_id] => 1033
[quiz_venue] => 6
[quiz_host] => 46
[quiz_golden_question] => 100
[quiz_golden_question_outcome] => 0
[quiz_running] => 1
[quiz_status] => 100
[quiz_trainee] => 0
[quiz_venue_name] => NEW VALUE
)
[1] => stdClass Object
(
[quiz_id] => 985
[quiz_venue] => 57
[quiz_host] => 21
[quiz_golden_question] => 0
[quiz_golden_question_outcome] => 0
[quiz_running] => 1
[quiz_status] => 310
[quiz_trainee] => 0
[quiz_venue_name] => NEW VALUE
)
Your $quizzes variable is an array of objects (Instance of stdClass), so you should use it attribute to set value in each iteration ($item->quiz_venue_name instead of $quizzes['quiz_venue_name']):
...
foreach ($quizzes as $quiz => $item) {
$venuetitle = get_the_title($item->quiz_venue);
$item->quiz_venue_name = $venuetitle;
}
...
In fact the $quizzes['quiz_venue_name']=... code will be set a value for index quiz_venue_name in root of $quizzes array.
I've two arrays as follows:
$grid_data = Array
(
[0] => Array
(
[newsletter_id] => 1
[newsletter_name] => Eywa Solutions
[newsletter_subject] => Holi Wishes
[newsletter_email_body] => Happy Holi to all the friends
[newsletter_call_to_action_status] => 0
[newsletter_call_to_action_text] =>
[newsletter_call_to_action_link] =>
[newsletter_status] => 2
[newsletter_schedule] => 0
[newsletter_created_date] => Mar 17 2014, 16:21 pm
[newsletter_updated_date] => 0
)
[1] => Array
(
[newsletter_id] => 2
[newsletter_name] => Akshay Nikte
[newsletter_subject] => The Don
[newsletter_email_body] => How are yoy Nikte Saheb?
[newsletter_call_to_action_status] => 0
[newsletter_call_to_action_text] =>
[newsletter_call_to_action_link] =>
[newsletter_status] => 2
[newsletter_schedule] => 0
[newsletter_created_date] => Mar 18 2014, 06:52 am
[newsletter_updated_date] => 0
)
)
and second array is as follows:
$LastSendNewsletterDetail = Array
(
[0] => Array
(
[newsletter_id] => 1
[newsletter_sent_count] => 5
[newsletter_sent_date] => 1395121193
)
[1] => Array
(
[newsletter_id] => 2
[newsletter_sent_count] => 7
[newsletter_sent_date] => 1395121227
)
)
Now what I want to achieve is compare the key values(named newsletter_id) with each other from above two arrays. If they match with each other then insert the data from array $LastSendNewsletterDetail to the first array $grid_data. If they don't match then insert blank values to the first array $grid_data.
For example the desired ultimate $grid_data array should look like this :
$grid_data = Array
(
[0] => Array
(
[newsletter_id] => 1
[newsletter_name] => Eywa Solutions
[newsletter_subject] => Holi Wishes
[newsletter_email_body] => Happy Holi to all the friends
[newsletter_call_to_action_status] => 0
[newsletter_call_to_action_text] =>
[newsletter_call_to_action_link] =>
[newsletter_status] => 2
[newsletter_schedule] => 0
[newsletter_created_date] => Mar 17 2014, 16:21 pm
[newsletter_updated_date] => 0
[newsletter_sent_count] => 5
[newsletter_sent_date] => 1395121193
)
[1] => Array
(
[newsletter_id] => 2
[newsletter_name] => Akshay Nikte
[newsletter_subject] => The Don
[newsletter_email_body] => How are yoy Nikte Saheb?
[newsletter_call_to_action_status] => 0
[newsletter_call_to_action_text] =>
[newsletter_call_to_action_link] =>
[newsletter_status] => 2
[newsletter_schedule] => 0
[newsletter_created_date] => Mar 18 2014, 06:52 am
[newsletter_updated_date] => 0
[newsletter_sent_count] => 7
[newsletter_sent_date] => 1395121227
)
)
How to achieve this? Thanks in advance.
try this
for($i=0; $i < $grid_data.lenght; $i++)
{
$track =0;
foreach($LastSendNewsletterDetail as $value)
{
if($value[newsletter_id] === $grid_data[$i][newsletter_id] )
{
array_merge($grid_data[$i], $value);
$track=1;
}
}
if($track === 0)
{
//insert blank value here
}
}
I have this:
print_r($response["member"]);
I need to retrieve name under levels, it's at the bottom, how should I write it:
I thought of $response["member"][0]["Sequential"]["levels"]...? Also this number under levels wont be always the same.
Thank you!
Array
(
[0] => Array
(
[ID] => 1
[UserInfo] => Array
(
[ID] => 1
[caps] => Array
(
[administrator] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => administrator
)
[allcaps] => Array
(
[switch_themes] => 1
[edit_themes] => 1
[activate_plugins] => 1
[edit_plugins] => 1
[edit_users] => 1
[edit_files] => 1
[manage_options] => 1
[moderate_comments] => 1
[manage_categories] => 1
[manage_links] => 1
[upload_files] => 1
[import] => 1
[unfiltered_html] => 1
[edit_posts] => 1
[edit_others_posts] => 1
[edit_published_posts] => 1
[publish_posts] => 1
[edit_pages] => 1
[read] => 1
[level_10] => 1
[level_9] => 1
[level_8] => 1
[level_7] => 1
[level_6] => 1
[level_5] => 1
[level_4] => 1
[level_3] => 1
[level_2] => 1
[level_1] => 1
[level_0] => 1
[edit_others_pages] => 1
[edit_published_pages] => 1
[publish_pages] => 1
[delete_pages] => 1
[delete_others_pages] => 1
[delete_published_pages] => 1
[delete_posts] => 1
[delete_others_posts] => 1
[delete_published_posts] => 1
[delete_private_posts] => 1
[edit_private_posts] => 1
[read_private_posts] => 1
[delete_private_pages] => 1
[edit_private_pages] => 1
[read_private_pages] => 1
[delete_users] => 1
[create_users] => 1
[unfiltered_upload] => 1
[edit_dashboard] => 1
[update_plugins] => 1
[delete_plugins] => 1
[install_plugins] => 1
[update_themes] => 1
[install_themes] => 1
[update_core] => 1
[list_users] => 1
[remove_users] => 1
[add_users] => 1
[promote_users] => 1
[edit_theme_options] => 1
[delete_themes] => 1
[export] => 1
[administrator] => 1
)
[filter] =>
[user_login] => admin
[user_nicename] => admin
[user_email] => goranefbl#gmail.com
[user_url] =>
[user_registered] => 2014-01-29 10:57:09
[user_activation_key] =>
[user_status] => 0
[display_name] => admin
[wlm_feed_url] => http://pialarson.com/excel/feed/?wpmfeedkey=1;2e7e48ca65d94e5f0ec1baae46e4972c
[wpm_login_date] => 1392155735
[wpm_login_ip] => 62.68.119.252
)
[Sequential] =>
[Levels] => Array
(
[1391447566] => stdClass Object
(
[Level_ID] => 1391447566
[Name] => Team Membership
[Cancelled] =>
[CancelDate] =>
[Pending] =>
[UnConfirmed] =>
[Expired] =>
[ExpiryDate] => 1393866766
[SequentialCancelled] =>
[Active] => 1
[Status] => Array
(
[0] => Active
)
[Timestamp] => 1391447566
[TxnID] => WL-1-1391447566
)
)
[PayPerPosts] => Array
(
)
)
)
An answer might be to use array_walk_recursive by following the official documentation:
http://www.php.net/manual/en/function.array-walk-recursive.php
<?php
$properties = new stdClass();
$properties->names = [];
function extractNames($levels, $key, $properties) {
if (
is_object($levels) &&
array_key_exists('Name', get_object_vars($levels)) &&
array_key_exists('Level_ID', get_object_vars($levels))
) {
$properties->names[] = $levels->Name;
}
}
array_walk_recursive($response, 'extractNames', $properties);
echo print_r($properties, true);
<?php
$nameInFirstLevelsElement = current($response["member"][0]["levels"])->Name
?>
This should work to retrieve the Name from the first levels element.
That empty space next to "Sequential" means it doesn't have a value. So that's not the one you're looking for.
Furthermore, one of those levels indicates "stdClass object", which means you can access its members via the -> operator.
Let's strip away everything that doesn't matter for a minute. I think it'll help you understand the data structure:
Array
(
[0] => Array
(
[Levels] => Array
(
[1391447566] => stdClass Object
(
[Level_ID] => 1391447566
[Name] => Team Membership
)
)
)
)
So this will work:
$object = $response["member"][0]["Levels"][1391447566];
$name = $object->Name;
Edit
If the index of Levels changes every time, then pull it apart a little bit more...
$levels = $response["member"][0]["Levels"];
$firstLevel = array_shift(array_values($levels));
$name = $firstLevel->Name;
See here for a good answer on getting the first element out of the $levels array: https://stackoverflow.com/a/3771228/266374
If the number under the 'Levels' array won't be the same, you can use a foreach to pull out the 'Name' information.
foreach ($response[0]['Levels'] AS $level_key => $level_val) {
$level_name = $level_key->Name;
}
echo 'Name: '.$level_name;
If there is only going to be one element, then it will grab it. If there are multiple numbers under 'Levels', then it will loop through them and assign each one to '$level_name', overwriting any previous assignments. In other words, only the last one it finds will be captured.
EDIT:
In the example, I mistakenly tried to grab the Name from the $key instead of the $val. This is the correct method:
foreach ($response[0]['Levels'] AS $level_key => $level_val) {
$level_name = $level_val->Name;
}
echo 'Name: '.$level_name;
Here is a demo of the working code
I have a data structure like this
Array
(
[0] => Array
(
[actionResult] => Array
(
[show_page] => Array
(
[15] => Array
(
[section_page_id] => 15
[metadata_id] => 62
[section_id] => 7
[display_order] => 0
[current_layout] => 15
[behaviors] => a:1:{i:0;a:1:{i:0;a:0:{}}}
[options] => a:1:{s:13:"defaultLayout";i:63;}
[section_title] => Ask Study
)
[16] => Array
(
[section_page_id] => 16
[metadata_id] => 66
[section_id] => 7
[display_order] => 1
[current_layout] => 16
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:67;}
[section_title] => Ask Study
)
[17] => Array
(
[section_page_id] => 17
[metadata_id] => 69
[section_id] => 7
[display_order] => 2
[current_layout] => 17
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:70;}
[section_title] => Ask Study
)
[18] => Array
(
[section_page_id] => 18
[metadata_id] => 72
[section_id] => 7
[display_order] => 3
[current_layout] => 18
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:73;}
[section_title] => Ask Study
)
)
)
)
[1] => Array
(
[actionResult] => Array
(
[view_page] => 18
)
)
)
What i need is the ability to flatten this to an array structure to a point where it stops at "actionResult" where all the actionResult will become ONE array rather than nested like this...
How can I go about by doing this in PHP???
if i have understood what you want correctly this should work:
$arr2=array();
foreach($arr as $tmp){
foreach($tmp as $actionRequest){
foreach($actionRequest as $key=>$val){
$arr2[$key]=$val;
}
}
}
where $arr is what you already have and $arr2 will be an array including 2 values , Show_Page and view_page
Best solution is:
$new_arr = array_map(function ($a) {
return $a['actionResult'];
}, $old_arr);
I've got this array:
Array
(
[0] => Array
(
[id]=>1
[account_id] => 1
[object_id] => 43
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
[1] => Array
(
[id] => 1
[account_id] => 1
[object_id] => 42
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
[2] => Array
(
[id] => 1
[account_id] => 1
[object_id] => 41
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
[3] => Array
(
[id] => 2
[account_id] => 2
[object_id] => 1
[object_type] => USER
[action_type] => FOLLOW_USER
)
[4] => Array
(
[id] => 1
[account_id] => 1
[object_id] => 2
[object_type] => USER
[action_type] => FOLLOW_USER
)
[5] => Array
(
[id] => 1
[account_id] => 1
[object_id] => 1
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
)
Now I want to group elements have same value (for example UPLOAD_PHOTO) by id as Primary Key and action_type as Secondary Key, like:
Array
(
[0] => Array(
[id] => 1
[account_id] => 1
[actions] => array(
[1] => array(
[object_id] => 42
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
[2] => array(
[object_id] => 43
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
)
)
[2] => Array
(
[id] => 1
[account_id] => 1
[actions] = array(
[0] => Array
(
[object_id] => 3
[object_type] => USER
[action_type] => FOLLOW_USER
)
[1] => Array
(
[object_id] => 4
[object_type] => USER
[action_type] => FOLLOW_USER
)
)
)
)
I tried some solutions but didn't succeed.
When constructing your output array, you'll want to use meaningful array keys. That way you can find the out element that the in element needs to be added to:
$in = (...your array...);
$out = array();
foreach($in as $element) {
$id = $element['id'];
//If that id hasn't been seen yet, create the out element.
if (!isset($out[$id])) {
$out[$id] = array(
'id'=>$element['id'],
'account_id' => $element['account_id'],
'actions' => array()
);
}
//Add that action to the out element
$out[$id]['actions'][] = array(
'object_id' => $element['object_id'],
'object_type' => $element['object_type'],
'action_type' => $element['action_type']
);
}
I'm not sure why your input elements have different fields... If this is a desired feature, where one set of possible fields belongs to the id group and another set belongs to the action, you can do this instead (slightly less readable, but more flexible):
$in = (...your array...);
$out = array();
//These define which fields from an
//input element belong to the id,
//and which to the action.
$id_fields = array_flip(array('id','account_id','object_user_id');
$action_fields = array_flip(array('object_id','object_type','action_type');
foreach($in as $element) {
$id = $element['id'];
//If that id hasn't been seen yet, create the out element.
if (!isset($out[$id])) {
$out[$id] = array_intersect_key($element, $id_fields);
$out[$id]['actions'] = array();
}
//Add that action to the out element
$out[$id]['actions'][] = array_intersect_key($element, $action_fields);
}
This is a better solution if the input elements can be different, because if an expected field (other than 'id') is missing, the script will cope with it easily.