CakePHP以错误的格式保存数组

时间:2014-09-19 09:24:02

标签: cakephp

我有这种格式的数组:

Array
(
   [0] => some text
   [1] => some text
   [2] => some text
   [3] => some text
   [4] => 
   [5] => 
   [6] => some text
   [7] => some text
   [8] => some text
   [9] => 
   [10] => 
   [11] => 
   [12] => 
   [13] => some text
   [14] => some text
)

此数组的长度可能会发生变化,有时ID如上所示为空白。

我需要将此数组保存到具有70列(所有VARCHAR)的MySQL表中,但不会以上述格式保存。我知道数组必须是这样的:

Array
(
    [ModelName] => Array
    (
        [fieldname1] => 'value'
        [fieldname2] => 'value'
    )
)

如何将上述数组修改为以下结构?所有列名都是str后跟列所在的数字(即str1,str2等)

1 个答案:

答案 0 :(得分:0)

将数组转换为蛋糕理解的内容,然后保存它

$modelArray = array('ModelName' => array());
foreach( $yourArray as $index => $value ){
    $modelArray['ModelName']['str' . $index] = $value
}
$this->ModelName->save($modelArray);