简体   繁体   English

如何在不删除PHP中文件的现有数组值的情况下在文本文件中编写另一组新数组?

[英]How can I write another new set of array in a text file without removing the existing array values of the file in PHP?

My output goes like this 我的输出是这样的

  B005CI62XY    Nike Men's Performance Moisture Wicking No Show Socks 6 Pair - Shoe Size: 8-12 - White
B008FZY5NU  Nike Men's Shox Turbo+ 13 Running Shoe
B008F9E7ZM  Nike Dual Fusion Women's Running Shoes 525752-001 Black/Fireberry/Silver
B006VXT4M0  Nike Free Run+ 2
B004PAR0N6  Nike Men's NIKE T-LITE VIII LEATHER 4E RUNNING SHOES
B005QA4X3O  Nike Mens Dual Fusion St 2 Neutral Runner Gray Sneaker
B004UTNLFS  Nike The Overplay VI Men's Basketball Shoe
B007T8HL9O  Nike Lady Free Run+ V3 Running Shoes
B004L7OH8O  Nike Men's Air Monarch IV Running
B007RJQQ7I  Nike Air Max+ 2012 Mens Running Shoes 487982-100
B004L0DAWA  Nike Women's Musique IV Dance Shoes
B004K4GNF8  Nike Men's Air Monarch IV Running
B004LBS6W8  NIKE Air Monarch IV (4E) MENS RUNNING SHOES
B0071AV6P0  Nike Air Max+ 2012 Mens Running Shoes 487982-001
B008MQ5CLQ  Nike Men's NIKE AIR VISI PRO III NBK BASKETBALL SHOES
B004TR0S8O  Nike Dri-Fit Elite Basketball Socks
B0070062M8  Nike Women's NIKE LUNARSWIFT+ 3 WMNS RUNNING SHOES
B007OX34FE  Nike Free Run+ 3 Mens Running Shoes 510642-002
B0070066R4  Nike Women's Revolution Running
B0021K13IU  Nike Men's NIKE AIR FORCE 1 '07 BASKETBALL SHOES
B004UTSMY8  Nike DART 9 Men's Running Shoe
B004ANFDU0  Nike Men's T-LITE VIII Leather Training Shoes
B003YY6RCE  Nike Men's T-Lite VIII Leather Running
B005CD4MUY  Nike Men's Performance Moisture Wicking No Show Socks 6 Pair - Shoe Size: 8-12 - Black
B007P4ZR0W  Nike Free Run+ 3 Mens Running Shoes 510642-706
B004YKIVVC  Nike Women's Dart 9 Running
B008FQCFV8  Nike Men's Dart 9 Running
B004RL0J9U  Nike Golf Men's Tech Essentials Web Belt
B0077QNU0W  Nike Women's NIKE FREE TR FIT 2 WMNS RUNNING SHOES
B003NZ0Y9G  Nike Dri-Fit Half-Cushion Crew Socks - 3 pack
B007IXAFFC  Nike Air Max Navigate LE Mens Running Shoes 456977-010
B0074SC7ZC  Nike Air Max+ 2012 Mens Running Shoes 487982-010
B003JK1UIE  Nike Men's NIKE CORTEZ BASIC NYLON '06 TRAINING SHOES
B0057FRVQY  Nike DART 9 LEATHER 443862-001 Black
B004PSS6WW  Nike Men's Crew Cut Moisture Management Socks 3 pack
B004RL0J9K  Nike Golf Mens Tech Essentials 3 Pack Belt Gift Set
B006YSPB9C  Nike Kids NIKE FREE RUN 2.0 (GS) RUNNING SHOES
B004LBWNYA  NIKE WMNS NIKE INITIATOR (WOMENS)
B007ZQG5FQ  Nike Lady Air Relentless 2 Running Shoes
B004IWW8OW  Nike 375833 Pro Bra
B007P4XKK6  Nike Free Run+ 3 Mens Running Shoes 510642-401
B006EPQ4SW  Nike One Size Fits All Web Belts : Black, Gray & Navy 3 Pack
B004LBSWI6  Nike Women's T-Lite VIII Leather Fitness
B0056IU79A  NIKE GTS '09 Canvas Sneakers Shoes White Mens
B00722V5DU  Nike Women's Free TR Twist SL
B003JK8N7A  Nike Men's NIKE CORTEZ BASIC LEATHER '06 CASUAL SHOES
B007V67N1K  Nike The Overplay VII Men's Basketball Shoe
B007ETEMWM  Nike Shox Calistra Womens Dance Pumps / Shoes - Black
B0060NPWIG  Nike Performance Athletic Crew Socks - 3 Pack White Size Large Men's shoe size 8-12 Made In USA
B0085ZD4WI  Nike Air Alvord 10
B005CD6JJG  Nike Men's Performance Moisture Wicking Crew Socks 6 Pack - Shoe Size: 8-12 Black
B007SOI26U  Nike Men's Dart 9 Running
B005844FQI  Nike Women' s Lunarglide+ 3
B007URIUTE  Nike Men's Air Relentless II Running
B007ZHLUUU  Nike Men's Air Relentless II Running
B004JVD1EI  NIKE 6.0 Mavrk Mens Shoes
B004KMC4FI  Nike Benassi Swoosh Men's Slide
B007P4VAI0  Nike Free Run+ 3 Mens Running Shoes 510642-600
B006ZBH7BS  Nike Men's Lunarfly+ 3 Running Training Shoes-Black/Gray/Blue/Green
B006T3FPMK  Nike Sweet Classic Leather
B0088CMY6U  Nike Men's NIKE MAVRK SKATE SHOES
B007W6O0AQ  Nike Air Max+ 2012 Womens Running Shoes White/Anthracite-Hot Punch-Pure Platinum 487679-103
B004LBIWA4  Nike T-Lite VIII Running Shoes White Womens
B00510KIKQ  Nike Men's Quarter Cut Moisture Management Socks 3 pack
B00785MBCU  Nike Air Max+ 2012 Mens Running Shoes 487982-104
B0071BQEII  Nike Women's NIKE FREE RUN+ 2 WMNS RUNNING SHOES
B004YKIZDG  Nike Dart 9 Men's Running Shoe
B004G83DHO  Nike Men's NIKE NIKE5 GATO LTR SOCCER INDOOR SHOES
B0072KINAU  Nike Lady Air Max+ 2012 Running Shoes
B0087G0XRE  Nike Women's Flex Trainer 2012 Running
B00767BA6S  Nike SCRAMBLER TR2 313388-072
B007Y8QL0E  Nike Air Max+ 2012 Mens Running Shoes 487982-008
B007C79DD4  Nike Free Run 2.0 GS Grey Blue Pink Flash 2012 Youth Running Shoes 477701-007
B009PQ6P2M  Nike Men's NIKE DART 9 RUNNING SHOES
B007ZTM97G  Nike Women's Zoom Vomero+ 7 Running
B003MBW6LK  Nike Men's NIKE SWEET CLASSIC LEATHER CASUAL SHOES
B008HORLC6  Nike Free Run+ 3 Men's Running Shoes
B007VCOJLG  Nike Free 4.0 V2 Dark Obsidian Blue Mens Running Shoes 511472-404
B004TN5O50  Nike Women's Musique IV Fitness
B008G1HON6  Nike Men's Shox Turbo+ 13 Running
B006U1URCE  Nike LunarEclipse+ 2
B003DKEW6M  Nike Golf Web Belt 3-Pack
B0035QSI2I  NIKE Cortez Basic Nylon (GS) Big Kid's Retro Sneaker
B005191WYI  Nike Kids' Dart 9 Running
B004LBT1NQ  Nike Air Monarch IV WIDE (4E) Mens 416355-001 Sneaker
B0065C9A0I  Nike Air Max 90 Mens Running Shoes
B0052SU5H8  Nike Brasilia 5 Medium Duffel Grip
B005HFAC6A  Nike Free Run+ 2 Women's Running Shoes Black White-Anthracite
B0073XDPTA  Nike Air Max+ 2012 Mens Running Shoes 487982-016
B00351Z7AY  Nike 269604 Pro-Core Men's Compression Shorts 6"
B009B1QA8K  Nike Free Run+ 3 Mens Running Shoes 510642-440
B007VFRFMI  Nike Free Run 3 (GS) Big Kids Running Shoes 512098-600
B0081V17PM  Nike Lunarfly 3 + Women's Running Shoes 510791 010 Grey
B0091HCGBY  Nike Women's Revolution Running
B0049P0YIU  Nike Pro Core Long Sleeve Compression Top
B008FR8FES  Nike Free Run+3 Womens Running Shoes 510643-401
B00767D71E  Nike Free Run+ 2 Mens Running Shoes 443815-012
B004LK2RHE  Nike Shox Air Lunar NZ Mens Running Shoes
B0082CTMIE  Nike Women's Flex Trainer 2
B003G7FWHU  Nike Men's NIKE CORTEZ BASIC LEATHER '06 CASUAL SHOES

