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 )

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.