Extract array with same name from multidimensional array - php

I have this Array from a json that represent a form. I have to collect all the [components] in a new array
Array
(
[components] => Array
(
[0] => Array
(
[key] => number1
[type] => number
[input] => 1
[label] => Test Number Field
)
[1] => Array
(
[key] => testRadioFiled
[type] => radio
[label] => Test Radio Filed
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
[2] => Array
(
[key] => testSelectBoxField
[type] => selectboxes
[input] => 1
[label] => Test Select Box Field
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
[3] => Array
(
[key] => testSelectField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
[4] => Array
(
[key] => testPanel
[type] => panel
[input] =>
[label] => Panel
[title] => Test Panel
[components] => Array
(
[0] => Array
(
[key] => testSelectMultipleField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[mask] =>
[sort] =>
[type] => select
[input] => 1
[label] => Test Select Multiple Field
)
[1] => Array
(
[key] => testColumns
[type] => columns
[label] => Test Columns
[columns] => Array
(
[0] => Array
(
[key] => column
[type] => column
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => radio
[type] => radio
[input] => 1
[label] => Radio
[values] => Array
(
[0] => Array
(
[label] => first
[value] => first
[shortcut] =>
)
[1] => Array
(
[label] => second
[value] => second
[shortcut] =>
)
[2] => Array
(
[label] => third
[value] => third
[shortcut] =>
)
)
)
)
)
[1] => Array
(
[key] => column
[type] => column
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => testSelectField2
[tab] => 1
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
)
)
)
)
)
[collapsible] =>
)
[5] => Array
(
[key] => tabs2
[mask] =>
[type] => tabs
[input] =>
[label] => Tabs
[components] => Array
(
[0] => Array
(
[key] => tab2
[label] => Tab 1
[components] => Array
(
[0] => Array
(
[key] => columns2
[tab] => 0
[mask] =>
[type] => columns
[input] =>
[label] => Columns
[logic] => Array
(
)
[columns] => Array
(
[0] => Array
(
[key] => column
[type] => column
[input] =>
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => number4
[type] => number
[input] => 1
[label] => Test Number Field in Column
)
)
)
[1] => Array
(
[key] => column
[type] => column
[input] =>
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => number3
[type] => number
[input] => 1
[label] => Test Number Field in Column
)
)
)
)
)
)
)
[1] => Array
(
[key] => tab2
[label] => Tab 2
[components] => Array
(
[0] => Array
(
[key] => testSelectField1
[tab] => 1
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
)
)
)
)
)
)
I solved making a serious of foreach and in each loop I used logic:
If not exist an array "components" -> check if is type columns
if yes check if exist "components" and saved in new array
if not save in new array
If exist again check apply the same condition of point 1 this for 3 or 4 sublevel.
This is the final array that I need
Array
(
[0] => Array
(
[key] => number1
[type] => number
[input] => 1
[label] => Test Number Field
)
[1] => Array
(
[key] => testRadioFiled
[type] => radio
[label] => Test Radio Filed
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
......
[2] => Array
(
[key] => testSelectMultipleField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[mask] =>
[sort] =>
[type] => select
[input] => 1
[label] => Test Select Multiple Field
)
}
This code make the job but can't be the final solution.
$test_array = Array();
foreach($data['components'] AS $comp_1){
if(!isset($comp_1['components'])){
if($comp_1['type'] != 'columns'){
$test_array[] = $comp_1;
} else {
foreach($comp_1['columns'] as $col_1){
if(!isset($col_1['components'])){
$test_array[] = $col_1;
} else {
foreach($col_1['components'] AS $col_comp_1){
if(!isset($col_comp_1['components'])){
$test_array[] = $col_comp_1;
}
}
}
}
}
} else {
foreach($comp_1['components'] AS $comp_2){
if(!isset($comp_2['components'])){
if($comp_2['type'] != 'columns'){
$test_array[] = $comp_2;
} else {
foreach($comp_2['columns'] as $col_2){
if(!isset($col_2['components'])){
$test_array[] = $col_2;
} else {
foreach($col_2['components'] AS $col_comp_2){
if(!isset($col_comp_2['components'])){
$test_array[] = $col_comp_2;
}
}
}
}
}
} else {
foreach($comp_2['components'] AS $comp_3){
if(!isset($comp_3['components'])){
if($comp_3['type'] != 'columns'){
$test_array[] = $comp_3;
} else {
foreach($comp_3['columns'] as $col_3){
if(!isset($col_3['components'])){
$test_array[] = $col_3;
} else {
foreach($col_3['components'] AS $col_comp_3){
if(!isset($col_comp_3['components'])){
$test_array[] = $col_comp_3;
}
}
}
}
}
}
}
}
}
}
}
My question is there is a way for extract all the "components" array that of coarse take care if not contain other "components" array ?

If I understood you correctly recursion can help:
function extractComponents($arrayWithComponents)
{
$components = [];
if (!isset($arrayWithComponents['components']) || !is_array($arrayWithComponents['components'])) {
return $components;
}
foreach ($arrayWithComponents['components'] as $component) {
if (isset($component['columns'])) {
foreach ($component['columns'] as $column) {
$components = array_merge($components, extractComponents($column));
}
} else {
$components = array_merge($components, extractComponents($component));
unset($component['components']);
$components[] = $component;
}
}
return $components;
}
var_dump(extractComponents($data));
Example.

Related

How to generate nested associative array of specific fields from database table which has different fields?

I want to generate a nested associative array from the result of SQL query from the table which should look like this:
[0] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Sciuridae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Marmota
[children] => Array
(
[0] => Array
(
[name] => Marmota himalayan
[children] => Himalayan Marmot
)
)
)
)
),
[1] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
),
[2] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
),
[3] => Array
(
[name] => Cricetidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Alticola
[children] => Array
(
[0] => Array
(
[name] => Alticola roylei
[children] => Royle's Mountain V
)
)
)
)
)
)
)
Problem: I am unable to figure it out how to nest array if there are same subsequent particular column values, for example, Rodentia has 3 different families (according to the table) it should nest inside children array and similarly for other fields as shown in the above array. My code is as follows:
$sql = "SELECT * FROM `mamallia_table`";
$result = $conn->query($sql);
$genus = array();
if ($result->num_rows > 0) {
foreach($result as $row){
$data[] = classification_order($row);
}
echo "<pre>";
print_r($data);
} else {
echo "0 results";
}
}
function classification_order($row){
$species[] = array("name"=>$row['species'],"children"=>$row['common_name']);
$genus[] = array("name"=>$row['genus'],"children"=>$species);
$family_count[] = array("name"=>$row['family'],"children_no"=>$row['no_of_species'],"children"=>$genus);
$data = ["name"=>$row['order'],"children_no"=>$row['no_of_family'],"children"=>$family_count];
return $data;
}
Output: Which gives me the resulting array:
Array
(
[0] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Sciuridae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Marmota
[children] => Array
(
[0] => Array
(
[name] => Marmota himalayan
[children] => Himalayan Marmot
)
)
)
)
)
)
)
[1] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
)
)
)
[2] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Cricetidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Alticola
[children] => Array
(
[0] => Array
(
[name] => Alticola roylei
[children] => Royle's Mountain V
)
)
)
)
)
)
)
)

