I have been a long time reader here and never posted but I was wondering if somebody could help me out with a problem I am having…
I have 2 arrays which print like this:
$fields = Array
(
[0] => Array
(
[field] => NewField
[label] => New Field
[required] => 1
)
[1] => Array
(
[field] => _Field2
[label] => Field 2
[required] => 0
)
)
$fields_data = Array
(
[0] => Array
(
[field_id] => 1
[field_name] => New Field
[db_name] => NewField
[field_type] => text
)
[1] => Array
(
[field_id] => 6
[field_name] => Field 2
[db_name] => _Field2
[field_type] => text
)
)
And I need to create an array which looks like this :
$new_arr = Array
(
[NewField] => Array
(
[field_id] => 1
[field_name] => New Field
[field_type] => text
[label] => New Field
[required] => 1
)
[_Field2] => Array
(
[field_id] => 6
[field_name] => Field 2
[field_type] => text
[label] => Field 2
[required] => 0
)
)
So what happens is the db_name become the array keys and then the 2 array where the field matches the db_name combine into one array.
Here is some code I have been trying to work with but obviously it is not working for whatever reason (PHP isn’t my strong point):
foreach($fields as $field){
$new_arr[$field['field']] = array();
foreach($fields_data as $tkey => $tval) {
if($fields_data[$tkey]['db_name'] = $fields['field']) {
$new_arr[$field['field']]['field_id'] = $fields_data[$tkey]['field_id'];
}
}
}
Obviously the above code would only create the field_id element in the array however as it is it isn’t working. I think having the 2 loops is overwriting the field_id value each time. But as I said php is not my strong point so any help on this would be much appreciated.
Thanks
This code will take the two arrays you’re given and turn them into the third. Hope it helps! Could probably use some tweaking, but hopefully it solves your problem.