-2

Need help for the code.

Here is my list of array which i want to remove the two (2) Wi-fi.

Array
(
    [20-10] => Array
        (
            [facilityCode] => 20
            [facilityGroupCode] => 10
            [order] => 1
            [number] => 1968
            [voucher] => 
            [description] => Year of construction
        )

    [550-70] => Array
        (
            [facilityCode] => 550
            [facilityGroupCode] => 70
            [order] => 1
            [indFee] => 
            [indYesOrNo] => 1
            [voucher] => 
            [description] => Wi-fi
        )

    [20-60] => Array
        (
            [facilityCode] => 20
            [facilityGroupCode] => 60
            [order] => 1
            [indLogic] => 1
            [voucher] => 
            [description] => Shower
        )


    [261-60] => Array
        (
            [facilityCode] => 261
            [facilityGroupCode] => 60
            [order] => 1
            [indFee] => 
            [indYesOrNo] => 1
            [voucher] => 
            [description] => Wi-fi
        )

)

I do also tried the array_unique();

here is the result:

Array
(
    [0] => Year of construction
    [1] => Shower
    [2] => Wi-fi

)

But i want to keep the facilityCode,facilityGroupCode,order,number etc. Thanks for any help.

Rahul
  • 18,271
  • 7
  • 41
  • 60
Miks Alibo
  • 61
  • 8

3 Answers3

1
//populate data
$mainArr = array();
$first = array(
    "facilityCode" => 20,
    "facilityGroupCode" => 10,
    "order" => 1,
    "number" => 1968,
    "voucher" => "",
    "description" => "Year of construction",
);
$second = array(
    "facilityCode" => 550,
    "facilityGroupCode" => 70,
    "order" => 1,
    "indFee" => "",
    "indYesOrNo" => 1,
    "voucher" => "",
    "description" => "Wi-fi"
);

$mainArr["20-10"] = $first;
$mainArr["550-70"] = $second;
$mainArr["261-60"] = $second;

//get duplicates
$counter = 0;
$duplicates = array();

foreach ($mainArr as $key=>$val) {
    $counter++;
    if (in_array($key, $duplicates)) continue;
    $i = 0;

    foreach ($mainArr as $key1=>$val1) {
        if ($i < $counter) {
            $i++;
            continue;
        }

        if ($val["description"] == $val1["description"]) {
            array_push($duplicates, $key1);
        }
    }
}

//remove duplicates
foreach($duplicates as $key) {
    unset($mainArr[$key]);
}
Sin Sopheak
  • 364
  • 2
  • 13
1
$itemRows = array(); // Your main array
$descriptionValues = array();

foreach ($itemRows as $itemKey => $itemRow) {
    foreach ($itemRow as $key => $value) {
        if ($key == 'description') {
            if (in_array($value, $descriptionValues)) {
                unset($itemRows[$itemKey]);

                continue 2;
            }

            $descriptionValues[] = $value;
        }
    }
}
m908070
  • 70
  • 3
1

One liner is all can do your requirement,

$result = array_reverse(array_values(array_column(array_reverse($arr), null, 'description')));

Source link for your requirement.

Rahul
  • 18,271
  • 7
  • 41
  • 60