获取另一个函数php中一个函数的数组输出 [英] Get array output of one function in another function php

查看:111
本文介绍了获取另一个函数php中一个函数的数组输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有下面的codeigniter控制器功能,

I have below controller function of codeigniter,

public function add_attachments($openid)
{
            $config = array(
                    'upload_path' => './uploads/attachments/',
                    'allowed_types' => 'gif|jpg|png|jpeg|doc|pdf',
                    'max_size' => '1024000000',
                    'multi' => 'all'
                ); 
            $this->load->library('upload', $config);   
            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors()); 
            }
            else
            {
                $data = $this->upload->data();  
            }
            $new_array = array_column ($data, 'full_path');         
}

我可以获得$ new_array的输出,

I can get output of $new_array,

但是我想在另一个函数中使用此输出,而该函数恰好在同一类中,

But i want to use this output in another function which is just below that in same class,

示例,

public function getback()
{
print_r($new_array);
}

我该怎么做?

谢谢,

推荐答案

在该函数中简单地将该值传递为

Simply pass that value within that function as

public function add_attachments($openid) {
    $config = array(
        'upload_path' => './uploads/attachments/',
        'allowed_types' => 'gif|jpg|png|jpeg|doc|pdf',
        'max_size' => '1024000000',
        'multi' => 'all'
    );
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
    } else {
        $data = $this->upload->data();
    }
    $new_array = array_column($data, 'full_path');
    $this->getback($new_array);
}

public function getback($new_array = array()) {
    print_r($new_array);
}

已编辑

使用js传递数据

public function add_attachments($openid) {
   //your code...
   echo json_encode($new_array);exit;
}

现在在ajax成功函数之内

Now within your ajax success function

 success:function(data){
     var data = $.parseJSON(data)
     //send it to your another function
 }

这篇关于获取另一个函数php中一个函数的数组输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