Here is the code for the output: 这是输出的代码:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Tester</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
</head>
<body>
<?php

$search = 'Nike';
$temp_search = $search;
$category = 'Shoes';
$flag = 0;
$page = 11;
$temp = '';

for($i=1;$i<=$page;$i++){
?>
<script type="text/javascript">
    $(function(){
        $.ajax({
            url: 'search.php',
            type: 'post',
            datatype: 'json',
            data: { search: '<?php echo $search; ?>', category: '<?php echo $category; ?>', page: '<?php echo $i; ?>' },
            success:function(data){
                $('#data').append(data);
            }
        });
    });
</script>
<?php
    if($i == 10){
        $i = 1;
        $flag++;
        $search = $temp_search.' '.$flag;
    }
    if($flag == 1){
        break;
    }
}
?>
<table id="data">

</table>

</body>
</html>

Here is the code for processing and returning the values to the index page 以下是处理值并将值返回到索引页面的代码

//search.php
    <?php

include 'Amazon-ECS-PHP-Library-master/samples/sampleSettings.php';
require 'Amazon-ECS-PHP-Library-master/lib/AmazonECS.class.php';

$amazonEcs = new AmazonECS(AWS_API_KEY, AWS_API_SECRET_KEY, 'com', AWS_ASSOCIATE_TAG);
$amazonEcs->requestDelay(true);
$amazonEcs->associateTag(AWS_ASSOCIATE_TAG);

