Flatten Nested Array to a certain Key - php

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);

Related

search inside arrays with condition in php

I have an api with a list of arrays inside one array and each array inside this array it has one key [attributes] and inside this key one array with key [CITY_NAME]
this is my array coming from api
this array number is 0 but every day this number is changed and my figures come wrong, what is the good way to always have my city figures using the key [CITY_NAME]
I am fetching the data using this code
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$data = $json_data['features'];
$mycity = $data[0]['attributes'];
Array
(
[0] => Array
(
[attributes] => Array
(
[CITY_NAME] => city1
[Name] => city1
[ADMIN_NAME] => city1
[POP_CLASS] => 5,000,000 to10,000,000
[Population] => 7676654
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2018
[CityID] => 14
[Longitude] => 46.7614868685786
[Latitude] => 24.7388786516234
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city1
[Join_Count] => 61
[Confirmed_SUM] => 5152
[Deaths_SUM] => 9
[Recovered_SUM] => 1407
[Active_SUM] => 3736
[Tested_SUM] => 376607
[ObjectId] => 14
)
)
[1] => Array
(
[attributes] => Array
(
[CITY_NAME] => city2
[Name] => city2
[ADMIN_NAME] => city2
[POP_CLASS] => 1,000,000 to 5,000,000
[Population] => 1675368
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2010
[CityID] => 9
[Longitude] => 39.8148987363852
[Latitude] => 21.4273876500039
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city2
[Join_Count] => 59
[Confirmed_SUM] => 6848
[Deaths_SUM] => 85
[Recovered_SUM] => 1145
[Active_SUM] => 5618
[Tested_SUM] => 0
[ObjectId] => 9
)
)
Since I may have misunderstood and you may have many, just build a new array with CITY_NAME:
foreach($data as $values) {
$result[$values['attributes']['CITY_NAME']] = $values['attributes'];
}
Now you can access by CITY_NAME:
echo $result['city1']['Population'];
This might be easier. Extract all attributes from all arrays into an array, then create an array from that indexed by CITY_NAME:
$data = array_column(array_column($json_data['features'], 'attributes'),
null, 'CITY_NAME');

Redefine array based on a column in child array column value - PHP

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;
}

How to delete array of array if particular key doesn't exist in it?

I have collection of array and i want delete array of array where "answer_id" key is not exits.
my array look like this.
Array
(
[0] => Array
(
[question_no] => 1
[subject_id] => 1
[question_id] => 255
[currect_ans_id] => 2657
[time_taken] => 110
[is_visited] => 1
[is_saved] => 0
[answer_id] => 2659
)
[1] => Array
(
[question_no] => 2
[subject_id] => 1
[question_id] => 256
[currect_ans_id] => 2662
[time_taken] => 0
[is_visited] => 1
[is_saved] => 0
)
)
and want array like this(where answer_id key exits).
Array
(
[0] => Array
(
[question_no] => 1
[subject_id] => 1
[question_id] => 255
[currect_ans_id] => 2657
[time_taken] => 110
[is_visited] => 1
[is_saved] => 0
[answer_id] => 2659
)
)
You can use array_filter to remove entries which don't have an answer_id:
$output = array_filter($input, function ($a) { return isset($a['answer_id']); });
Demo on 3v4l.org

checking condition before foreach loop execution php

i want to check condition before for loop execution
this is my array
$rs=Array (
[0] => Array (
[questionID] => 47
[surveyID] => 51
[userID] => 31
[question_Title] => Choose Any One?
[question_Type] => Dropdown
[response] => 1.Android 2.Windows 3.Blackberry
[required] => 0
[add_time] => 0
)
[1] => Array (
[questionID] => 48
[surveyID] => 51
[userID] => 31
[question_Title] => Is it?
[question_Type] => Bigbox
[response] => Yes No
[required] => 1
[add_time] => 0
)
[2] => Array (
[questionID] => 129
[surveyID] => 51
[userID] => 31
[question_Title] => sELECT
[question_Type] => Single
[response] => DFG HBK GHCK HK
[required] => 0
[add_time] => 0
)
)
now i want to check if in $rs [required] => 1
then stop over all execution or $rs traversing using for each loop
rather from above example i want to stop executing first loop also.
Use array_column and achieve this functionality
Reference
http://php.net/manual/en/function.array-column.php
http://php.net/manual/en/function.array-search.php
Lower version array column function code
https://github.com/ramsey/array_column/blob/master/src/array_column.php
<?php
//assumed records is your array
$required = array_column($records, 'required');
if(FALSE===array_search('1', $required))
{
//value is not exist then process your loop
}
else
{
//required value is 1 then ignore the loop
}

