Date Added: 14/01/2015


<?php

function arrayUnique($array, $preserveKeys = false)
{
    // Unique Array for return
    $arrayRewrite = array();
    // Array with the md5 hashes
    $arrayHashes = array();
    foreach($array as $key => $item) {
        // Serialize the current element and create a md5 hash
        $hash = md5(serialize($item));
        // If the md5 didn't come up yet, add the element to
        // to arrayRewrite, otherwise drop it
        if (!isset($arrayHashes[$hash])) {
            // Save the current element hash
            $arrayHashes[$hash] = $hash;
            // Add element to the unique Array
            if ($preserveKeys) {
                $arrayRewrite[$key] = $item;
            } else {
                $arrayRewrite[] = $item;
            }
        }
    }
    return $arrayRewrite;
}

$input = array(
    array(
        'id'    => 123,
        'ean'   => '1234567890123'
    ),
    array(
        'id'    => 123,
        'ean'   => '4852950174938'
    ),
    array(
        'id'    => 123,
        'ean'   => '1234567890123'
    ),
);

$input = arrayUnique($input);

print_r($input);

?>

2 dimentional array sorting by key
</pre>
function sortMultiArrayByKey($argArray, $argKey, $argOrder=SORT_ASC){
 foreach ($argArray as $key => $row){
 $key_arr[$key] = $row[$argKey];
 }
 array_multisort($key_arr, $argOrder, $argArray);
 return $argArray;
 }
<pre>

output:
<strong></strong>
Array
(
    [0] => Array
        (
            [id] => 123
            [ean] => 1234567890123
        )

    [1] => Array
        (
            [id] => 123
            [ean] => 4852950174938
        )

)
<strong></strong>

Last Update: Posted by: müslüm ÇEN
Not Commented Yet !
Please login in order to comment . Login