PHP Array_merge function issue

3.27K views
no comments
6 Oct 2015 2:40 am

I got issue lost data  from new $variable of array_merged because array_merge function cannot merged if any passed parameter type not array, for example I merged two array for email template, the first array from $_POST and second array from Database but $user_data will be null If user’s not found from database. array_merge function thrown a warning if passed parameter type not object or array and it cannot thrown warning when our project or application set on production or testing mood.

Here is code example of merged two but failed to merged

//USER DATA FROM POST BY USER
$post_data = array();
$post_data['param1'] = $_POST['param1'];
$post_data['username'] = $_POST['username'];
$post_data['userpass'] = $_POST['userpass'];

//USER DATA FROM DATABASE
$userdata = DB::getresult("SELECT useremail,userid FROM USER_TBL WHERE userlogin='$username' AND userpass='$userpass' ")->get();
//array_merge function failed to merge both arrays if anyone null or not object or array, and you can validate user db data or return error if record not found, but this is just example of code.
$newdata = array_merge($post_data,$userdata);
//RESULT of $newdata = NULL

Here is function for solution of array_merge

function merge_arrays_obj(){
	$func_info = debug_backtrace();
	$func_args = $func_info[0]['args'];
	$return_array_data = array();
	foreach($func_args as $args_index=>$args_data){
		if(is_array($args_data) || is_object($args_data)){
		$return_array_data = array_merge($return_array_data,$args_data);
		}
	}
	return $return_array_data;
}

Definition and detail of merge_arrays_obj function

debug_backtrace(); function called to get the passed arguments in function
$func_info[0][‘args’] code used to get arguments from debug_backtrace() function; to user pass many arguments of array object data to merge them into a single array and I have passed three arguments.
$return_array_data = array(); used to assign null array;
if(is_array($args_data) || is_object($args_data)) used to check if argument is array or object
$return_array_data = array_merge($return_array_data,$args_data); code use to appending array object data from arguments into a single array.
return $return_array_data; code used to return all merged data

$array = array('abc'=>'abcparam');
$null_array = null;
$new_arraydata = array('username'=>'Rameez Soomro','usersite'=>'https://sdtuts.com');
$new_arrayOBJ = array ( (object) array('obj_DATA' => 'ABC_OBJ','obj_next_index '=>'ABC_OBJ_NEXT_INDEX') );
$new_array_data = merge_arrays_obj($array,$null_array,$new_arraydata,$new_arrayOBJ);
print_r($new_array_data);

result of above merged array and object

Array
(
    [abc] => abcparam
    [username] => Rameez Soomro
    [usersite] => https://sdtuts.com
    [0] => stdClass Object
        (
            [obj_DATA] => ABC_OBJ
            [obj_next_index ] => ABC_OBJ_NEXT_INDEX
        )

)

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>