function array_intersect_assoc_recursive($arr1, $arr2) { if (!is_array($arr1) || !is_array($arr2)) { // return $arr1 == $arr2; // Original line return $arr2; } $commonkeys = array_intersect(array_keys($arr1), array_keys($arr2)); $ret = array(); foreach ($commonkeys as $key) { $ret[$key] = array_intersect_assoc_recursive($arr1[$key], $arr2[$key]); } return $ret; }
function array_intersect_assoc_recursive(&$arr1, &$arr2) { if (!is_array($arr1) || !is_array($arr2)) { return $arr1 == $arr2; // or === for strict type } $commonkeys = array_intersect(array_keys($arr1), array_keys($arr2)); $ret = array(); foreach ($commonkeys as $key) { $ret[$key] =& array_intersect_assoc_recursive($arr1[$key], $arr2[$key]); } return $ret; }
/** * Sync page index * * @access public * @return array message */ function sync_page() { if (!file_exists(PAGE_FOLDER . '/' . $this->navfile)) { write_file(PAGE_FOLDER . '/' . $this->navfile, json_encode(array(), JSON_PRETTY_PRINT)); } $output = array('status' => 'success', 'message' => 'Everything already synced.'); if (!is_writable(PAGE_FOLDER)) { $output = array('status' => 'error', 'message' => "Page folder is not writable. Make it writable first.\n"); } // get current directory map $map = $this->scan_pages(); // get the old page index $from_file = json_decode(file_get_contents(PAGE_FOLDER . '/' . $this->navfile), true); // add new item to index $merge_diff = array_merge_recursive($from_file, $map); // remove unused item from index $new_index = array_intersect_assoc_recursive($merge_diff, $map); // make sure it is writablle if (!write_file(PAGE_FOLDER . '/' . $this->navfile, json_encode($new_index, JSON_PRETTY_PRINT), "w")) { $output = array('status' => 'error', 'message' => "Page index file " . $this->navfile . " is not writable. Make it writable first.\n"); } else { $output = array('status' => 'success', 'message' => "Page index synced.\n"); } return $output; }