Get a specific value from a multidimensional array provided by AgileCRM

I'm trying to get the first deal ID from AgileCRM.
When using:
$test = json_decode($deal, true);
print_r($test);
I get the following result:
Array (
[0] => Array (
[colorName] => WHITE
[id] => 5686812383117312
[apply_discount] =>
[discount_value] => 0
[discount_amt] => 0
[discount_type] => Value
[name] => New Home Loan
[contact_ids] => Array (
[0] => 5645056174194688
)
[custom_data] => Array (
)
[products] => Array (
)
[description] => New Lead
[expected_value] => 0
[milestone] => New Loan
[probability] => 10
[close_date] => 1521192269
[created_time] => 1510824270
[milestone_changed_time] => 0
[entity_type] => deal
[notes] => Array (
)
[note_ids] => Array (
)
[note_created_time] => 0
[pipeline_id] => 5719238044024832
[archived] =>
[lost_reason_id] => 0
[deal_source_id] => 0
[total_deal_value] => 0
[updated_time] => 1510824270
[isCurrencyUpdateRequired] => 1
[currency_conversion_value] => 0
[tags] => Array (
)
[tagsWithTime] => Array (
)
[contacts] => Array (
[0] => Array (
[id] => 5645056174194688
[type] => PERSON
[properties] => Array (
[0] => Array (
[type] => SYSTEM
[name] => first_name
[value] => piet
)
[1] => Array (
[type] => SYSTEM
[name] => last_name
[value] => pompies
)
[2] => Array (
[type] => SYSTEM
[name] => name
[value] =>
)
)
)
)
[owner] => Array (
[id] => 5178546118721536
[domain] => domainname
[email] => myemail#email.com
[phone] =>
[name] => Piet Pompies
[pic] => https://d1gwclp1pmzk26.cloudfront.net/img/gravatar/48.png
[schedule_id] => Piet Pompies
[calendar_url] => https://homeside.agilecrm.com/calendar/Piet_Pompies
[calendarURL] => https://homeside.agilecrm.com/calendar/Piet_Pompies
)
)
)
I want to echo "5686812383117312" from "[id] => 5686812383117312" (4th line in the array above)
I've tried "foreach" statements but my expertise on it is limited and can't seem to get it right.
Any help will be appreciated.
In order to access the ID field you should:
get the array's first key
Access the required field
Array:
Array ( //$test
[0] => Array ( //first key [0]
[colorName] => WHITE
[id] => 5686812383117312 //the required field ['id']
[apply_discount] =>
PHP:
$test = json_decode($deal, true);
print_r($test);
echo $test[0]['id']; //Output: 5686812383117312

Filtering multi-dimensional array

I have an array like below and i am trying to filter entries that have a certain label or that are empty (not set). This is however not working. I guess it is do the fact that it is multi-dimensional. Anyone?
My array:
Array
(
[0] => Array
(
[id] => app_i-have
[type] => checkbox
[props] => Array
(
[required] => 0
[label] => I have
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => mobile studio
)
[1] => Array
(
[baseline] => 0
[value] => makeup artist
)
)
)
)
[1] => Array
(
[id] => app_customers
[type] => select
[props] => Array
(
[required] => 0
[label] => Customers
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => Private
)
[1] => Array
(
[baseline] => 0
[value] => Business
)
)
)
)
[2] => Array
(
[id] => app_exclude
[type] => select
[props] => Array
(
[required] => 0
[label] => Exclude
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => option 1
)
[1] => Array
(
[baseline] => 0
[value] => option 2
)
)
)
)
[3] => Array
(
[id] => app_exclude-2
[type] => input_text
[props] => Array
(
[required] => 0
[label] => Exclude 2
[tip] =>
)
)
)
My code:
function get_listing_cfs() {
global $wpdb;
$serialized=$wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='va_form'");
$array=unserialize($serialized);
echo '<pre>'.print_r($array, true).'</pre>';
$source = array_filter($array, function($el) {
return !(
$el['label'] == 'Exclude' ||
$el['label'] == 'Exclude 2' ||
!isset($el['label']) ||
empty($el['value']) ||
!isset($el['value'])
);
});
echo '<pre>'.print_r($source, true).'</pre>';
}
So I am trying to filter out the last 2 entries within the array and also filter out any entries that have an empty label or an empty value. I am doing this within a function that i want to use in my wordpress installation. Who can help me out?
Array_filter loops over the old array and returns only the results that will return true.
Your single element will look like this:
Array
(
[id] => app_i-have
[type] => checkbox
[props] => Array
(
[required] => 0
[label] => I have
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => mobile studio
)
[1] => Array
(
[baseline] => 0
[value] => makeup artist
)
)
)
)
Which means that instead of $el['label'], you need to do $el['props']['label'].
//Looping internal options array
foreach ($el['props']['options'] as $sub){
if (empty($sub['value'])) //save the return value?
}

