Filtering multi-dimensional array - php

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

Related

Compare two multidimensional key values & combine non duplicates

I have two multidimensional arrays. I'm looping through both arrays, checking for certain values & creating a new array.
$full_cats array
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
)
$sub_cats array
Array
(
[0] => Array
(
[sub_cats] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
[1] => Array
(
[sub_cats] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
[2] => Array
(
[sub_cats] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
I'm looping through both arrays to check if $sub_cat['sub_cats']['parent'] value is the same as the $full_cat['parent_cats']['id'] value. If this is true, both values are added to the $master_cats array.
$master_cats = array();
foreach ($sub_cats as $sub_cat) {
foreach ($full_cats as $full_cat) {
if( $sub_cat['sub_cats']['parent'] == $full_cat['parent_cats']['id'] ){
$master_cats[] = array(
"parent_cats" => array(
$full_cat['parent_cats'],
),
"sub_cats" => array(
$sub_cat['sub_cats'],
)
);
};
};
};
The $master_cats output -
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[2] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
I'm having two issues with the $master_cats array.
1st problem - As you can see, index 0 & 1 have the same [parent_cats] values. I only want to add the [parent_cats] key/values if they dont already exist.
2nd Problem - the $master_cats array index 0 & 1, the sub_cats array, some values are different but both have the same [sub_cats][parent] => 384 so they belong in the same array index, eg 0.
Below is what I'm hoping to achieve with the $master_cats array from the foreach/loop above
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384 <-- Parent ID
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384 <-- belongs to [parent_cats][id]
)
[1] => array
(
[id] => 385
[name] => Beers
[parent] => 384 <-- belongs to [parent_cats][id]
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387 <-- Parent ID
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387 <-- belongs to [parent_cats][id]
)
)
)
)
Here's one way to go about it. Note: your array structures are a bit over-nested... but here's a solution for the given structure
$all = [];
$tmpsubs = [];
foreach ($full_cats as $parent) {
$tmp = ['parent_cats' => $parent['parent_cats'], 'sub_cats' => []];
foreach ($sub_cats as $sub) {
if ($sub['sub_cats']['parent'] == $parent['parent_cats']['id']) {
$tmp['sub_cats'][]=$sub['sub_cats'];
}
}
$all[] = $tmp;
}
print_r($all);
Example: https://3v4l.org/01oLF
Output:
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
[1] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
Note: here's an example of simplifying your array structure (which would require changing the code in my answer, but ultimately might make your life a little easier)
$parent_cats= array(
array(
"id" => "384",
"name" => "Beers & Ales",
"parent" => "0"
),
array(
"id" => "387",
"name" => "Wines",
"parent" => "0"
));

How to unset specific array in nested foreach?

I have an output array like this:
Array
(
[0] => Array
(
[item] => null
[count] => 0
[child] => Array
(
[Dagadu Bocah] => Array
(
[item] => Dagadu Bocah
[count] => 47
[child] => Array
(
[HirukPikuk] => Array
(
[item] => HirukPikuk
[count] => 5
[child] => Array
(
[DGD] => Array
(
[item] => DGD
[count] => 1
[child] =>
)
)
)
[DGD] => Array
(
[item] => DGD
[count] => 5
[child] => Array
(
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 1
[child] =>
)
)
)
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 2
[child] =>
)
)
)
)
)
)
in my expectations I can use the loop as I asked earlier in this question by doing repetitions in such a way as to delete certain arrays to eliminate different parts of the array which is an array that is above the array which has three items namely 'item', 'count' and 'child' and how to produce arrays like this from the array above?
Array
(
[0] => Array
(
[item] => null
[count] => 0
[child] => Array
(
[0] => Array
(
[item] => Dagadu Bocah
[count] => 47
[child] => Array
(
[0] => Array
(
[item] => HirukPikuk
[count] => 5
[child] => Array
(
[0] => Array
(
[item] => DGD
[count] => 1
[child] =>
)
)
)
[1] => Array
(
[item] => DGD
[count] => 5
[child] => Array
(
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 1
[child] =>
)
)
)
[2] => Array
(
[item] => Malioboroman
[count] => 2
[child] =>
)
)
)
)
)
)
It seems you want to convert the child arrays from associative arrays to indexed arrays.
Assuming your data is called $data, here is a recursive function you could use:
function convert(&$data) {
foreach ($data as $row) {
if(isset($row["child"])) {
$row["child"] = array_values($row["child"]);
convert($row["child"]);
}
}
}
// call it for each row
foreach($data as $row) convert($row);

Extract array with same name from multidimensional array

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.

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

array one format to another

I have one array in two format. I want to change array from
Array
(
[step_number] => 4
[app_id] => Array
(
[0] => 2
[1] => 3
)
[formdata] => Array
(
[0] => Array
(
[name] => app_id[]
[value] => 2
)
[1] => Array
(
[name] => app_id[]
[value] => 3
)
[2] => Array
(
[name] => fieldval[2][2][]
[value] => 1
)
[3] => Array
(
[name] => fieldval[3][3][]
[value] => 200
)
[4] => Array
(
[name] => fieldval[3][3][]
[value] => day
)
[5] => Array
(
[name] => title
[value] => new plan
)
[6] => Array
(
[name] => feature_plan
[value] => 3
)
[7] => Array
(
[name] => plan_type
[value] => free
)
[8] => Array
(
[name] => price
[value] =>
)
[9] => Array
(
[name] => sell_type
[value] => us
)
)
)
this format to
Array
(
[app_id] => Array
(
[0] => 2
[1] => 3
)
[fieldval] => Array
(
[2] => Array
(
[2] => Array
(
[0] => 1
)
)
[3] => Array
(
[3] => Array
(
[0] => 200
[1] => day
)
)
)
[title] => new plan
[feature_plan] => 3
[plan_type] => free
[price] =>
[sell_type] => us
)
these are are one array into two format. i have data in to first array format and i want to change that format to second array type format.
please tell me how i am trying this for 2 days but not succeed.
Here is a function you could use to produce that conversion:
function convert_formdata($input) {
$output = array();
foreach($input['formdata'] as $data) {
$keys = preg_split("#[\[\]]+#", $data['name']);
$value = $data['value'];
$target = &$output;
foreach($keys as $key) {
// Get index for "[]" reference
if ($key == '') $key = count($target);
// Create the key in the parent array if not there yet
if (!isset($target[$key])) $target[$key] = array();
// Move pointer one level down the hierarchy
$target = &$target[$key];
}
// Write the value at the pointer location
$target = $value;
}
return $output;
}
You would call it like this:
$output = convert_formdata($input);
See it run on eval.in for the given input. The output is:
array (
'app_id' =>
array (
0 => 2,
1 => 3,
),
'fieldval' =>
array (
2 =>
array (
2 =>
array (
0 => 1,
),
),
3 =>
array (
3 =>
array (
0 => 200,
1 => 'day',
),
),
),
'title' => 'new plan,',
'feature_plan' => 3,
'plan_type' => 'free',
'price' => NULL,
'sell_type' => 'us',
)

Categories