skip to Main Content

I have 2 arrays like this:

Array(    [0] => 24    [1] => 24    [2] => NOW    [3] => false    [4] => Boş    [5] => USD    [6] => 42.35    [7] => false    [8] => A2U0MT0XPL9B69    [9] => Boş    [10] => USD    [11] => 0.0    [12] => Boş    [13] => US    [14] => NJ    [15] => new    [16] => true    [17] => Boş    [18] => 1062    [19] => 86.0)

Array(    [0] => minimumHours    [1] => maximumHours    [2] => availabilityType    [3] => IsFulfilledByAmazon    [4] => ListingPrice    [5] => CurrencyCode    [6] => Amount    [7] => IsBuyBoxWinner    [8] => SellerId    [9] => Shipping    [10] => CurrencyCode    [11] => Amount    [12] => ShipsFrom    [13] => Country    [14] => State    [15] => SubCondition    [16] => IsFeaturedMerchant    [17] => SellerFeedbackRating    [18] => FeedbackCount    [19] => SellerPositiveFeedbackRating)

When I try to combine them like this with

print_r(array_combine($array1, $array2)); 

the new array looks like this because of the repeated key.

Array(    [minimumHours] => 24    [maximumHours] => 24    [availabilityType] => NOW    [IsFulfilledByAmazon] => false    [ListingPrice] =>     [CurrencyCode] => USD    [Amount] => 0.0
    [IsBuyBoxWinner] => false    [SellerId] => A2U0MT0XPL9B69    [Shipping] =>     [ShipsFrom] =>     [Country] => US    [State] => NJ    [SubCondition] => new    [IsFeaturedMerchant] => true    [SellerFeedbackRating] =>     [FeedbackCount] => 1062    [SellerPositiveFeedbackRating] => 86.0)

What I want is to add values ​​like _1,2,3 to the repeating keys.

[CurrencyCode] => USD [Amount] => 42.35 [CurrencyCode_1] => USD [Amount_1] => 0.0

Thank you very much in advance.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you very much.

    It solved all my problems.


  2. Walk over your key array, while keeping count of how many times you encountered each value already. If the current count is > 1, append the underscore and that number.

    $array = ['minimumHours', 'maximumHours', 'availabilityType',
              'IsFulfilledByAmazon', 'ListingPrice', 'CurrencyCode', 'Amount', 
              'IsBuyBoxWinner', 'SellerId', 'Shipping', 'CurrencyCode', 'Amount', 
              'ShipsFrom', 'Country', 'State', 'SubCondition', 'IsFeaturedMerchant',
              'SellerFeedbackRating', 'FeedbackCount', 'SellerPositiveFeedbackRating'];
    
    $valueCount = [];
    array_walk($array, function(&$item) use (&$valueCount) {
        $valueCount[$item] = ($valueCount[$item] ?? -1) + 1;
        $item .= $valueCount[$item] ? '_' . $valueCount[$item] : '';
    });
    
    print_r($array);
    

    Result:

    Array
    (
        [0] => minimumHours
        [1] => maximumHours
        [2] => availabilityType
        [3] => IsFulfilledByAmazon
        [4] => ListingPrice
        [5] => CurrencyCode
        [6] => Amount
        [7] => IsBuyBoxWinner
        [8] => SellerId
        [9] => Shipping
        [10] => CurrencyCode_1
        [11] => Amount_1
        [12] => ShipsFrom
        [13] => Country
        [14] => State
        [15] => SubCondition
        [16] => IsFeaturedMerchant
        [17] => SellerFeedbackRating
        [18] => FeedbackCount
        [19] => SellerPositiveFeedbackRating
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search