K
K
krispey1022021-06-25 09:48:28
PHP
krispey102, 2021-06-25 09:48:28

Php how to remove certain values ​​in an array?

There is an array $arResult["arClinics"], for example:

Array
(
    [0] => Array
        (
            [ID] => 10882
            [~ID] => 10882
            [PROPERTY_SPECIALIZATIONS_VALUE] => Array
                (
                )

            [~PROPERTY_SPECIALIZATIONS_VALUE] => Array
                (
                )

            [PROPERTY_SPECIALIZATIONS_DESCRIPTION] => Array
                (
                )

            [~PROPERTY_SPECIALIZATIONS_DESCRIPTION] => Array
                (
                )

            [PROPERTY_SPECIALIZATIONS_PROPERTY_VALUE_ID] => Array
                (
                )

            [~PROPERTY_SPECIALIZATIONS_PROPERTY_VALUE_ID] => Array
                (
                )

        )

    [1] => Array
        (
            [ID] => 10883
            [~ID] => 10883
            [PROPERTY_SPECIALIZATIONS_VALUE] => Array
                (
                )

            [~PROPERTY_SPECIALIZATIONS_VALUE] => Array
                (
                )

            [PROPERTY_SPECIALIZATIONS_DESCRIPTION] => Array
                (
                )

            [~PROPERTY_SPECIALIZATIONS_DESCRIPTION] => Array
                (
                )

            [PROPERTY_SPECIALIZATIONS_PROPERTY_VALUE_ID] => Array
                (
                )

            [~PROPERTY_SPECIALIZATIONS_PROPERTY_VALUE_ID] => Array
                (
                )

        )

    [2] => Array
        (
            [ID] => 10884
            [~ID] => 10884
            [PROPERTY_SPECIALIZATIONS_VALUE] => Array
                (
                    [0] => 11281
                    [1] => 11254
                    [2] => 10885
                    [3] => 10895
                    [4] => 10887
                    [5] => 10886
                    [6] => 10888
                    [7] => 21736
                )

            [~PROPERTY_SPECIALIZATIONS_VALUE] => Array
                (
                    [0] => 11281
                    [1] => 11254
                    [2] => 10885
                    [3] => 10895
                    [4] => 10887
                    [5] => 10886
                    [6] => 10888
                    [7] => 21736
                )

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

it is necessary to remove the variables [PROPERTY_SPECIALIZATIONS_DESCRIPTION], [~PROPERTY_SPECIALIZATIONS_VALUE], [~PROPERTY_SPECIALIZATIONS_DESCRIPTION], [PROPERTY_SPECIALIZATIONS_DESCRIPTION], [~PROPERTY_SPECIALIZATIONS_VALUE] from this array in the keys, how to do this?
Roughly speaking, for the new array to be from the variables ID and [PROPERTY_SPECIALIZATIONS_VALUE]

I tried to do this, it does not work
foreach($arResult["arClinics"] as $key => $item){
    $arResult["arClinics"]["ITEMS"] = $item["ID"];
    $arResult["arClinics"]["ITEMS"] = $item["PROPERTY_SPECIALIZATIONS_VALUE"];
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Slava Rozhnev, 2021-06-25
@krispey102

Below is a possible solution:

foreach($arClinics as $id => $Clinic) {
  $arClinics[$id] = array_filter(
    $Clinic,
    function($key) {
      return $key == 'ID' || $key == 'PROPERTY_SPECIALIZATIONS_VALUE';
    },
    ARRAY_FILTER_USE_KEY
  );
}

Execute PHP online

T
ThunderCat, 2021-06-25
@ThunderCat

1) unset
2) as $key => $item hints that you need to use the current key for verification, and for anset.
3) You can simply transfer these properties to a new array if the structure of the old one does not suit you.

M
Mors Clamor, 2021-06-25
@66demon666

So what's the problem? Do you know cycles?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question