I got the following response output from curl request.
Array
(
[code] => 200
[message] => Message history obtained successfully.
[data] => Array
(
[0] => Array
(
[id] => 3390257
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784285
[ext_message_id] => 152127f68b2000031bf6584fa8494a7b
[send_date] => 2016-01-05 09:54:17
[dest_address] => 8476099160
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
[1] => Array
(
[id] => 3390261
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784289
[ext_message_id] => 152127f6c36000171df5874efba398b4
[send_date] => 2016-01-05 09:54:17
[dest_address] => 6188892860
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
[2] => Array
(
[id] => 3390265
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784293
[ext_message_id] => 152127f65dd000031bf6584fa84949af
[send_date] => 2016-01-05 09:54:17
[dest_address] => 2032415751
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
[3] => Array
(
[id] => 3390270
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784298
[ext_message_id] => 152127f6a2a0001a8d8e4ef0eebbbe5d
[send_date] => 2016-01-05 09:54:17
[dest_address] => 3106193605
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
)
)
Now I want to get the array value like $keyword_id = 1459 But I can't access the array.
Assuming this response is stored in a variable called $response, here are some examples of accessing array elements...
$response['data'][0]['id'] // 3390257 (id of 1st data element)
$response['data'][1]['dest_address'] // 6188892860 (dest_address of 2nd data element)
$response['data'][3]['keyword_id'] // 1459 (keyword_id of 4th data element)
You could also iterate over the data elements and print the ids as follows...
foreach ($response['data'] as $i => $element) {
echo "The id of element " . $i . " is " . $element['id'] . "\n";
}
you can get keyword_id through foreach loop. run the foreach loop on your array and get data through keyword_id index.
foreach($array as $value){
$keyword_id = $value['keyword_id'];
}
this will give you keyword_id.
Related
I am trying to access a multi-dimensional array using PHP. I can access the first level using -
$response['id']
but having trouble with how the second level is defined so I can do -
$response['first level']['second level']
Here is my json response -
Array
(
[id] => 7181676
[api_request_id] => 20984853
[user] => 8305
[vin] => JTDKN3DU7D1643423
[make] => Toyota
[model_name] => Prius
[model_year] => 2013
[last_updated] => 2019-02-22T01:08:15.628318Z
[recall_count] => 1
[status] => ok
[recalls] => Array
(
[0] => Array
(
[recall_id] => 15753
[recall_last_updated] => 2019-02-22T00:33:07.663232Z
[recall_age] => 0
[nhtsa_id] => 18V684000
[oem_id] => J0V
[name] => HybridSystem
[description] => CollisionRisk
[campaign_type] => nhtsa
[is_remedy_available] => 1
[are_parts_available] => 1
[risk_type] => collision
[risk_rank] => 5
[profit_rank] => 3
[overall_rank] => 5
[labor_difficulty] => 3
[government_id] => 18V684000
[is_reviewed] => 1
[child] => Array
(
)
)
)
)
How would I get lets say -
$response['profit_rank']
I query db and I have an array. How can I access stdClass object ? And I want also serializedName property like in java serializedName. So, The fields which query from db must be assign with my custom object class fields.How can I use this?
Array
(
[0] => stdClass Object
(
[receipt_id] => 1
[defect] => asd
[tec_report] => sad
[doc_number] => asd
[warrant] => 12
[warrant_start] => 2016-01-05
[warrant_end] => 2016-01-29
[repair_time] => 3
[receipt_date] => 2016-01-05
[delivery_date] =>
[service_type_id] => 0
[service_price] => 0
[part_price] => 0
[pay_type] =>
[labour] => 0
[summary] => 0
[technician_id] =>
[customer_id] => 0
[device_id] => 0
[seri_number] =>
)
[1] => stdClass Object
(
[receipt_id] => 2
[defect] => asdsad
[tec_report] => asds
[doc_number] =>
[warrant] =>
[warrant_start] =>
[warrant_end] =>
[repair_time] =>
[receipt_date] => 2016-01-05
[delivery_date] =>
[service_type_id] => 1
[service_price] => 12
[part_price] => 12
[pay_type] =>
[labour] => 12
[summary] => 12
[technician_id] =>
[customer_id] => 21
[device_id] => 12
[seri_number] =>
)
)
try this for one by one
echo $array[0]->receipt_id;
try this :
foreach($yourArray as $val){
echo $val->receipt_id;
echo "<br/>".$val->defect;
}
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 have a array called $data['job_list'] which is as follows
Array
(
[0] => Array
(
[job_id] => 2
[job_title] => JQuery developer
[job_desc] => Developer
[job_slug] => 2-JQuery-developer
[job_type] => 191
[job_skill] => 2
[job_salary] => 2
[job_experience] => 1
[company_name] => IGUTS
[company_desc] => IGUTS is a fresh company
[company_industry] => 24
[company_address] => 35 Lawrence Street
[company_state] => 35
[company_city] => 650
[user_id] => 1
[concerned_fname] => Saswat
[concerned_lname] => Routroy
[contact] => 8961287928
[date_of_post] => 26-04-2014
[job_timestamp] => 1398517810
[industry_id] => 191
[industry_title] => Web Designer/Developer
[p_cid] => 24
[industry_slug] => 191-Web-Designer-Developer
[industry_desc] =>
[industry_image] =>
[industry_priority] => 0
[industry_timestamp] => 1396535046
)
[1] => Array
(
[job_id] => 1
[job_title] => PHP developer
[job_desc] => Developer
[job_slug] => 1-PHP-developer
[job_type] => 191
[job_skill] => 1,2
[job_salary] => 1
[job_experience] => 1
[company_name] => IGUTS
[company_desc] => IGUTS Company
[company_industry] => 24
[company_address] => 35 Lawrence Street
[company_state] => 35
[company_city] => 650
[user_id] => 1
[concerned_fname] => Saswat
[concerned_lname] => Routroy
[contact] => 8961287928
[date_of_post] => 18-04-2014
[job_timestamp] => 1397842605
[skill_id] => 2
[skill_title] => JQuery
[skill_slug] => 2-JQuery
[industry] => 24
[skill_timestamp] => 1397395987
)
[2] => Array
(
[job_id] => 2
[job_title] => JQuery developer
[job_desc] => Developer
[job_slug] => 2-JQuery-developer
[job_type] => 191
[job_skill] => 2
[job_salary] => 2
[job_experience] => 1
[company_name] => IGUTS
[company_desc] => IGUTS is a fresh company
[company_industry] => 24
[company_address] => 35 Lawrence Street
[company_state] => 35
[company_city] => 650
[user_id] => 1
[concerned_fname] => Saswat
[concerned_lname] => Routroy
[contact] => 8961287928
[date_of_post] => 26-04-2014
[job_timestamp] => 1398517810
[skill_id] => 2
[skill_title] => JQuery
[skill_slug] => 2-JQuery
[industry] => 24
[skill_timestamp] => 1397395987
)
)
[job_id] => 2 is present twice
What I want is that, the row with duplicate job_id => 2 should be removed
How can I achieve that??
$jobIds = array();
foreach ($jobs as $key => $job) {
if (in_array($jobIds, $job['job_id'])) {
unset($jobs[$key]);
continue;
}
$jobIds[] = $job['job_id'];
}
print_r($jobs);
I would cycle though each "job" and create an array of Job IDs. If I see the same ID twice then I would remove the duplicate and store it somewhere else if needed.
As Matei Mihai stated, i made some minor changes in his script and bingo it solved..
Matei Mihai made a mistake regarding the data-type of arguments of the in_array() function
I fixed that.
here's my code
$jobIds = array();
for($i = 0;$i < count($data['job_list']); $i++ )
{
if (in_array($data['job_list'][$i]['job_id'], $jobIds))
{
unset($data['job_list'][$i]);
continue;
}
$jobIds[] = $data['job_list'][$i]['job_id'];
}
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