how to check if value in mutidimension array are the same?

if I have multidimensional array like this
Array
(
[0] => Array
(
[value] => Dummy
[key] => 5501____I2-6
)
[1] => Array
(
[value] => Dummy
[key] => 5497____I2-2
)
[2] => Array
(
[value] => Dummy
[key] => 5502____I2-7
)
[3] => Array
(
[value] => Dummy
[key] => 5499____I2-4
)
[4] => Array
(
[value] => Dummy
[key] => 5496____I2-1
)
[5] => Array
(
[value] => Dummy
[key] => 5500____I2-5
)
[6] => Array
(
[value] => Dummy
[key] => 5503____I2-8
)
[7] => Array
(
[value] => Dummy
[key] => 5498____I2-3
)
)
How can I check if each value in [value] => are the same ?
thank for help
For two-dimensional array you can check duplicate value by following.
$array = array();
$temp_array=array();
$array[0]['value']='Dummy';
$array[0]['key']='1';
$array[1]['value']='Dummy';
$array[1]['key']='2';
$array[2]['value']='Not Dummy';
$array[2]['key']='3';
foreach($array as $k=>$step1){
if(in_array($step1['value'],$temp_array)){
// In this you can find out duplicate values.
}
$temp_array[$k]=$step1['value'];
}
thank everyone for help.
I found the way for fix it now.
I'm not sure it 's best way but It can fix this problem for me.
$data = Array
(
[0] => Array
(
[value] => Dummy
[key] => 5501____I2-6
)
[1] => Array
(
[value] => Dummy
[key] => 5497____I2-2
)
[2] => Array
(
[value] => Dummy
[key] => 5502____I2-7
)
[3] => Array
(
[value] => Dummy
[key] => 5499____I2-4
)
[4] => Array
(
[value] => Dummy
[key] => 5496____I2-1
)
[5] => Array
(
[value] => Dummy
[key] => 5500____I2-5
)
[6] => Array
(
[value] => Dummy
[key] => 5503____I2-8
)
[7] => Array
(
[value] => Dummy
[key] => 5498____I2-3
)
)
$newarray = array();
for ($i=0; $i < count($data); $i++) {
array_push($newarray,$json_data[$i]['value']);
}
if (count(array_unique($newarray)) == 1){
echo 'The same';
}
else{
echo 'not The same';
}

