gfazioli
11/2/2017 - 5:27 PM

Array Assoc default

<?php

$default = [
  'title',
  'editor',
  'author',
  'thumbnail',
  'excerpt',
  'trackbacks',
  'custom-fields',
  'comments',
  'revisions',
  'post-formats'
];


$custom = [
  'aggiunto',
  'revisions' => false
];

function arrayAssocDefault( $default, $merge )
{
  
  $add = [];
  $del = [];
  
  foreach( $merge as $key => $value ) {
    if( is_numeric( $key ) && ! is_bool( $value ) ) {
      $add[] = $value;
    }
    elseif( ! is_numeric( $key ) && is_bool( $value ) && $value === false ) {
      $del[] = $key;
    }
  }
  
  $result = array_unique( array_merge( array_diff( $default, $del ), $add ) );
  
  return $result;  
}


$array = arrayAssocDefault( $default, $custom );

var_dump( $array );

/*

array(10) {
  [0] =>
  string(5) "title"
  [1] =>
  string(6) "editor"
  [2] =>
  string(6) "author"
  [3] =>
  string(9) "thumbnail"
  [4] =>
  string(7) "excerpt"
  [5] =>
  string(10) "trackbacks"
  [6] =>
  string(13) "custom-fields"
  [7] =>
  string(8) "comments"
  [8] =>
  string(12) "post-formats"
  [9] =>
  string(8) "aggiunto"
}

*/