Преобразование CSV в массив объектов в PHP

#php #ajax #wordpress

#php #ajax #wordpress

Вопрос:

Привет, я создаю функцию, которая извлекает мой загруженный CSV-файл через ajax, затем я хотел бы сформировать объект json, который можно опубликовать в woocommerce API.

Первый цикл в цикле while создает мой массив меток, и каждый цикл через него использует метки в качестве ключа.

         if ($file !== FALSE) {
            while (($data = fgetcsv($file, 0, ",")) !== FALSE):
//                print_r($data);
                if($row == 1) {
                    $labels = $data;
                } else {
                    $new_data = array_combine($labels, $data);
                    array_push($csv_data, $new_data);
                }
                $row  ;
            endwhile;
            foreach($csv_data as $data){
                echo "New Customer:  ";
                print_r($data);

            }
            fclose($file);
        }
 

Когда я запускаю этот код, я получаю такой результат:

 New Customer:  Array
(
    [Name] => Test Dude
    [Username] => Test
    [Email] => test@johndeer.com
    [Orders] => 1
    [proceed_id] => 765
    [AOV] => 765
    [Country / Region] => US
    [City] => Testerton
    [Region] => AR
    [Postal Code] => 71601
    [Address] => John Drive
)
New Customer:  Array
(
    [Name] => John Deer
    [Username] => johndeer
    [Email] => johndeer@jonnydeers.com
    [Orders] => 0
    [proceed_id] => 0
    [AOV] => 0
    [Country / Region] => CA
    [City] => Testerton
    [Region] => AB
    [Postal Code] => T6M 2V5
    [Address] => John Street
)
New Customer:  Array
(
    [Name] => John Doe
    [Username] => john.doe
    [Email] => john.doe@example.com
    [Orders] => 0
    [proceed_id] => 0
    [AOV] => 0
    [Country / Region] => US
    [City] => San Francisco
    [Region] => CA
    [Postal Code] => 94103
    [Address] => John Drive
)
New Customer:  Array
(
    [Name] => John Doe
    [Username] => johndoe
    [Email] => johndoe@example.com
    [Orders] => 0
    [proceed_id] => 0
    [AOV] => 0
    [Country / Region] => US
    [City] => San Francisco
    [Region] => CA
    [Postal Code] => 94103
    [Address] => 
)
New Customer:  Array
(
    [Name] => John Doe
    [Username] => johndoe1
    [Email] => johndoe123@example.com
    [Orders] => 0
    [proceed_id] => 0
    [AOV] => 0
    [Country / Region] => US
    [City] => San Francisco
    [Region] => CA
    [Postal Code] => 94103
    [Address] => 
)
New Customer:  Array
(
    [Name] => John Doe
    [Username] => johndoe7
    [Email] => johndoe1234@example.com
    [Orders] => 0
    [proceed_id] => 0
    [AOV] => 0
    [Country / Region] => US
    [City] => San Francisco
    [Region] => CA
    [Postal Code] => 94103
    [Address] => 
)
New Customer:  Array
(
    [Name] => John Doe
    [Username] => johndoe5
    [Email] => johndoe5@example.com
    [Orders] => 0
    [proceed_id] => 0
    [AOV] => 0
    [Country / Region] => US
    [City] => San Francisco
    [Region] => CA
    [Postal Code] => 94103
    [Address] => 
)
New Customer:  Array
(
    [Name] => John Doe
    [Username] => johndoe6
    [Email] => johndoe6@example.com
    [Orders] => 0
    [proceed_id] => 0
    [AOV] => 0
    [Country / Region] => US
    [City] => San Francisco
    [Region] => CA
    [Postal Code] => 94103
    [Address] => 
)
New Customer:  Array
(
    [Name] => John Doe
    [Username] => johndoe8
    [Email] => johndoe8@example.com
    [Orders] => 0
    [proceed_id] => 0
    [AOV] => 0
    [Country / Region] => US
    [City] => San Francisco
    [Region] => CA
    [Postal Code] => 94103
    [Address] => 
)
0
 

