PHP - 从另一个函数内部调用的函数返回数组结果

时间:2018-03-28 13:15:19

标签: php mysql arrays

嗨再一次,我的班级里面有几个功能。其中一个功能是从数据库进行查询并将数据存储在数组中。我调用了这个函数,他在另一个函数中创建了mysql查询,将结果数组传递给自己并进行操作。我需要从第一个函数中捕获结果数组,并将其放入调用第一个函数的函数内的另一个数组中。希望能够正确解释。在我的第一个函数中,我启动了return $arraytoclass,但第二个函数内的print_r不会打印任何内容。非常感谢我将在下面发布我的代码。 ____编辑---我根据以下朋友的建议修改了代码。

<?php

    class AdvancedExport extends Module
        {

        public function target_query(){

            include_once "connection.php";
            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            }

            $sql = "SELECT codice_target FROM customer";
            $result = $conn->query($sql);
            $arraytoclass = array();

            if ($result->num_rows > 0) {
            // output data of each row
            //echo "tutto ok";
                while($row = $result->fetch_row()) {
                //echo "Codice target: " . $row["codice_target"]."<br>";
                $arraytoclass[] = $row;
                //echo "codice target:".$arraytoclass[$i]['codice_target']; 

                }
                //print_r($arraytoclass);
                return $arraytoclass;
                //print_r($arraytoclass);

            } else {
                echo "NO results";
            }


            $conn->close();
        }

        public function fputToFile($file, $allexportfields, $object, $ae)
                {

                    if($allexportfields && $file && $object && $ae)
                    {
                        //one ready for export product
                        $readyForExport = array();


                        //put in correct sort order
                        foreach ($allexportfields as $value)
                        {
                            $object = $this->processDecimalSettings($object, $ae, $value);
                            $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);

                        }

            $arraytoclass = $this->target_query();
            print_r ($arraytoclass);

                        $readyForExport['codice_target'] = $arraytoclass[];


                        //scambio l'id customer con il codice_target

                        //$readyForExport['codice_target'] = 8989;

                        $textTarget = (string)$readyForExport['codice_target']; 

                        $readyForExport['id_customer'] = $textTarget;

                        // LOTS OF CODE HERE ...

                        //write into csv line by line
                        fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
                    }
                }
        }


?>
print_r ($arraytoclass);之后

$arraytoclass = $this->target_query(); 给了我这个结果:


    Array
    (
        [0] => Array
            (
                [0] => 0
            )

        [1] => Array
            (
                [0] => 898989
            )

        [2] => Array
            (
                [0] => 0
            )

        [3] => Array
            (
                [0] => 0
            )

        [4] => Array
            (
                [0] => 0
            )

        [5] => Array
            (
                [0] => 0
            )

        [6] => Array
            (
                [0] => 0
            )

        [7] => Array
            (
                [0] => 0
            )

        [8] => Array
            (
                [0] => 0
            )

        [9] => Array
            (
                [0] => 0
            )

        [10] => Array
            (
                [0] => 0
            )

        [11] => Array
            (
                [0] => 0
            )

        [12] => Array
            (
                [0] => 0
            )

        [13] => Array
            (
                [0] => 0
            )

        [14] => Array
            (
                [0] => 0
            )

        [15] => Array
            (
                [0] => 0
            )

        [16] => Array
            (
                [0] => 0
            )

        [17] => Array
            (
                [0] => 0
            )

        [18] => Array
            (
                [0] => 0
            )

        [19] => Array
            (
                [0] => 0
            )

        [20] => Array
            (
                [0] => 0
            )
    )

0 个答案:

没有答案