20 helpfull php array manipulation functions

0.34K views
no comments
21 Apr 2015 12:31 am

1.Remove duplicate values from array – ARRAY_UNIQUE

$array = array(0,1,2,3,3,4,5,5,7,8,7,7);
$array = array_unique($array);

Result

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [5] => 4
    [6] => 5
    [8] => 7
    [9] => 8
)

2.Re-order with random values from array-SHUFFLE

$array = array(0,1,2,3,3,4,5,5,7,8,7,7);
shuffle($array);

Result

Array
(
    [0] => 2
    [1] => 8
    [2] => 3
    [3] => 7
    [4] => 7
    [5] => 5
    [6] => 1
    [7] => 0
    [8] => 4
    [9] => 7
    [10] => 3
    [11] => 5
)

3.Remove null values from array-ARRAY_FILTER

$array = array(0,1,2,null,3,4,5,null,7,8,7,7);
$array = array_filter($array);

Result

Array
(
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 3
    [6] => 5
    [7] => 5
    [8] => 7
    [9] => 8
    [10] => 7
    [11] => 7
)

4.Ascending order array values-SORT

$numbers  = array(
	1 => 'one'
	,2 => 'two'
	,3 => 'three'
	,4 => 'four'
	,5 => 'five'
	,6 => 'six'
	,7 => 'seven'
	,8 => 'eight'
	,9 => 'nine'
	,10 => 'ten'
	);

sort($numbers);
print_r($numbers);

Result

Array
(
    [0] => eight
    [1] => five
    [2] => four
    [3] => nine
    [4] => one
    [5] => seven
    [6] => six
    [7] => ten
    [8] => three
    [9] => two
)

5.Descending order array values-ARSORT

$numbers  = array(
	1 => 'one'
	,2 => 'two'
	,3 => 'three'
	,4 => 'four'
	,5 => 'five'
	,6 => 'six'
	,7 => 'seven'
	,8 => 'eight'
	,9 => 'nine'
	,10 => 'ten'
	);

arsort($numbers);
print_r($numbers);

Result

Array
(
    [2] => two
    [3] => three
    [10] => ten
    [6] => six
    [7] => seven
    [1] => one
    [9] => nine
    [4] => four
    [5] => five
    [8] => eight
)

6.Ascending order array keys

$numbers  = array(
	10 => 'ten'
	,9 => 'nine'
	,8 => 'eight'
	,7 => 'seven'
	,6 => 'six'
	,5 => 'five'
	,4 => 'four'
	,3 => 'three'
	,2 => 'two'
	,1 => 'one'
	);

ksort($numbers);
print_r($numbers);

Result

Array
(
    [1] => one
    [2] => two
    [3] => three
    [4] => four
    [5] => five
    [6] => six
    [7] => seven
    [8] => eight
    [9] => nine
    [10] => ten
)

7.Descending order array keys

$numbers  = array(
	1 => 'one'
	,2 => 'two'
	,3 => 'three'
	,4 => 'four'
	,5 => 'five'
	,6 => 'six'
	,7 => 'seven'
	,8 => 'eight'
	,9 => 'nine'
	,10 => 'ten'
	);

arsort($numbers);
print_r($numbers);

Result

Array
(
    [10] => ten
    [9] => nine
    [8] => eight
    [7] => seven
    [6] => six
    [5] => five
    [4] => four
    [3] => three
    [2] => two
    [1] => one
)

8.Replace text string from array with STR_REPLACE

$old_array = array(
 'site_name' => 'SDTutorials',
 'site_domain' => 'SDTutorials.com'
);
$new_array = str_replace('SDTutorials','SDTuts',$old_array);

Result

Array
(
    [site_name] => SDTuts
    [site_domain] => SDTuts.com
)

8.Change columns keys with custom function

$array_data[] = array('col1'=>'SDTuts.com','col2'=>'ABC','col3'=>'DEF','col4'=>'GHI','col5'=>'JKL','col6'=>'MNO');
$array_data[] = array('col1'=>'SDTuts.com','col2'=>'ABC','col3'=>'DEF','col4'=>'GHI','col5'=>'JKL','col6'=>'MNO');
$keysarray = array('col1'=>'my_table_col1','col2'=>'my_table_col2','col3'=>'my_table_col3','col4'=>'my_table_col4','col5'=>'my_table_col5','col6'=>'my_table_col6');


function changecols_keys($arraydata,$keysarray){
	$returnarray_data = array();
	$auto_index = -1;
	foreach($arraydata as $colsarrayindex=>$colsarraydata){
		$auto_index++;
		foreach($colsarraydata as $colkey=>$coldata){
			$returnarray_data[$auto_index][$keysarray[$colkey]] = $coldata;
		};
	}
	return $returnarray_data;
}

$modified_keys = changecols_keys($array_data,$keysarray);
print_array($modified_keys);

Result

Array
(
    [0] => Array
        (
            [my_table_col1] => SDTuts.com
            [my_table_col2] => ABC
            [my_table_col3] => DEF
            [my_table_col4] => GHI
            [my_table_col5] => JKL
            [my_table_col6] => MNO
        )

    [1] => Array
        (
            [my_table_col1] => SDTuts.com
            [my_table_col2] => ABC
            [my_table_col3] => DEF
            [my_table_col4] => GHI
            [my_table_col5] => JKL
            [my_table_col6] => MNO
        )

)

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>