Reputation: 621
I'm hacking Expression Engine to enable the use of multiselect, radio and checkbox custom field types within the members profile form.
The model which parses the form and commits the update query submits all values from the form in one array variable - '$data'. One of the array values within $data is another array coming from a multiselect field type - so when the query is submitted it returns an error...
Unknown column 'Array' in 'field list'
UPDATE `apcims_member_data` SET `m_field_id_1` = '', `m_field_id_2` = Array WHERE `member_id` = '2'
So I need to implode any arrays within the $data array before the SQL is executed.
Is there a function something like...
foreach($data AS $value) {
if($value(is_array)) { $value = implode("|", $value); }
}
...then reinsert at the original index or position?
Any help appreciated.
Upvotes: 0
Views: 1566
Reputation: 3249
This is best use for new mapping function with anonymous function (since PHP 5.3)
<?php
$data = array('a' => array(1, 2, 3), 'b' => 9, 'c' => array(4, 5, 6));
$data = array_map(function($value) {
return is_array($value) ? implode('|', $value) : $value;
}, $data);
var_dump($data);
?>
Upvotes: 1
Reputation: 3601
You were pretty close. The method you are looking for is is_array
. Also, foreach
, can give you the index as well as the value so that you can update the value in the array yourself.
<?php
$data =array( 'a' => array( 1,2,3 ), 'c' => array( 4,5,6 ) );
foreach($data AS $key => $value) {
if(is_array($value))
{
$data[ $key ] = implode("|", $value);
}
}
var_dump( $data );
?>
Upvotes: 1