$response = $amazonEcs->responseGroup('Large')->category('Shoes')->page($_POST['page'])->search($_POST['search']);
$fh = fopen('log', 'w');
$array_item = unserialize(file_get_contents('log'));
foreach($response->Items->Item as $item){
    $array_item[] = array('ASIN'=>$item->ASIN, 'Title'=>$item->ItemAttributes->Title);
    echo '<tr><td>'.$item->ASIN.'</td><td>'.$item->ItemAttributes->Title.'</td></tr>';
}
fwrite($fh, serialize($array_item));
fclose($fh);
?>

And here is the code for viewing the values of the text file this is just for checking purposes 以下是用于查看文本文件值的代码,这仅用于检查目的

 <?php
var_dump(unserialize(file_get_contents('log')));
?>

And the output of it is just like this 它的输出就是这样的

 array (size=10)
  0 => 
    array (size=2)
      'ASIN' => string 'B005CI62XY' (length=10)
      'Title' => string 'Nike Men's Performance Moisture Wicking No Show Socks 6 Pair - Shoe Size: 8-12 - White' (length=86)
  1 => 
    array (size=2)
      'ASIN' => string 'B008FZY5NU' (length=10)
      'Title' => string 'Nike Men's Shox Turbo+ 13 Running Shoe' (length=38)
  2 => 
    array (size=2)
      'ASIN' => string 'B008F9E7ZM' (length=10)
      'Title' => string 'Nike Dual Fusion Women's Running Shoes 525752-001 Black/Fireberry/Silver' (length=72)
  3 => 
    array (size=2)
      'ASIN' => string 'B006VXT4M0' (length=10)
      'Title' => string 'Nike Free Run+ 2' (length=16)
  4 => 
    array (size=2)
      'ASIN' => string 'B004PAR0N6' (length=10)
      'Title' => string 'Nike Men's NIKE T-LITE VIII LEATHER 4E RUNNING SHOES' (length=52)
  5 => 
    array (size=2)
      'ASIN' => string 'B005QA4X3O' (length=10)
      'Title' => string 'Nike Mens Dual Fusion St 2 Neutral Runner Gray Sneaker' (length=54)
  6 => 
    array (size=2)
      'ASIN' => string 'B004UTNLFS' (length=10)
      'Title' => string 'Nike The Overplay VI Men's Basketball Shoe' (length=42)
  7 => 
    array (size=2)
      'ASIN' => string 'B007T8HL9O' (length=10)
      'Title' => string 'Nike Lady Free Run+ V3 Running Shoes' (length=36)
  8 => 
    array (size=2)
      'ASIN' => string 'B004L7OH8O' (length=10)
      'Title' => string 'Nike Men's Air Monarch IV Running' (length=33)
  9 => 
    array (size=2)
      'ASIN' => string 'B007RJQQ7I' (length=10)
      'Title' => string 'Nike Air Max+ 2012 Mens Running Shoes 487982-100' (length=48)

