Dai Wang
Dai Wang

Reputation: 57

why list() doesn't work in my case

Trying to create a php class for importing CSV data. Sorry for the messy looking codes. I created a function _santitize_product_list() to filter through the CSV file separating out the product data that pass the import requirement and product data that missing something. The function works fine if :

$this->_sanitized_data = $this->_sanitize_product_list($this->_csv_data);

So that I get an array that contains both group. If I want to separate them out, I try to use list():

list($this->_sanitized_data, $this->_error_list) = $this->_sanitize_product_list($this->_csv_data);

this time the code returns Undefined offset: 1 error. My question is why list() doesn't work in this case?

class data_import_update {
    protected $_dbc;
    protected $_update;
    protected $_total_line;
    protected $_csv_data;
    protected $_sanitized_data;
    protected $_error_list;
    protected $_update_list;
    protected $_import_list;

    // constructor
    // plug in DB connection
    function __construct($csv_data) {

        $this->_csv_data = $csv_data;

        // step1. fiter the input data

        //list($this->_sanitized_data, $this->_error_list) = $this->_sanitize_product_list($this->_csv_data);
        $this->_sanitized_data = $this->_sanitize_product_list($this->_csv_data);
    } // End of Constructor

    // filter out items that no meet requirement
    private function _sanitize_product_list ($list_input) {
        $err_item = array();
        foreach ($list_input as $key => $item) {
            //require fields: product_sku, product_name, product_unit, product_packaging, category, file_url, slash in the category
            //make sure each item required fields aren't left empty
            // || empty($item['file_url']) empty($item['product_name']) || empty($item['product_packaging']) ||
            if (empty($item['product_sku']) || (empty($item['category']) || !strpos($item['category'], '/'))) {

                // log error item and unset it from import list
                $err_item[] = $list_input[$key]['product_sku'];
                unset($list_input[$key]);
            }
        } // End of foreach loop

        if (!empty($err_item[0])) {
            return array('import_list'=>$list_input, 'error_item'=>$err_item);
        }else {
            return array('import_list'=>$list_input);
        }
    } 

Upvotes: 1

Views: 47

Answers (2)

Barmar
Barmar

Reputation: 781096

list() only works with numeric arrays, not associative arrays, because it assigns the first variable from $array[0], the second variable from $array[1], and so on. You also need to return 2 elements all the time. So change the if at the end to just:

return array($list_input, $err_item);

Upvotes: 2

NeoThermic
NeoThermic

Reputation: 298

Your if check on $err_item has an else condition that only returns one element in the array. I can only assume your code is hitting that return condition, so your list() call won't have a second parameter to use.

Upvotes: 4

Related Questions