Welcome Guest, Not a member yet? Register   Sign In
isset: without or with?
#6

[eluser]jonez[/eluser]
You can also define defaults, then merge the post data with those defaults to ensure every key exists with at least a default value.

Code:
$defaults = array(
'q' => null,
);

$data = $this->input->post( );
if ( !is_array( $data ) ) $data = array( );
$data = array_merge( $defaults, $data );

if ( $data[ 'q' ] !== null ) {
//clean
}

Or with some helpers in MY_array_helper.php:
Code:
function trim_array( $arr ) {
foreach ( $arr as $k => $v ) {
  if ( is_scalar( $v ) ) {
   $arr[ $k ] = trim( $v );
  }
  else if ( is_array( $v ) ) {
   $arr[ $k ] = trim_array( $v );
  }
  else {
   $arr[ $k ] = $v;
  }
}

return $arr;
}

function merge_array( ) {
$args = func_get_args( );
$result = array( );

foreach ( $args as $arr ) {
  $arr = ( is_array( $arr ) ) ? trim_array( $arr ) : array( );

  $result = array_merge( $result, $arr );
}

return $result;
}

Code:
$defaults = array(
'q' => null,
);

$data = merge_array( $defaults, $this->input->post( ) );

if ( $data[ 'q' ] !== null ) {
//clean
}


Messages In This Thread
isset: without or with? - by El Forum - 07-06-2014, 09:01 AM
isset: without or with? - by El Forum - 07-06-2014, 09:30 AM
isset: without or with? - by El Forum - 07-06-2014, 09:46 AM
isset: without or with? - by El Forum - 07-06-2014, 04:05 PM
isset: without or with? - by El Forum - 07-06-2014, 11:37 PM
isset: without or with? - by El Forum - 07-07-2014, 05:01 AM
isset: without or with? - by El Forum - 07-07-2014, 05:09 AM
isset: without or with? - by El Forum - 07-07-2014, 05:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB