PHP recursion not going back to foreach loop - php

Need some guidance with some PHP recursion, I'm looping through some data comparing it against a Map, and if a form (in the data) has a subform it needs to be added to the DB with the parents forms ID.
All seems to be working except a subform is an array as there can be multiple subforms of the same type.
Heres a brief structure:
Parent[0]
SF_1[0]
_SF_2[0]
_SF_2[1]
_SF_2[n]
SF_1[1]
SF_2[0]
SF_n[0]
So I add the parent in a separate function, then start the subform recursion (this bit works fine):
private function add_form_records($_data, $map)
{
$form = new FormItem();
foreach($map["fields"] as $item) {
$key = $item->field;
if(array_key_exists($key, $_data))
{
$form->column_data->$key = $_data[$key];
}
}
$form->list_name = $map["sp_list"];
$form->form_id = $this->id_count;
$form->__metadata->type = $this->get_list_id($form->list_name);
//Add the form to SP and get the ID back for the subforms
$id = $this->add_record($form);
foreach($map["subforms"] as $key=>$value) {
$this->add_subform_records($_data[$key], $value, $form->form_id);
}
}
Then heres the function that gets called to add the subforms and then call itself it the subform has subforms:
private function add_subform_records($_data, $_map, $_parent_id)
{
for ($i = 0; $i < count($_data); ++$i) {
$form = new FormItem();
foreach($_map["fields"] as $fieldItem)
{
$key = $fieldItem->field;
if(array_key_exists($key, $_data[$i]))
{
$form->column_data->$key = $_data[$i][$key];
}
}
$form->parent_id = $_parent_id;
$form->list_name = $_map["sp_list"];
$form->form_id = $this->id_count;
$form->form_name = $_map["subform_name"];
$form->__metadata->type = $this->get_list_id($form->list_name);
$id = $this->add_record($form);
if(array_key_exists('subforms', $_map))
{
foreach($_map["subforms"] as $key=>$value) {
foreach($_data as $subform)
{
if(array_key_exists($key, $subform))
{
$this->add_subform_records($subform[$key],$value, $form->form_id);
}
}
}
}
}
}
So the problem is it adds the first subform, goes to add its subforms (and does) but doesn't come back to the for ($i = 0; $i < count($_data); ++$i) to add the others.
Thanks!
$_data
array ( '_submit' => 'submit', '_submittedTime' => '1386837565194', '_submittedTimezoneOffset' => '11', 'ReportedBy' => 'test', 'DateAndTime' => '2013-12-12 19:38', 'Location' => 'hello', 'ClientName' => 'my', 'DiscussionType' => 'Meeting', 'SF_Attendees' => array ( 0 => array ( 'AttendeeName' => 'name', 'Company' => 'is', 'SF_Trip' => array ( 0 => array ( 'test1' => '/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCABgAIADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD401i0tNMKy3MqxIvzEn/PNee3UdzrN60FhAzrI5bgcYz+gr1PWtLXVLU28oJU8A46eo/z71UsNIs9JhEFrCF9T3J9zXNh6LqpSue9VxXJuZXh7w++l24+13DyMeduTtX6VvKyIuEVQBUNzNHbrvlbGTgD1Pp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ReWpkxnipDAoUsHBpyw85IWnlQiFz2HrQxpHL+J4hJouppjP8Aok5H1EbYr55h8L2Hj74y6R4a8QeOrPwjpmtC1jk1i9Gbe0BgUBpOQANy4ySAM5JA5r0f45fE+48DwW+naPBBdX17FPLNDJkjyApXsQQSTwf9g18n+JfiBeeILqCa90/7G0cCQL5bMAVUnBIPfkivKrJyqPsdHOoRS6nofiBdQ8H65rem6F4pa/i06/ms49TsZGSO6tzuiZ1I52SKendWx3rWMYu/DMYxnapT8iV/pXD6LHbXWnTodbtpGOM5EmcDnnK13OjzW76NJbi6RwrE5Cnv83ce9dmEXK7MxnLm1P/Z', '_action' => 'add', 'test1_mimetype' => 'image/jpeg', 'test2' => '', 'test3' => '', ), ), '_action' => 'add', ), 1 => array ( 'AttendeeName' => 'S', 'Company' => 'T', '_action' => 'add', ), ), 'ReasonforMeeting' => 'test', 'FollowUp' => '2013-12-19 19:38', 'UserEmail' => 'dev#dev.com', 'EmailCopyTo' => 's#s.com', 'SF_Attend' => array ( 0 => array ( 'AttendeeName' => 'test', 'Company' => 'test', '_action' => 'add', ), ), '_action' => 'add', '_uuid' => '63FD4677-C72A-4F1A-B711-963849B6840B', '_DateAndTime_time_offset' => '+11:00', '_FollowUp_time_offset' => '+11:00', )
$map
array ( 'sp_list' => 'ClientMeetingNote', 'fields' => array ( 0 => SchemaItem::__set_state(array( 'field' => 'ReportedBy', 'type' => 'textbox', )), 1 => SchemaItem::__set_state(array( 'field' => 'DateAndTime', 'type' => 'timestamp', )), 2 => SchemaItem::__set_state(array( 'field' => 'Location', 'type' => 'textbox', )), 3 => SchemaItem::__set_state(array( 'field' => 'ClientName', 'type' => 'textbox', )), 4 => SchemaItem::__set_state(array( 'field' => 'DiscussionType', 'type' => 'radio', )), 5 => SchemaItem::__set_state(array( 'field' => 'ReasonforMeeting', 'type' => 'text_area', )), 6 => SchemaItem::__set_state(array( 'field' => 'FollowUp', 'type' => 'timestamp', )), 7 => SchemaItem::__set_state(array( 'field' => 'Signature', 'type' => 'sketch_signature', )), 8 => SchemaItem::__set_state(array( 'field' => 'UserEmail', 'type' => 'email', )), 9 => SchemaItem::__set_state(array( 'field' => 'EmailCopyTo', 'type' => 'email', )), ), 'subforms' => array ( 'SF_Attendees' => array ( 'subform_name' => 'SF_Attendees', 'subform_data_name' => 'SF_Attendees', 'sp_list' => 'Attendees', 'fields' => array ( 0 => SchemaItem::__set_state(array( 'field' => 'AttendeeName', 'type' => 'textbox', )), 1 => SchemaItem::__set_state(array( 'field' => 'Company', 'type' => 'textbox', )), ), 'subforms' => array ( 'SF_Trip' => array ( 'subform_name' => 'SF_Trip', 'subform_data_name' => 'SF_Trip', 'sp_list' => 'Trip', 'fields' => array ( 0 => SchemaItem::__set_state(array( 'field' => 'test1', 'type' => 'camera', )), 1 => SchemaItem::__set_state(array( 'field' => 'test2', 'type' => 'image_library', )), 2 => SchemaItem::__set_state(array( 'field' => 'test3', 'type' => 'file_upload', )), ), ), ), ), 'SF_Attend' => array ( 'subform_name' => 'SF_Attend', 'subform_data_name' => 'SF_Attendees', 'sp_list' => 'Attendees', 'fields' => array ( 0 => SchemaItem::__set_state(array( 'field' => 'AttendeeName', 'type' => 'textbox', )), 1 => SchemaItem::__set_state(array( 'field' => 'Company', 'type' => 'textbox', )), ), 'subforms' => array ( 'SF_Trip' => array ( 'subform_name' => 'SF_Trip', 'subform_data_name' => 'SF_Trip', 'sp_list' => 'Trip', 'fields' => array ( 0 => SchemaItem::__set_state(array( 'field' => 'test1', 'type' => 'camera', )), 1 => SchemaItem::__set_state(array( 'field' => 'test2', 'type' => 'image_library', )), 2 => SchemaItem::__set_state(array( 'field' => 'test3', 'type' => 'file_upload', )), ), ), ), ), ), )

