Jump to content

Unserialize multiple files while adding the content in an array


darkminos

Recommended Posts

Hi,

 

What I am looking at now is to unserialize multiple files while adding them into an array:

 

      $directory = "cache/";
      $filecount = count(glob($directory . "*"));
  
      
      $allFiles = scandir($directory);
      $files = array_diff($allFiles, array('.', '..'));
      $i=0;

	foreach($files as $file) {
	   $un = unserialize(file_get_contents("cache/".$file.""));
	   $i++;

                .......????????????????????

	echo "<br>".count($all_results)."<br>";
	}

 

Any ideas? because I run out of them...

 

file output format:

 

Array (
[0] => Array ( 
[pic] => 1.jpg 
[link] => http://
[from] => shop 
[title] => title 
[cost] => 0.01 
[description1] => asd
[description2] => fgh
[description3] => jkl
[category] => LED )

Link to comment
Share on other sites

Perhaps you are very right sir  :) How embarrassing...

 

For anyone interested in the full solution:

 

      $directory = "cache/";
      $filecount = count(glob($directory . "*"));
      //echo $filecount;
      
      $allFiles = scandir($directory);
      $files = array_diff($allFiles, array('.', '..'));


	foreach($files as $file) {
	   $un[] = unserialize(file_get_contents("cache/".$file.""));		   
	}


	foreach ($un as $num => $entry) {
	    if ($num == sizeof ($un)-1) continue;
	    if (isset ($un[$num]) && isset ($un[$num+1])) {
	        if (isset ($res)) $res = array_merge ($res, $un[$num], $un[$num+1]);
	        else $res = array_merge ($un[$num], $un[$num+1]);
	    }
	}

	if ( !function_exists( "arrayUnique" ) ){ 
	    function arrayUnique ( $rArray ){ 
	        $rReturn = array (); 
	        while ( list( $key, $val ) = each ( $rArray ) ){ 
	            if ( !in_array( $val, $rReturn ) ) 
	            array_push( $rReturn, $val ); 
	        } 
	        return $rReturn; 
	    } 
	} 

	$myArray = arrayUnique ($res);

 

This will get all the serialized data from all files in a folder, merge them in one array, and remove duplicates

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.