Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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 );
?>
share|improve this answer
    
Amazing! Thank you so much! –  digiwig Oct 31 '11 at 15:11
    
Your welcome. If I answered your question correctly, you should mark it as accepted. It will improve your acceptance rate which is what Dems is referring to. –  Brian Oct 31 '11 at 20:13
add comment

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);

?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.