【问题标题】:Retrieve some Array data from a Submit form in cakePHP从 cakePHP 的提交表单中检索一些数组数据
【发布时间】:2010-07-16 15:54:24
【问题描述】:

我正在学习 cakePHP 1.26。

我有一个 HTML Select 标记,其中包含如下选项:

<form method="post" action="/testing">  
<table border="1">  
<tr>    
<td>
<select name="data[Test][number]">  
<option name="editquote" value="[29,1]">One</option>    
<option name="editquote" value="[24,2]">Two</option>    
</select>   
</td>   
<tr>    
<td>    
<input type="submit" value="Send" class="mybutton"> 
</td>   
</tr>   
</table>    
</form> 

我选择了选项一并提交了表单。
这是 cakePHP 内置函数 Debug() 的结果

Array
(
    [Test] => Array
        (
            [number] => [29,1]
        )

)

我尝试使用以下代码从数据中获取两个数字(即本示例中的 29 和 1),但未能成功

$myData=$this->data;
$myData['Test']['number'];  // [29, 1]

我应该怎么做才能分别得到这两个数字?

【问题讨论】:

    标签: cakephp cakephp-1.2


    【解决方案1】:

    你可以用PHP explode试试这个。

    $numbers = explode(',', trim($myData['Test']['number'], '[]'));
    $numbers[0]; //29
    $numbers[1]; //1
    

    【讨论】:

    • 也更新了 trim(),虽然在源数据中去掉 [] 可能更容易?
    猜你喜欢
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多