PHP - updating multidimensional arrays

Using foreach loops in PHP I would like to add ids to the following object...
$array_before
Array
(
[1111] => Array
(
[Name] => Name A
[Subcats] => Array
(
[11111] => Array
(
[Name] => Name A.1
)
[11112] => Array
(
[Name] => Name A.2
)
)
)
[2222] => Array
(
[Name] => Name B
[Subcats] => Array
(
[22221] => Array
(
[Name] => Name B.1
)
[22222] => Array
(
[Name] => Name B.2
)
)
)
)
... so it looks similar to the below:
$array_after
Array
(
[1111] => Array
(
[Id] => 1
[Name] => Name A
[Subcats] => Array
(
[11111] => Array
(
[Id] => 1
[Name] => Name A.1
)
[11112] => Array
(
[Id] => 2
[Name] => Name A.2
)
[11113] => Array
(
[Id] => 3
[Name] => Name A.2
)
)
)
[2222] => Array
(
[Id] => 2
[Name] => Name B
[Subcats] => Array
(
[22221] => Array
(
[Id] => 1
[Name] => Name B.1
)
[22222] => Array
(
[Id] => 2
[Name] => Name B.2
)
)
)
)
Could someone point me in the right direction?
Thanks,
LG
Try this:
$id = 0;
array_walk($array,function(&$a) use (&$id) {$a['id'] = ++$id;});
This will modify the originl array to add the IDs, rather than create a new one.

Categories