There is an array of this type:

0 => string '"Datasheets","Image","Digi-Key Part Number","Manufacturer Part Number","Manufacturer","Description","Quantity Available","Factory Stock","Unit Price (USD)","@ qty","Minimum Quantity","Series","Accessory Type","Material","Color","For Use With/Related Products"' (length=262) 1 => string '"http://www.knowles.com/eng/content/download/3165/37817/version/2/file/bf-1861-000.pdf","http://media.digikey.com/Photos/Knowles%20Acoustics%20Photos/BF-1861-000.jpg","423-1158-ND","BF-1861-000","Knowles","ACOUSTIC DAMPER 1500 OHMS",1067,0,"1.67000",0,1,"BF","Damper","Metal Ferrule Housing","Green","Hearing Aids"' (length=314) 

It is required to break lines into arrays with a comma as a separator.
I tried to break using explode , but nothing happened.
The string index [0] is broken correctly, and the second with index [1] is broken incorrectly, because In the line in unnecessary places there is a separator.
Tell me how to ignore the extra delimiters so that when the array is broken down, the result is:

 array (size=16) 0 => string '"Datasheets"' (length=15) 1 => string '"Image"' (length=7) 2 => string '"Digi-Key Part Number"' (length=22) 3 => string '"Manufacturer Part Number"' (length=26) 4 => string '"Manufacturer"' (length=14) 5 => string '"Description"' (length=13) 6 => string '"Quantity Available"' (length=20) 7 => string '"Factory Stock"' (length=15) 8 => string '"Unit Price (USD)"' (length=18) 9 => string '"@ qty"' (length=7) 10 => string '"Minimum Quantity"' (length=18) 11 => string '"Series"' (length=8) 12 => string '"Accessory Type"' (length=16) 13 => string '"Material"' (length=10) 14 => string '"Color"' (length=7) 15 => string '"For Use With/Related Products"' (length=31) 
  • It's funny, even here this invisible character is, and is preserved after all the hashcode filters. See that between single and double quotes before "Datasheets" in the original question; ) - Sergiks
  • $ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, " digikey.com/product-search / ... ); curl_setopt ($ curl, CURLOPT_HEADER, 0); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); $ out = curl_crt, 1); = explode ("\ n", $ out); $ string [] = array ($ item_dan); var_dump ($ string [0]); $ aKeys = json_decode ('['. $ string [0]. ']' ); $ aVals = json_decode ('['. $ string [1]. ']'); var_dump ($ aKeys); @sergiks Here's the source one according to your example! - ggLike
  • Leave json_decode() aside and look carefully at my answer below the phrase “Upd. III” - Sergiks
  • @sergiks, thanks for helping with the example! Everything turned out, everything works. Function implemented! The second week, I trampled on one place, did not expect someone to help, thank you! - ggLike pm
  • @ggLike, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Sergiks 1:49 pm

2 answers 2

The format of strings is close to JSON, I would try this (it works ):

 $aKeys = json_decode( '[' . $strings[0] . ']'); $aVals = json_decode( '[' . $strings[1] . ']'); if( is_null( $aKeys) || is_null( $aVals) || ( count( $aKeys) != count( $aVals))) { // что могло пойти не так?! return; } return array_combine( $aKeys, $aVals); 

The second option - str_getcsv() - parse the string, assuming its valid CSV format. Works with your sample data.

Upd. III Indeed, once you give CSV, it is better to use the f str_getcsv() . Here is the working code. Filtering is needed to clear out unprintable characters at the beginning.

 function filt( $s) { return filter_var( $s, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_NO_ENCODE_QUOTES ); } $url = "http://www.digikey.com/product-search/download.csv?FV=fff4000b%2Cfff80054&mnonly=0&newproducts=0&ColumnSort=0&page=1&stock=0&pbfree=0&rohs=0&quantity=0&ptm=0&fid=0&pageSize=500"; $curl = curl_init(); curl_setopt_array( $curl, array( CURLOPT_URL => $url , CURLOPT_HEADER => 0 , CURLOPT_RETURNTRANSFER => 1 ) ); $out = curl_exec($curl); // очистить лишние символы $item_dan = array_map( "filt", explode("\n", $out)); // из каждой строки сделать массив по запятой снаружи кавычек $items = array_map( "str_getcsv", $item_dan); print_r( $items); 
  • And how will the line be separated here? I can't understand the logic to the end! - ggLike
  • It is already divided:) ["string",12,34,"string2"] - this is how arrays are written in json; "string",12,34,"string2" is quite a decent entry in CSV (the format is, "comma-separated values ​​are values ​​separated by a comma. Therefore, it is enough either to assign square brackets so that it becomes json, which is parsed by the php json_decode() function json_decode() , or writing nothing at all to parse the line as if it were a CSV f- str_getcsv() . Did you check that it works with the rest of the data? With the examples in the example, everything works. - Sergiks
  • Yes, double quotes around the strings will disappear, so in none of the options I have suggested, you will get exactly the same as in the quotes-string question> string '"@ qty"' (length = 7) There will be string '@ qty' (length=5) . But do you really need this in the end? > How will the string be separated here? Commas, between which can be either numbers without quotes, or strings in quotes (and commas inside these strings are not considered separators). - Sergiks
  • Displays a blank value is not clear why! - ggLike
  • It is clear why: where return NULL means, the condition is fulfilled. Updated the answer to the working version. It is necessary to filter the data - there the invisible symbol comes at the beginning, it has to be filtered out. - Sergiks

I assume that you need one received array as keys, the second as values.

 $keys_string = 'Тут строка с индексом [0] из исходного массива'; $values_string = 'Тут строка с индексом [1] из исходного массива'; $keys = explode(',', $keys_string); // Это массив, который вы хотели получить $values = explode(',', $values_string); $data = array_combine($keys, $values); print_r($data); 

Result:

 Array ( ["Datasheets"] => "http://www.knowles.com/eng/content/download/3165/37817/version/2/file/bf-1861-000.pdf" ["Image"] => "http://media.digikey.com/Photos/Knowles%20Acoustics%20Photos/BF-1861-000.jpg" ["Digi-Key Part Number"] => "423-1158-ND" ["Manufacturer Part Number"] => "BF-1861-000" ["Manufacturer"] => "Knowles" ["Description"] => "ACOUSTIC DAMPER 1500 OHMS" ["Quantity Available"] => 1067 ["Factory Stock"] => 0 ["Unit Price (USD)"] => "1.67000" ["@ qty"] => 0 ["Minimum Quantity"] => 1 ["Series"] => "BF" ["Accessory Type"] => "Damper" ["Material"] => "Metal Ferrule Housing" ["Color"] => "Green" ["For Use With/Related Products"] => "Hearing Aids" ) 

View working example

I do not understand why your string did not break correctly.

  • Will crash incorrectly if a comma suddenly appears inside the quotes. - Sergiks
  • Delimit the set of symbols "," - Get
  • @Get bad advice: a string can contain numeric values ​​without quotes ( "Get",21,"неправ" ) - Sergiks