duanqu9292 2016-06-08 11:55
浏览 30
已采纳

将txt文件列表项转换为用于在php中创建数组的实际代码

to start i am terrible with loops and arrays.

I have a txt file that i can turn into an array using file() but i need to pull the data from the text file into my code to create the code for making an array, not put the data into an actual array.

i need to turn:

cat
dog
mouse

into the following code dynamically (i do not need to make an actual outputted array, i need to create the code dynamically somehow).:

Array('cat'=>'cat','dog'=>'dog','mouse'=>'mouse')

My problem is that i need to do the following:

txt file input:

Crafts & Sewing\Sale\Sale\Sale on Crafts & Sewing
Crafts & Sewing\Sale\Sale\Crafts & Sewing Sale

Each item is on a seperate line of the txt file (over 10k lines)

Output (not browser output, still in code):

$somevariable = Array('line1' => 'line1', 'line2' => 'line2')

But everything i try gives me the actual array.

I need to create the actual CODE for an array by taking each line of the text file and pulling it into my code and adding in the commas, and quotes, and => without actually going through and manually adding that all to 10k lines of code.

basically i have a HUGE list in a text file and instead of going through and manually adding and editing over 10k lines of the text file to be in that format and then cluttering up my code with that, i want to put it all into the correct format programatically and then just use that variable in my code rather than 10k lines of code. This also allows me to edit the text file with new content whenever it changes without needing to edit the code.

I am using it to create a dropdown in a site that take the format as follows:

array(
    'id'  => 'quality',
    'type' => 'select',
    'label' => __('Quality', 'esc_vff'),
    'options' => array(
        'New'  => 'New',
        'Used'  => 'Used',
        'Refurbished' => 'Refurbished'              
    ),
    'description' => __('Overstock data for "Quality" column.', 'esc_vff'),
    'desc_tip'    => true,
)

in the end i would like to be able to do something like change:

'options' => array(
    'New' => 'New',
    'Used' => 'Used',
    'Refurbished' => 'Refurbished'              
),

to:

'options' => $myarrayfromtextfile,

where $myarrayfromtextfile is a variable or something that gives me the code that should be put there, but isnt 10k lines of code.

maybe to better clarify: an array printed out has the brackets... i need to find a way to print out the code structure of the array (i dunno if thats the best way to word this) because i am taking a bunch of lines of text and trying to format it to look like i went through and wrote the comma seperated code version of the array

array('foo' => 'foo' , 'bar' => 'bar' )

so i need a variable that when dropped into another array gets compiled/converted to the physical code structure, not the final browser rendered array with length counts and brackets...

Or ill take any other ideas on how to make 10k lines of text end up looking like

 array('foo' => 'foo' , 'bar' => 'bar' )

without going line by line adding in commas and => and ''

  • 写回答

2条回答 默认 最新

  • drci47425 2016-06-08 12:01
    关注

    Use php file()

    File — Reads entire file into an array

    <?php 
    $lines = file('test.txt'); ///path to your file
    foreach ($lines as $line_num => $line) 
    {
        echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />
    ";
    }
    ?>
    

    As per your requirement

    <?php 
    $lines = file('test.txt'); ///path to you file
    $arrayVar = "array(";
    foreach ($lines as $line_num => $line) 
    {
        $arrayVar.= "'".preg_replace( "/|
    /", "", $line)."' => '".preg_replace( "/|
    /", "", $line)."',";
    }
    $arrayVar = rtrim($arrayVar,',').');';
    echo $arrayVar;
    ?>
    

    I have this in my test.txt file

    abc
    cde
    efg
    hij
    klm
    

    The code above will give you array like this in your browser

    array('abc' => 'abc','cde' => 'cde','efg' => 'efg','hij' => 'hij','klm' => 'klm');
    

    Is this you want? @HostandMore

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流