PHP Array Generator

I have some values ​​in an excel file and I want all of them to be an element of the array and there is other data in it.

I know that one way is to copy them one at a time and put them in an array to initialize the statment

An example list that is part of the entire list

Bay Area 
Greater Boston
Greater Chicago
Greater Dallas-Ft. Worth
Greater D.C.
Las Vegas
Greater Houston
Greater LA
Greater New York
Greater San Diego
Seattle
South Florida

It is easy to initialize an array with values ​​when there are not as many elements as

$array= array('Bay Area '=>'Bay Area ','Greater Boston'=>'Greater Boston',....) 
// and so on

But I have 70-80 elements, it is a very tedious task to initialize the array, as shown above.

So Guys Is there an alternative or short header for assigning an array with a list of values?

Is there any automatic array generator tool?

+3
source share
5 answers

, php,

$myArray = file('myTextFile');

//sets the keys to the array equal to the values
$myArray = array_combine($myArray, $myArray); 

excel CSV, php, :

$myArray = array();
if (($file = fopen("myTextFile", "r")) !== FALSE) {
    while (($data = fgetcsv($file)) !== FALSE) {
        foreach($data as $value) {
            $myArray[$value] = $value;
        }
    }
    fclose($handle);
}
+6
$array = explode("\n", file_get_contents('yourfile.txt'));

CSV PHP fgetcsv() PHPExcelReader XLS.

( )

( , ohmusama file() + array_combine() )

+3

:

$string_var = "
Bay Area 
Greater Boston
Greater Chicago
Greater Dallas-Ft. Worth
";

$array_var = explode("\n", $string_var);
+1

get notepad ++, excel . - "(. *)\n" "'(\ 1)'," ("quoutes not included), :

" ", " ", " "

php.

+1

I think it looks better:

$a[] = "Bay Area";
$a[] = "Greater Boston";
$a[] = "Greater Chicago";

Use Excel to create such a text file (I don't have Excel, but it looks somewhat):

=concatenate(" $a[] = ",chr(34),A1,chr(34),";")

Then export only this column.

0
source

All Articles