20 helpfull php array manipulation functions

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

1.Remove duplicate values from array – ARRAY_UNIQUE

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

Result

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

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

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

Result

[php]
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
)
[/php]

3.Remove null values from array-ARRAY_FILTER

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

Result

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

4.Ascending order array values-SORT

[php]
$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);
[/php]

Result

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

5.Descending order array values-ARSORT

[php]
$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);
[/php]

Result

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

6.Ascending order array keys

[php]
$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);
[/php]

Result

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

7.Descending order array keys

[php]
$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);
[/php]

Result

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

8.Replace text string from array with STR_REPLACE

[php]
$old_array = array(
‘site_name’ => ‘SDTutorials’,
‘site_domain’ => ‘SDTutorials.com’
);
$new_array = str_replace(‘SDTutorials’,’SDTuts’,$old_array);
[/php]

Result

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

8.Change columns keys with custom function

[php]
$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’);
[/php]

[php]
$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);
[/php]

Result

[php]
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
)

)
[/php]

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>