darkminos Posted July 20, 2012 Share Posted July 20, 2012 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/266011-unserialize-multiple-files-while-adding-the-content-in-an-array/ Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 $un[] = unserialize(file_get_contents("cache/".$file."")); perhaps? Quote Link to comment https://forums.phpfreaks.com/topic/266011-unserialize-multiple-files-while-adding-the-content-in-an-array/#findComment-1363125 Share on other sites More sharing options...
darkminos Posted July 20, 2012 Author Share Posted July 20, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/266011-unserialize-multiple-files-while-adding-the-content-in-an-array/#findComment-1363160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.