Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7671529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:04:50+00:00 2026-05-31T16:04:50+00:00

So I have a file called cache, which stores traffic analysis of a site,

  • 0

So I have a file called cache, which stores traffic analysis of a site, using multidimensional arrays.

cache.php

$traffic_array=array("date_ip_uniqueNU"=>array("pageviews"=>34,"time_enteredOnsite"=>"12:00"),"date_ip_uniqueNU"=>array("pageviews"=>34,"time_enteredOnsite"=>"12:00"));//ect ect

Now, I need to somehow append to the array above.

I could simply just read the whole file, go through and rebuild the array in a foreach loop, and then just re-write the whole file, something like this:

include('cache.php');

foreach($traffic_array as $mainKey){

$rebuild_contents.="array("something"=>array("pageviews"=>".$mainKey['pageviews'].","time_enteredOnsite"=>".$mainKey['time_enteredOnsite'].");"//so I'm just building a string containing all the code to re-write the file.

} 

//then write to the file

$file="cache.php";

$content_to_put"\<? \$traffic_array=\"$rebuild_content\";"

file_put_contents($file,$content_to_put);

//NOTE: I just quickly wrote this up now, so expect syntax errors.

So do you see what I’ve done above – Simply rebuild the array contents into a string, and then write that string into the cache file.

But, I’m sure there is a much better approach to this, so can someone help me out?

Thank you! xD

EDIT: Also a problem with the method above, If this process was to happen multiple times, at the EXACT same millisecond – something would mess up, right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:04:52+00:00Added an answer on May 31, 2026 at 4:04 pm

    It’s very, hm, strange method of caching.
    Let suppose that you wish to change some php code in the file.

    file_put_contents have ability to append the data to end of file, call this method with flag FILE_APPEND (details).
    You may change method of defining array to like this:

    $traffic_array[new key 1] = array(new data 1);
    $traffic_array[new key 2] = array(new data 2);
    ...
    

    And then you just add content to end of file like this

    file_put_contents($file, '$traffic_array[your new key] = array(data for adding);', FILE_APPEND);
    

    But if you may not change method of defining the array traffic_array then file_put_contents not for you. Use fopen() with flag ‘r+’, use fseek() for move a pointer to to correct place for put new data at the file, fwrite() for write only new data, and fclose().

    // Content of file: <?php $a = array('k' => array(), 'k2' => array());\n
    $f = fopen('file', 'r+');
    fseek($f, -3, SEEK_END); // place the pointer after last value of array, before ");\n"; cont from end
    fwrite($f, ", 'k3' => array('f'=>'a'));\n"); // put new data, also add overwrited data ");\n"
    fclose($f);
    //now content of file: <?php $a = array('k' => array(), 'k2' => array(), 'k3' => array('f'=>'a'));\n
    

    Changing of input file can cause huge problems.

    Also a problem with the method above, If this process was to happen multiple times, at the EXACT same millisecond – something would mess up, right?

    Right, read about file locking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using a custom template file called user-profile.tpl.php for a while. But
I have a file called webpart.cs which is called by one of my masterpages
I have a file called 34.php?session=2534464a39a2c97cb7113689726a4e52 in my /var/www/html folder but I can't view
I have a class called language.php in my environment which handles the language detection
I have a file called main.py and a file called classes.py main.py contains the
I have a file called hellowf.cs class MyFirstApp { static void Main() { System.Windows.Forms.MessageBox.Show(Hello,
I have a file called header.html and it is included by base.html . In
I have a file called error.log on my server that I need to frequently
I have a file called config.yml in my /config folder of my rails application.
I have a file called foobar (without .py extension). In the same directory I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.