Он отображает нового клиента: в каждом цикле просто для того, чтобы показать, что это один цикл цикла. Однако, когда я пытаюсь выбрать «Name» или «proceed_id», он возвращает null. Я пытался использовать оба этих синтаксиса $data['Name] и $data->Name . Как мне выбрать по ключу внутри этого foreach?

             foreach($csv_data as $data){
                //$data is the individual rows
                $proceed_id = $data['proceed_id']; 
            }
 

РЕДАКТИРОВАТЬ 1:

Вот vardump внешнего цикла $csv_data.

 array(9) {
  [0]=>
  array(11) {
    ["Name"]=>
    string(9) "Test Dude"
    ["Username"]=>
    string(4) "Test"
    ["Email"]=>
    string(17) "test@johndeer.com"
    ["Orders"]=>
    string(1) "1"
    ["proceed_id"]=>
    string(3) "765"
    ["AOV"]=>
    string(3) "765"
    ["Country / Region"]=>
    string(2) "US"
    ["City"]=>
    string(9) "Testerton"
    ["Region"]=>
    string(2) "AR"
    ["Postal Code"]=>
    string(5) "71601"
    ["Address"]=>
    string(10) "John Drive"
  }
  [1]=>
  array(11) {
    ["Name"]=>
    string(9) "John Deer"
    ["Username"]=>
    string(8) "johndeer"
    ["Email"]=>
    string(23) "johndeer@jonnydeers.com"
    ["Orders"]=>
    string(1) "0"
    ["proceed_id"]=>
    string(1) "0"
    ["AOV"]=>
    string(1) "0"
    ["Country / Region"]=>
    string(2) "CA"
    ["City"]=>
    string(9) "Testerton"
    ["Region"]=>
    string(2) "AB"
    ["Postal Code"]=>
    string(7) "T6M 2V5"
    ["Address"]=>
    string(11) "John Street"
  }
  [2]=>
  array(11) {
    ["Name"]=>
    string(8) "John Doe"
    ["Username"]=>
    string(8) "john.doe"
    ["Email"]=>
    string(20) "john.doe@example.com"
    ["Orders"]=>
    string(1) "0"
    ["proceed_id"]=>
    string(1) "0"
    ["AOV"]=>
    string(1) "0"
    ["Country / Region"]=>
    string(2) "US"
    ["City"]=>
    string(13) "San Francisco"
    ["Region"]=>
    string(2) "CA"
    ["Postal Code"]=>
    string(5) "94103"
    ["Address"]=>
    string(10) "John Drive"
  }
  [3]=>
  array(11) {
    ["Name"]=>
    string(8) "John Doe"
    ["Username"]=>
    string(7) "johndoe"
    ["Email"]=>
    string(19) "johndoe@example.com"
    ["Orders"]=>
    string(1) "0"
    ["proceed_id"]=>
    string(1) "0"
    ["AOV"]=>
    string(1) "0"
    ["Country / Region"]=>
    string(2) "US"
    ["City"]=>
    string(13) "San Francisco"
    ["Region"]=>
    string(2) "CA"
    ["Postal Code"]=>
    string(5) "94103"
    ["Address"]=>
    string(0) ""
  }
  [4]=>
  array(11) {
    ["Name"]=>
    string(8) "John Doe"
    ["Username"]=>
    string(8) "johndoe1"
    ["Email"]=>
    string(22) "johndoe123@example.com"
    ["Orders"]=>
    string(1) "0"
    ["proceed_id"]=>
    string(1) "0"
    ["AOV"]=>
    string(1) "0"
    ["Country / Region"]=>
    string(2) "US"
    ["City"]=>
    string(13) "San Francisco"
    ["Region"]=>
    string(2) "CA"
    ["Postal Code"]=>
    string(5) "94103"
    ["Address"]=>
    string(0) ""
  }
  [5]=>
  array(11) {
    ["Name"]=>
    string(8) "John Doe"
    ["Username"]=>
    string(8) "johndoe7"
    ["Email"]=>
    string(23) "johndoe1234@example.com"
    ["Orders"]=>
    string(1) "0"
    ["proceed_id"]=>
    string(1) "0"
    ["AOV"]=>
    string(1) "0"
    ["Country / Region"]=>
    string(2) "US"
    ["City"]=>
    string(13) "San Francisco"
    ["Region"]=>
    string(2) "CA"
    ["Postal Code"]=>
    string(5) "94103"
    ["Address"]=>
    string(0) ""
  }
  [6]=>
  array(11) {
    ["Name"]=>
    string(8) "John Doe"
    ["Username"]=>
    string(8) "johndoe5"
    ["Email"]=>
    string(20) "johndoe5@example.com"
    ["Orders"]=>
    string(1) "0"
    ["proceed_id"]=>
    string(1) "0"
    ["AOV"]=>
    string(1) "0"
    ["Country / Region"]=>
    string(2) "US"
    ["City"]=>
    string(13) "San Francisco"
    ["Region"]=>
    string(2) "CA"
    ["Postal Code"]=>
    string(5) "94103"
    ["Address"]=>
    string(0) ""
  }
  [7]=>
  array(11) {
    ["Name"]=>
    string(8) "John Doe"
    ["Username"]=>
    string(8) "johndoe6"
    ["Email"]=>
    string(20) "johndoe6@example.com"
    ["Orders"]=>
    string(1) "0"
    ["proceed_id"]=>
    string(1) "0"
    ["AOV"]=>
    string(1) "0"
    ["Country / Region"]=>
    string(2) "US"
    ["City"]=>
    string(13) "San Francisco"
    ["Region"]=>
    string(2) "CA"
    ["Postal Code"]=>
    string(5) "94103"
    ["Address"]=>
    string(0) ""
  }
  [8]=>
  array(11) {
    ["Name"]=>
    string(8) "John Doe"
    ["Username"]=>
    string(8) "johndoe8"
    ["Email"]=>
    string(20) "johndoe8@example.com"
    ["Orders"]=>
    string(1) "0"
    ["proceed_id"]=>
    string(1) "0"
    ["AOV"]=>
    string(1) "0"
    ["Country / Region"]=>
    string(2) "US"
    ["City"]=>
    string(13) "San Francisco"
    ["Region"]=>
    string(2) "CA"
    ["Postal Code"]=>
    string(5) "94103"
    ["Address"]=>
    string(0) ""
  }
}
0
 

Var_export() =

 array (
  0 => 
  array (
    'Name' => 'Test Dude',
    'Username' => 'Test',
    'Email' => 'test@johndeer.com',
    'Orders' => '1',
    'proceed_id' => '765',
    'AOV' => '765',
    'Country / Region' => 'US',
    'City' => 'Testerton',
    'Region' => 'AR',
    'Postal Code' => '71601',
    'Address' => 'John Drive',
  ),
  1 => 
  array (
    'Name' => 'John Deer',
    'Username' => 'johndeer',
    'Email' => 'johndeer@jonnydeers.com',
    'Orders' => '0',
    'proceed_id' => '0',
    'AOV' => '0',
    'Country / Region' => 'CA',
    'City' => 'Testerton',
    'Region' => 'AB',
    'Postal Code' => 'T6M 2V5',
    'Address' => 'John Street',
  ),
  2 => 
  array (
    'Name' => 'John Doe',
    'Username' => 'john.doe',
    'Email' => 'john.doe@example.com',
    'Orders' => '0',
    'proceed_id' => '0',
    'AOV' => '0',
    'Country / Region' => 'US',
    'City' => 'San Francisco',
    'Region' => 'CA',
    'Postal Code' => '94103',
    'Address' => 'John Drive',
  ),
  3 => 
  array (
    'Name' => 'John Doe',
    'Username' => 'johndoe',
    'Email' => 'johndoe@example.com',
    'Orders' => '0',
    'proceed_id' => '0',
    'AOV' => '0',
    'Country / Region' => 'US',
    'City' => 'San Francisco',
    'Region' => 'CA',
    'Postal Code' => '94103',
    'Address' => '',
  ),
  4 => 
  array (
    'Name' => 'John Doe',
    'Username' => 'johndoe1',
    'Email' => 'johndoe123@example.com',
    'Orders' => '0',
    'proceed_id' => '0',
    'AOV' => '0',
    'Country / Region' => 'US',
    'City' => 'San Francisco',
    'Region' => 'CA',
    'Postal Code' => '94103',
    'Address' => '',
  ),
  5 => 
  array (
    'Name' => 'John Doe',
    'Username' => 'johndoe7',
    'Email' => 'johndoe1234@example.com',
    'Orders' => '0',
    'proceed_id' => '0',
    'AOV' => '0',
    'Country / Region' => 'US',
    'City' => 'San Francisco',
    'Region' => 'CA',
    'Postal Code' => '94103',
    'Address' => '',
  ),
  6 => 
  array (
    'Name' => 'John Doe',
    'Username' => 'johndoe5',
    'Email' => 'johndoe5@example.com',
    'Orders' => '0',
    'proceed_id' => '0',
    'AOV' => '0',
    'Country / Region' => 'US',
    'City' => 'San Francisco',
    'Region' => 'CA',
    'Postal Code' => '94103',
    'Address' => '',
  ),
  7 => 
  array (
    'Name' => 'John Doe',
    'Username' => 'johndoe6',
    'Email' => 'johndoe6@example.com',
    'Orders' => '0',
    'proceed_id' => '0',
    'AOV' => '0',
    'Country / Region' => 'US',
    'City' => 'San Francisco',
    'Region' => 'CA',
    'Postal Code' => '94103',
    'Address' => '',
  ),
  8 => 
  array (
    'Name' => 'John Doe',
    'Username' => 'johndoe8',
    'Email' => 'johndoe8@example.com',
    'Orders' => '0',
    'proceed_id' => '0',
    'AOV' => '0',
    'Country / Region' => 'US',
    'City' => 'San Francisco',
    'Region' => 'CA',
    'Postal Code' => '94103',
    'Address' => '',
  ),
)
 

Комментарии:

1. Не могли бы вы, пожалуйста, упорядочить jsfiddle или фрагмент? даже с 4-5 строками CSV-данных в коде, а не в отдельном файле.

2. Это должно сработать. Что говорит var_dump($csv_data); (вне цикла)?

3. @OfirBaruch Я не могу создать пример JSfiddle, поскольку он находится на стороне PHP.

4. Если вы можете добавить пример для данных в файле CSV, этого будет достаточно, чтобы мы могли его воспроизвести.

5. Не волнуйтесь, мы все учимся на собственном опыте 🙂