As what I notice the array stored is only the last 10 items from the index page, What I to happen is that all items from the index page will be save in the text file, I dont how can I fix this, Anyone any advice or suggestion to help me with my problem will be a big help thanks! 正如我注意到存储的数组只是索引页面中的最后10个项目,我要做的是索引页面中的所有项目都将保存在文本文件中,我不知道如何解决这个问题,任何建议或者建议帮助我解决我的问题将是一个很大的帮助谢谢!

Without caring about file locking issues you need to move a few statements around: 如果不关心文件锁定问题,您需要移动一些语句:

// removed the fopen() because file_get_contents() already opens the file for you
$array_item = unserialize(file_get_contents('log'));

// manipulate the array here
foreach($response->Items->Item as $item){
    $array_item[] = array('ASIN'=>$item->ASIN, 'Title'=>$item->ItemAttributes->Title);
    echo '<tr><td>'.$item->ASIN.'</td><td>'.$item->ItemAttributes->Title.'</td></tr>';
}

file_put_contents('log', serialize($array_item));

That's all you need. 这就是你所需要的。

Update 更新

Your code performs 11 AJAX requests (for 11 result pages) each calling Amazon to populate the results of each page; 您的代码执行11个AJAX请求(对于11个结果页面),每个请求都调用Amazon来填充每个页面的结果; this is highly inefficient and you should perform the search only once, find out how many pages there are and then use the Amazon API itself to get all the results in one go. 这是非常低效的,您应该只执行一次搜索,找出有多少页面,然后使用Amazon API本身一次性获得所有结果。

Doing so will eliminate the need to keep a temporary file in the first place as your search.php can return ALL results to JavaScript in just one call. 这样做将消除首先保留临时文件的需要,因为search.php可以在一次调用search.php所有结果返回给JavaScript。

我认为你需要在'追加'模式下打开文件。

$fh = fopen('log', 'a');

I think your answer might be in the mode you set for "fopen" 我想你的回答可能是你为“fopen”设置的模式

http://us3.php.net/manual/en/function.fopen.php http://us3.php.net/manual/en/function.fopen.php

You can see that the second parameter for this function is the mode. 您可以看到此功能的第二个参数是模式。 You have it as "w", which means, straight from the docs: 你把它作为“w”,这意味着,直接来自文档:

Open for writing only; 仅供写作; place the file pointer at the beginning of the file and truncate the file to zero length. 将文件指针放在文件的开头,并将文件截断为零长度。 If the file does not exist, attempt to create it. 如果该文件不存在,请尝试创建它。

This will overwrite your file every time. 这将每次覆盖您的文件。 You'll most likely want to open the file in "a" (that is, append) mode. 您很可能希望以“a”(即追加)模式打开文件。

Open for writing only; 仅供写作; place the file pointer at the end of the file. 将文件指针放在文件的末尾。 If the file does not exist, attempt to create it. 如果该文件不存在,请尝试创建它。

$fh = fopen('log', 'w'); Change it to $fh = fopen('log', 'a'); 将其更改为$fh = fopen('log', 'a');

w - will remove existing content a- will keep the content and append the new content w - 将删除现有内容a-将保留内容并附加新内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM