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
Related
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;
}
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
How can we sort an associative array with custom order?
My array look like this
Array
(
[pa_color] => Array
(
[name] => pa_color
[value] =>
[position] => 0
[is_visible] => 1
[is_variation] => 1
[is_taxonomy] => 1
)
[pa_dimension] => Array
(
[name] => pa_dimension
[value] =>
[position] => 1
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_travel-duration] => Array
(
[name] => pa_travel-duration
[value] =>
[position] => 2
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_travel-type] => Array
(
[name] => pa_travel-type
[value] =>
[position] => 3
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_travelling-with] => Array
(
[name] => pa_travelling-with
[value] =>
[position] => 4
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_volume] => Array
(
[name] => pa_volume
[value] =>
[position] => 5
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_weight] => Array
(
[name] => pa_weight
[value] =>
[position] => 6
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
)
and i want this array is like pa_travel-duration first pa_volume second ?? I know there is a native php function usort but i could not understand this.
This will do the work, but im pretty sure there's much better ways to do it:
Code:
$array = array("pa_color" => "color",
"pa_dimension" => "dimension",
"pa_travel-duration" => "Random Stuff: " . rand(100,999),
"pa_volume" => "volumen"
);
$tmp = array("pa_travel-duration" => $array["pa_travel-duration"],
"pa_volume" => $array["pa_volume"],
);
unset($array["pa_travel-duration"], $array["pa_volume"]);
$array = array_merge($tmp,$array);
print_r($array);
Result:
Array
(
[pa_travel-duration] => Random Stuff: 127
[pa_volume] => volumen
[pa_color] => color
[pa_dimension] => dimension
)
Take care because if the array doesn't have the proper key it will throw an error, you need to add few checks there.
$sort_by = array('pa_travel-duration', 'pa_volume', 'pa_color','pa_dimension','pa_travel-type','pa_travelling-with','pa_weight');
$temp_arr = array();
foreach ($sort_by as $key) {
$temp_arr[$key] = $data[$key];
}
$data = $temp_arr;
echo '<pre>'; print_r($data);
Define your order in $sort_by array
Scenario:
I downloaded a Joomla extension and am editing the php file to alter the layout of the module. Now in the source code of the file lies this bit of code.
<?php if ( !empty($this->fields) ) { ?>
foreach ($this->fields as $field)
{
echo RSDirectoryFilter::getInstance($field, $options)->generate();
}
} ?>
which cycles through a list of form fields and prints out the fields and their options.
what i want to do is apply some styling to a specific form field only.
that loop printed out 3 fields altogether. My first step was to add a print_r($field) into the loop to see what data is stored within the $field parameter. each field was an array of data
this is what it printed out
stdClass Object ( [id] => 1 [field_type_id] => 1 [name] => title [column_name] => title [form_field_name] => title [required] => 1 [published] => 1 [field_type] => title [core] => 1 [create_column] => 0 [expect_value] => 1 [properties] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( [form_caption] => Title [default_value] => [field_prepend] => [field_append] => [show_help_tip] => 1 [help_tip] => [show_help_text] => 1 [help_text_position] => block [help_text] => [readonly] => 0 [additional_html_attributes] => [id] => 1 [searchable_simple] => 1 [searchable_advanced] => textbox [searchable_advanced_caption] => Keywords [searchable_advanced_items] => [searchable_advanced_condition_type] => containing [default_validation_rule] => none [extra_accepted_chars] => [regex_syntax] => [custom_validation_rule] => [characters_limit] => 0 [validation_message] => There was an error with the title field. ) [separator] => . ) )
stdClass Object ( [id] => 31 [field_type_id] => 11 [name] => status [column_name] => f_31 [form_field_name] => status [required] => 1 [published] => 1 [field_type] => dropdown [core] => 0 [create_column] => 1 [expect_value] => 1 [properties] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( [id] => 31 [help_text] => [additional_html_attributes] => [credits] => 0 [default_values] => Stolen Lost Found [default_value] => [size] => 1 [multiple] => 0 [field_prepend] => [field_append] => [show_help_tip] => 1 [help_tip] => [show_help_text] => 1 [help_text_position] => block [listing_caption] => status [dependency] => 0 [items] => Stolen Lost Found [form_caption] => status [searchable_simple] => 1 [searchable_advanced] => dropdown [searchable_advanced_caption] => Listing Status [use_dependency] => 1 [use_field_items] => 1 [searchable_advanced_items] => [searchable_advanced_condition_type] => strict [validation_message] => Invalid input. ) [separator] => . ) )
stdClass Object ( [id] => 34 [field_type_id] => 17 [name] => date-of-incident [column_name] => f_34 [form_field_name] => date_of_incident [required] => 1 [published] => 1 [field_type] => calendar [core] => 0 [create_column] => 1 [expect_value] => 1 [properties] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( [form_caption] => Date Of Incident [listing_caption] => Date Of Incident [default_date] => [min_date] => [max_date] => [date_mask] => d F Y [time_mask] => g:i a [calendar_layout] => flat [readonly] => 0 [show_help_tip] => 0 [help_tip] => [show_help_text] => 0 [help_text_position] => block [help_text] => [additional_html_attributes] => [credits] => 0 [id] => 0 [searchable_simple] => 1 [searchable_advanced] => date_range [searchable_advanced_caption] => Date Range [searchable_advanced_items] => [searchable_advanced_condition_type] => strict [validation_message] => Invalid input. ) [separator] => . ) )
so the field i want to target is the one which has the [name] date-of-incident. (the 3rd one above)
i tried to modify the loop so target this specific record like so
<?php if ( !empty($this->fields) )
{
foreach ($this->fields as $field)
{
print_r($field);
if($field[name] == "date-of-incident")
{
echo "Date of incident here";
}
else
{
echo "<div class='span3'>";
echo RSDirectoryFilter::getInstance($field, $options)->generate();
echo "</div>";
}
}
} ?>
but this just crashes the site but doesnt give an error message, i assume i am using if($field[name] == "date-of-incident") incorrectly?
Appreciate any help
Thanks
Luke
Note from your print_r($field); that the output says they are Objects i.e. stdClass Object
You therefore address the name property using the object notation -> and not the array notation.
<?php
if ( !empty($this->fields) ) {
foreach ($this->fields as $field) {
//print_r($field);
// here is the change
if($field->name == "date-of-incident") {
echo "Date of incident here";
} else {
echo "<div class='span3'>";
echo RSDirectoryFilter::getInstance($field, $options)->generate();
echo "</div>";
}
}
}
?>
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.
i was trying to access this php array with no luck, i want to access the [icon] => icon.png
Array ( [total] => 2
[total_grouped] => 2
[notifys] => Array ( [0] => Array ( [notifytype_id] => 12
[grouped] => 930
[icon] => icon.png
[n_url] => wall_action.php?id=930
[desc] => 690706096
[text] => Array ( [0] => Sarah O'conner ) [total] => 1 )))
$arr['notifys'][0]['icon']
ETA: I'm not sure what your comment means, the following code:
$arr = array('total'=>2, 'total_grouped' => 2, 'notifys' => array(array(
'notifytype_id' => 12, 'icon' => 'icon.png')));
echo '<pre>';
print_r($arr);
echo '</pre>';
var_dump($arr['notifys'][0]['icon']);
outputs:
Array
(
[total] => 2
[total_grouped] => 2
[notifys] => Array
(
[0] => Array
(
[notifytype_id] => 12
[icon] => icon.png
)
)
)
string(8) "icon.png"
Generally, code never outputs nothing. You should be developing with all errors and notifications on.
$arr['notifys'][0]['icon']
rg = Array ( [total] => 2 [total_grouped] => 2 [notifys] => Array ( [0] => Array ( [notifytype_id] => 12 [grouped] => 930 [icon] => icon.png [n_url] => wall_action.php?id=930 [desc] => 690706096 [text] => Array ( [0] => Sarah O'conner ) [total] => 1 )));
icon = rg["notifsys"][0]["icon"];
Everybody is posting right answer. Its just you have giving a wrong deceleration of array.
Try var_dump/print_r of array and then you can easily understand nodes.
$arr = array(total => 2,
total_grouped => 2,
notifys => array( 0 => array(notifytype_id => 12,
grouped => 930,
icon => 'icon.png',
n_url => 'wall_action.php?id=930',
desc => 690706096,
text =>array(0 => 'Sarah Oconner' ),
total => 1,
),
),
);
echo $arr['notifys']['0']['icon'];