Figured it out!! Had to restructure code as it was looping through the data twice, recursion was working fine I was sending it the wrong data.

Related

PHP How to access array data

I get passed an array in a callback function. Now I want access a value of this array.
I can dump this array into a file with var_export($fields[1], True)
Here is the content of the export:
helper_plugin_bureaucracy_fieldtextbox::__set_state(array(
'mandatory_args' => 2,
'opt' =>
array (
'cmd' => 'textbox',
'label' => 'Kunde',
'display' => 'Kunde',
'value' => 'myimportantdata',
),
'tpl' =>
array (
'_elem' => 'textfield',
'_text' => '##DISPLAY##',
'_class' => '##CLASS##',
'id' => '##ID##',
'name' => '##NAME##',
'value' => '##VALUE##',
'class' => 'edit required',
'required' => 'required',
),
'checks' =>
array (
),
'hidden' => false,
'error' => false,
'checktypes' =>
array (
'/' => 'match',
'<' => 'max',
'>' => 'min',
),
))
I want to access the value of opt->value whitch is 'myimportantdata' in this case.
How can I achieve this?
I already tried:
$mydata = $fields[1]['helper_plugin_bureaucracy_fieldtextbox']['opt'];
$mydata = $fields[1][0][2];
$mydata = $fields[1]->helper_plugin_bureaucracy_fieldtextbox['opt'];
without success :-(
fields[1] contains an object of the type 'helper_plugin_bureaucracy_fieldtextbox'. The access to the properties of the object such as 'opt' must be done with the -> operator.
$opt = $fields[1]->opt;
$opt_value = $fields[1]->opt['value']; //myimportantdata
$data = array(
'mandatory_args' => 2,
'opt' =>
array (
'cmd' => 'textbox',
'label' => 'Kunde',
'display' => 'Kunde',
'value' => 'myimportantdata',
),
'tpl' =>
array (
'_elem' => 'textfield',
'_text' => '##DISPLAY##',
'_class' => '##CLASS##',
'id' => '##ID##',
'name' => '##NAME##',
'value' => '##VALUE##',
'class' => 'edit required',
'required' => 'required',
),
'checks' =>
array (
),
'hidden' => false,
'error' => false,
'checktypes' =>
array (
'/' => 'match',
'<' => 'max',
'>' => 'min',
),
);
echo $data["opt"]["value"];

Replace key in array, with keeping order intact

I would like to replace keys in arrays, because I will move them on two indexes up.
Problem that I am facing is that those are containing same names which will not be ok, if i want to move them up.
This is how array looks like.
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '3; top',
),
1 => array(
'Name' => 'level',
'value' => '1; blue',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GSSD154',
),
),
),
),
1 => array(
'messagetype' => 'Information',
'visibility' => 'Private',
'info' => array(
0 => array(
'urgency' => 'Minor',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '1; left',
),
1 => array(
'Name' => 'level',
'value' => '1; red',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GBECS23',
),
),
),
),
),
),
),
),
);
and this is how I would like the output to be with changing keys in Name0, Name1, which are inside params.
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params' => array(
0 => array(
'Name0' => 'display',
'value0' => '3; top',
),
1 => array(
'Name1' => 'level',
'value1' => '1; blue',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GSSD154',
),
),
),
),
1 => array(
'messagetype' => 'Information',
'visibility' => 'Private',
'info' => array(
0 => array(
'urgency' => 'Minor',
'params' => array(
0 => array(
'Name0' => 'display',
'value0' => '1; left',
),
1 => array(
'Name1' => 'level',
'value1' => '1; red',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GBECS23',
),
),
),
),
),
),
),
),
);
I have tried with a lots of examples over this website, but could not find one to achieve this.
Code that I used from
How to replace key in multidimensional array and maintain order
function replaceKey($subject, $newKey, $oldKey) {
// if the value is not an array, then you have reached the deepest
// point of the branch, so return the value
if (!is_array($subject)) {
return $subject;
}
$newArray = array(); // empty array to hold copy of subject
foreach ($subject as $key => $value) {
// replace the key with the new key only if it is the old key
$key = ($key === $oldKey) ? $newKey : $key;
// add the value with the recursive call
$newArray[$key] = replaceKey($value, $newKey, $oldKey);
}
return $newArray;
}
$s = replaceKey($list, 'Name0', 'Name');
print "<PRE>";
print_r($s);
at the moment I get this output:
[0] => Array
(
[Name0] => display
[value] => 1; left
)
[1] => Array
(
[Name0] => level
[value] => 1; red
)
any help would be appreciated. regards
A very strange question, but why not?
The following function returns nothing (a procedure) and changes the array in-place using references but feel free to rewrite it as a "real" function (without references and with a return statement somewhere).
The idea consists to search for arrays, with numeric keys and at least 2 items, in which each item has the Name and value keys. In other words, this approach doesn't care about paths where the targets are supposed to be:
function replaceKeys(&$arr) {
foreach ($arr as &$v) {
if ( !is_array($v) )
continue;
$keys = array_keys($v);
if ( count($keys) < 2 ||
$keys !== array_flip($keys) ||
array_keys(array_merge(...$v)) !== ['Name', 'value'] ) {
replaceKeys($v);
continue;
}
foreach ($v as $k => &$item) {
$item = array_combine(["Name$k", "value$k"], $item);
}
}
}
replaceKeys($list);
print_r($list);
demo

Get woocommerce user active memberships - extract data from php multi array

array (
0 =>
WC_Memberships_User_Membership::__set_state(array(
'id' => 52330,
'plan_id' => 18952,
'plan' =>
WC_Memberships_Membership_Plan::__set_state(array(
'id' => 18952,
'name' => 'Level 2 Access Until June',
'slug' => 'level-2-access-until-june',
'post' =>
WP_Post::__set_state(array(
'ID' => 18952,
'post_author' => '8',
'post_date' => '2017-09-11 11:05:13',
'post_date_gmt' => '2017-09-11 15:05:13',
)),
'access_method_meta' => '_access_method',
'rules' =>
array (
),
)),
'user_id' => '5213',
'status' => 'wcm-active',
'post' =>
WP_Post::__set_state(array(
'ID' => 52330,
'post_title' => 'Auto Draft',
)),
'product' => NULL,
'renewal_login_token_meta' => '_renewal_login_token',
'locked_meta' => '_locked',
)),
1 =>
WC_Memberships_User_Membership::__set_state(array(
'id' => 28639,
'plan_id' => 27786,
'plan' =>
WC_Memberships_Membership_Plan::__set_state(array(
'id' => 27786,
'name' => 'Level 1 – Ethical and Professional Standards',
'slug' => 'level-1-ethical-and-professional-standards',
'post' =>
WP_Post::__set_state(array(
'ID' => 27786,
'post_author' => '8',
'post_mime_type' => '',
'comment_count' => '0',
'filter' => 'raw',
)),
'access_method_meta' => '_access_method',
'email_content_meta' => '_email_content',
'rules' =>
array (
),
)),
'user_id' => '5213',
'status' => 'wcm-active',
'post' =>
WP_Post::__set_state(array(
'ID' => 28639,
'post_author' => '5213',
'post_date' => '2017-11-27 21:41:06',
'filter' => 'raw',
)),
'product' => NULL,
'type' => 'manually-assigned',
'locked_meta' => '_locked',
)),
);
I have this array given by woocommerce method: wc_memberships_get_user_active_memberships($user_id);
I need to extract out the name of the memberships the user has, it must be in a loop as the user can have multiple memberships. I am looking to get back name = Level 2 Access Until June, Level 1 – Ethical and Professional Standards
From what you've posted, I would try this:
$memberships_info = wc_memberships_get_user_active_memberships($user_id);
foreach ($memberships_info as $membership) {
echo $membership['plan']['name'];
}
From the woocommerce docs, it looks like you can specify what status of active memberships. So you might want to do that to include all membership types:
$args = array(
'status' => array( 'active', 'complimentary', 'pending' ),
);
$memberships_info = wc_memberships_get_user_active_memberships($user_id, $args); // adding the args bit to get all memberships
foreach ($memberships_info as $membership) {
echo $membership['plan']['name'];
}
Woocommerce docs: https://docs.woocommerce.com/document/woocommerce-memberships-function-reference/
This is what finally worked for me.
$user_id = get_current_user_id();
$memberships = wc_memberships_get_user_active_memberships($user_id);
foreach ($memberships as $membership => $val) {
echo $val->plan->name . '<br>';
};

sugarCRM : creating relationship using module loader, package not installing

I am trying to create relationships through module loader in SugarCRM. But the problem is my package is stopped installing after 55%. When I am viewing display log the error is:-
Failed to copy
cache/upgrades/temp/SYWr9G/custom/metadata/accounts_contacts_1MetaData.php
custom/metadata/accounts_contacts_1MetaData.php
I tried to change permissions also but at some point they through internal server error 500. Following are the code which I am using.
1.In manifest.php file :
$installdefs = array(
'id' => 'package_20170804',
'copy' => array(
0 => array(
'from' => '<basepath>/accounts_contacts_1MetaData.php',
'to' => 'custom/metadata/accounts_contacts_1MetaData.php',
),
1 => array(
'from' => '<basepath>/accounts_contacts_1.php',
'to' => 'custom/Extension/application/Ext/TableDictionary/accounts_contacts_1.php',
),
),
'relationships'=>array (
array (
'module'=> 'Accounts',
'meta_data'=>'<basepath>/custom/metadata/accounts_contacts_1MetaData.php',
'module_vardefs'=>'<basepath>/custom/Extension/application/Ext/TableDictionary/accounts_contacts_1.php'
)
),
);
2. In accounts_contacts_1MetaData.php file :
<?php
$dictionary["accounts_contacts_1"] = array (
'true_relationship_type' => 'one-to-many',
'from_studio' => true,
'relationships' =>
array (
'accounts_contacts_1' =>
array (
'lhs_module' => 'Accounts',
'lhs_table' => 'accounts',
'lhs_key' => 'id',
'rhs_module' => 'Contacts',
'rhs_table' => 'contacts',
'rhs_key' => 'id',
'relationship_type' => 'many-to-many',
'join_table' => 'accounts_contacts_1_c',
'join_key_lhs' => 'accounts_contacts_1accounts_ida',
'join_key_rhs' => 'accounts_contacts_1contacts_idb',
),
),
'table' => 'accounts_contacts_1_c',
'fields' =>
array (
0 =>
array (
'name' => 'id',
'type' => 'varchar',
'len' => 36,
),
1 =>
array (
'name' => 'date_modified',
'type' => 'datetime',
),
2 =>
array (
'name' => 'deleted',
'type' => 'bool',
'len' => '1',
'default' => '0',
'required' => true,
),
3 =>
array (
'name' => 'accounts_contacts_1accounts_ida',
'type' => 'varchar',
'len' => 36,
),
4 =>
array (
'name' => 'accounts_contacts_1contacts_idb',
'type' => 'varchar',
'len' => 36,
),
),
'indices' =>
array (
0 =>
array (
'name' => 'accounts_contacts_1spk',
'type' => 'primary',
'fields' =>
array (
0 => 'id',
),
),
1 =>
array (
'name' => 'accounts_contacts_1_ida1',
'type' => 'index',
'fields' =>
array (
0 => 'accounts_contacts_1accounts_ida',
),
),
2 =>
array (
'name' => 'accounts_contacts_1_alt',
'type' => 'alternate_key',
'fields' =>
array (
0 => 'accounts_contacts_1contacts_idb',
),
),
),
);
3. In accounts_contacts_1.php file:
<?php
include('custom/metadata/accounts_contacts_1MetaData.php');
?>
I just wanted to create a field in relationship that's all . Maybe I am missing somewhere in manifest file or i needed to include some additional file.
I solved it. You can follow following steps to create relationships in SugarCRM through module loader.
Step 1:- Give permission to your sugar directory according to following
For Linux: http://support.sugarcrm.com/Knowledge_Base/Platform_Management/Required_File_System_Permissions_on_Linux/
For Windows:
http://support.sugarcrm.com/Knowledge_Base/Platform_Management/Required_File_System_Permissions_on_Windows_With_IIS/
Step 2:- In manifest.php
<?php
$manifest = array(
'acceptable_sugar_flavors' => array('CE','PRO','CORP','ENT','ULT'),
'acceptable_sugar_versions' => array(
'exact_matches' => array(),
'regex_matches' => array('(.*?)\\.(.*?)\\.(.*?)$'),
),
'author' => 'Ravi Ranjan',
'description' => 'Relationship',
'icon' => '',
'is_uninstallable' => true,
'name' => 'custom relation',
'published_date' => '2017-08-10 2017 11:45:04',
'type' => 'module',
'version' => '20170810',
);
$installdefs = array(
'id' => 'package_20170810',
'copy' => array(
0 => array(
'from' => '<basepath>/accounts_contacts_1MetaData.php',
'to' => 'custom/metadata/accounts_contacts_1MetaData.php',
),
1 => array(
'from' => '<basepath>/accounts_contacts_1.php',
'to' => 'custom/Extension/application/Ext/TableDictionary/accounts_contacts_1.php',
),
),
);
?>
Step 3:- The contents of both file accounts_contacts_1MetaData.php and accounts_contacts_1.php will be same as you can see in question.
Step 4:- Compress all three files and upload through module loader, after installation quick repair and rebuild.
Go in Studio > Accounts > Relationships You will see a new field there name accounts_contacts_1 That's what I wanted to create.

Get values from unserialized data

I have a serialized field in my database. I can get the contents of this field and unserialize them however I am unsure how to get certain values of these. I ultimately need a foreach of each item and value from the data.
In the below example I need to be able to get the following:
Main Image Replacement:
BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14.JPG Complimentary:
Comp Text Quote Code: Code Text
I need the label as one variable and the value as another within a foreach. These labels and values are variable so I cannot manually get this data by their labels.
array (
0 =>
array (
'mode' => 'builder',
'cssclass' => '',
'hidelabelinorder' => '',
'hidevalueinorder' => '',
'element' =>
array (
'type' => 'radio',
'rules_type' =>
array (
'Reprint_0' =>
array (
0 => '',
),
'Edit Artwork_1' =>
array (
0 => '',
),
),
'_' =>
array (
'price_type' => '',
),
),
'name' => '',
'value' => 'Edit Artwork',
'price' => '',
'section' => '58073632e582b5.35893028',
'section_label' => '',
'percentcurrenttotal' => 0,
'currencies' =>
array (
),
'price_per_currency' =>
array (
'GBP' => '',
),
'quantity' => 1,
'multiple' => '1',
'key' => 'Edit Artwork_1',
'use_images' => '',
'changes_product_image' => '',
'imagesp' => '',
'images' => '',
),
1 =>
array (
'mode' => 'builder',
'cssclass' => '',
'hidelabelinorder' => '',
'hidevalueinorder' => '',
'element' =>
array (
'type' => 'radio',
'rules_type' =>
array (
'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14.JPG_0' =>
array (
0 => '',
),
'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-21.JPG_1' =>
array (
0 => '',
),
'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-77.JPG_2' =>
array (
0 => '',
),
),
'_' =>
array (
'price_type' => '',
),
),
'name' => 'Main Image Replacement',
'value' => 'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14.JPG',
'price' => '',
'section' => '58073632e582d2.46631826',
'section_label' => 'Main Image Replacement',
'percentcurrenttotal' => 0,
'currencies' =>
array (
),
'price_per_currency' =>
array (
'GBP' => '',
),
'quantity' => 1,
'multiple' => '1',
'key' => 'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14.JPG_0',
'use_images' => 'images',
'changes_product_image' => '',
'imagesp' => '',
'images' => 'http://burhill.immaculate.co.uk/wp-content/uploads/2016/10/BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14-150x150.jpg',
),
2 =>
array (
'mode' => 'builder',
'cssclass' => 'col-6',
'hidelabelinorder' => '',
'hidevalueinorder' => '',
'element' =>
array (
'type' => 'textfield',
'rules_type' =>
array (
0 =>
array (
0 => '',
),
),
'_' =>
array (
'price_type' => '',
),
),
'name' => 'Complimentary',
'value' => 'Comp Text',
'price' => '',
'section' => '58073632e582f4.32183997',
'section_label' => 'Complimentary',
'percentcurrenttotal' => 0,
'currencies' =>
array (
),
'price_per_currency' =>
array (
),
'quantity' => 1,
),
3 =>
array (
'mode' => 'builder',
'cssclass' => 'col-6',
'hidelabelinorder' => '',
'hidevalueinorder' => '',
'element' =>
array (
'type' => 'textfield',
'rules_type' =>
array (
0 =>
array (
0 => '',
),
),
'_' =>
array (
'price_type' => '',
),
),
'name' => 'Quote Code',
'value' => 'Code Text',
'price' => '',
'section' => '58073632e58317.46363272',
'section_label' => 'Quote Code',
'percentcurrenttotal' => 0,
'currencies' =>
array (
),
'price_per_currency' =>
array (
),
'quantity' => 1,
),
)
foreach($your_array as $key => $value) {
$your_value = $value['value'];
$your_label = $value['section_label'];
}
This should work for you as long as i got the right keys there.

Categories