complicated multidimentional array process with foreach

im having a problem trying to process this array tried several different ways but none where right, here's the array
Array (
[search] => Array (
[response] => Array (
[errors] =>
[number_of_hotels] => 1 of 1
)
[lr_rates] => Array (
[hotel] => Array (
[hotel_ref] => 3116
[hotel_currency] => [U] => USD
[hotel_rooms] => Array (
[room] => Array (
[ref] => 6382
[type] => 1
[type_description] => Standard
[sleeps] => 8
[rooms_available] =>
[adults] => 8
[children] =>
[breakfast] => false
[dinner] => false
[description] =>
[alternate_description] =>
[rack_rate] => 82.01
[date] => 19/08/201220/08/201221/08/2012
[numeric_hotelcurrencyprice] => FullFullFull
[formatted_date] => 19 August 201220 August 201221 August 2012
[price] => FullFullFull
[hotelcurrencyprice] => FullFullFull
[numeric_price] => FullFullFull
[requested_currency] => GBPGBPGBP
[numeric_hotelcurrencyprice] => FullFullFull
[available_online] => false
[minimum_nights] => 1
[bed_type] =>
[cancellation_policy] =>
[cancellation_days] =>
[cancellation_hours] =>
[room_terms] =>
)
[room] => Array (
[ref] => 6382
[type] => 1
[type_description] => Standard
[sleeps] => 8
[rooms_available] =>
[adults] => 8
[children] =>
[breakfast] => false
[dinner] => false
[description] =>
[alternate_description] =>
[rack_rate] => 82.01
[date] => 19/08/201220/08/201221/08/2012
[numeric_hotelcurrencyprice] => FullFullFull
[formatted_date] => 19 August 201220 August 201221 August 2012
[price] => FullFullFull
[hotelcurrencyprice] => FullFullFull
[numeric_price] => FullFullFull
[requested_currency] => GBPGBPGBP
[numeric_hotelcurrencyprice] => FullFullFull
[available_online] => false
[minimum_nights] => 1
[bed_type] =>
[cancellation_policy] =>
[cancellation_days] =>
[cancellation_hours] =>
[room_terms] =>
)
)
[cancellation_type] => First Night Stay Chargeable
[cancellation_policy] => 2 Days Prior to Arrival
[CityTax] => Array (
[TypeName] =>
[Value] =>
[OptedIn] =>
[IsCityTaxArea] =>
)
)
)
)
)
ok i need to traverse the array and create a loop so for every instance of room it will repeat the process then i need to extract the data from room array and use it to populate rows in MySQL there will be multiple instances of room this is the code i have so far which prints the names and values in the room array but it only gets one of the room arrays what can i do to set it up to read them all and i am also thinking this is too many for-each but don't seem to be able to traverse down ['']['']['']...
or by just using the associative name.
foreach($arr['search'] as $lr_rates) {
foreach($lr_rates['hotel'] as $hotel) {
foreach($hotel['room'] as $field => $value){
print $field;print $value;
}
}
}
it mite also be worth mentioning the values in these arrays are always fluctuating
you don't have to use so much foreach loops in your script as it not looks good. this is an associative array.
you can simply access associate array by using its keys. do some google for it.you can find many scripts on this.
foreach($arr as $search) {
foreach($search as $lr_rates) {
foreach($lr_rates as $hotel) {
foreach($hotel as $hotel_rooms) {
print_r($hotel_rooms['room'])
}
}
}
}
EDIT: These many foreach loops are just to make understand how to reach to room. You can also get the result directly ofcourse.
print_r($arr['search']['lr_rates']['hotel']['hotel_rooms']['room']);

Categories