Php 我想在两个CodeIgniter函数之间发送数据

Php 我想在两个CodeIgniter函数之间发送数据,php,codeigniter,Php,Codeigniter,我必须从一个函数传递另一个函数的值 我写了第一个这样的函数 function test1(){ $product_details[] = array( 'product_id' => '1', 'count' => '2' ); $this->test2($product_details); } 我这样写了第二个函数。我必须保留这张$\美元的帖子 function

我必须从一个函数传递另一个函数的值

我写了第一个这样的函数

function test1(){
        $product_details[] = array(
            'product_id' => '1',
            'count'      => '2'
        );
        $this->test2($product_details);
}    
我这样写了第二个函数。我必须保留这张$\美元的帖子

function test2(){
        $product_details = $_POST['product_details'];
        foreach($product_details as $row){
            $this->db->insert('table',$row);
        }
}
它甚至没有打印发布的结果。。
如果您希望将数据从test1传递到test2,就像在test1中一样,那么非常感谢..:)

function test1()
{
    $product_details[] = array(
        'product_id' => '1',
        'count'      => '2'
    );
    $this->test2($product_details); # here you are trying to pass the $product_details to test2
}    
test2的签名必须反映:

function test2(array $product_details) # this is the function signature
{
    $product_details = $_POST['product_details']; # this line would currently override your $product_details even if you passed them from test1
    $product_details[] = $_POST['product_details']; # maybe this is what you meant?
    foreach($product_details as $row){
        $this->db->insert('table',$row); # I imagine you tried echo $row or var_dump($row) here before?
    }
}

第一个函数中的$product\u详细信息会发生什么变化?在我看来,这看起来像是死代码,除非你打算将它传递给第二个函数,在这种情况下,你需要调整函数签名。它将把所有数组值插入数据库,而函数签名又如何呢?我没有得到它。
function test2()
是你当前的函数签名。如果要将值从test1传递到test2,除了j4g0指出的关于第二个函数不带参数的内容之外,这个问题非常令人困惑,它应该类似于
函数test2(数组$product_details){…}
它甚至不打印发布的结果
。。。。这里面没有“打印”代码(甚至是任何类型的返回),你为什么希望它“打印”任何东西?使用这些代码都不起作用..因为函数test2上的$\u帖子。但我必须把它记在我的密码里。是的,你完全正确。但是在这个例子中,它需要一个空索引,比如->数组([0]=>Array([product\u id]=>4[quantity]=>3)[1]=>Array([product\u id]=>1[quantity]=>2)[2]=>),而不是使用这个方法。请检查一下,告诉我可以用这个吗。?如果(isset($\u POST['product\u details']){$product\u details=$\u POST['product\u details'];}它已经为我工作了。是,这将覆盖从test1传递的$product\u详细信息,前提是$product\u POST['product\u details']可用,这似乎是您想要实现的。....非常感谢…D..)