2

I have three arrays and all are multidimensional.

What I want is:

Array1: 
id=[1,2,3]
name=[a,b,c]
price=[1,2,3]

Array2: 
id=[4,5,6]
name=[d,e,f]
price=[4,5,6]

Array3: 
id=[7,8,9]
name=[g,h,i]
price=[7,8,9]

So, how can i merge/combine the ID,Name and Price from array2 and array3, to array 1 so that I get this result:

Array1: 
id=[1,2,3,4,5,6,7,8,9]
name=[a,b,c,d,e,f,g,h,i]
price=[1,2,3,4,5,6,7,8,9]

I have to do it seperate. Or is it possible like this:

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:array1id aray2id array3id,array1name array2name array3name,array1price array2price array3price, nil];
2
  • Do each of the array contains a dictionary named id, name and price which contains another array? Commented May 21, 2014 at 9:46
  • yes thats correct: array with array Commented May 21, 2014 at 9:52

2 Answers 2

1

You can do it using loops by :

NSMutableArray *mergeArray = [NSMutableArray new];

for (NSInteger i=0; i<3; i++) {
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:arr1[i]];
    [tempArray addObjectsFromArray:arr2[i]];
    [tempArray addObjectsFromArray:arr3[i]];
    [mergeArray addObject:tempArray];
}

NSLog(@"%@",mergeArray);

Edit:

The input is:

    NSArray *arr1 = @[@[@1,@2,@3],
                      @[@"a", @"b", @"c"],
                      @[@"A", @"B", @"C"]
                      ];
    NSArray *arr2 = @[@[@4,@5,@6],
                      @[@"d", @"e", @"f"],
                      @[@"D", @"E", @"F"]
                      ];
    NSArray *arr3 = @[@[@7,@8,@9],
                      @[@"g", @"h", @"i"],
                      @[@"G", @"H", @"I"]
                      ];

The output is :

( ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), ( a, b, c, d, e, f, g, h, i ), ( A, B, C, D, E, F, G, H, I ) )

Sign up to request clarification or add additional context in comments.

2 Comments

Thaths not exactly what I want. The result should be a multidimensional array
sorry you were right. I wrote wrong function. That worked very great thanks!
1

First of all you should to have 3 NSMutableDictionary with 3 NSArray in.

You can simply get an union from arrays by KVC Collection Operators:

NSArray *newArray = [@[array1, array2, array3] valueForKeyPath:@"@unionOfArrays.self"];

or if you don't want duplicate:

NSArray *newArray = [@[array1, array2, array3] valueForKeyPath:@"@distinctUnionOfArrays.self"];

So in your case should be something like:

NSMutableDictionary dict1 {
    id:NSArray,
    name:NSArray,
    price:NSArray
}

and so:

dict1[@"id"] = [@[dict1[@"id"], dict2[@"id"], dict3[@"id"]] valueForKeyPath:@"@unionOfArrays.self"];

Use this example to understand:

NSDictionary *dict1 = @{@"id":@[@1,@2,@3],@"name":@[@"a",@"b",@"c"]};

NSDictionary *dict2 = @{@"id":@[@4,@5,@6],@"name":@[@"d",@"e",@"f"]};

NSDictionary *dict3 = @{@"id":@[@7,@8,@9],@"name":@[@"g",@"h",@"i"]};

NSMutableDictionary *mDict = [NSMutableDictionary new];

mDict[@"id"] = [@[dict1[@"id"], dict2[@"id"], dict3[@"id"]] valueForKeyPath:@"@unionOfArrays.self"];
mDict[@"name"] = [@[dict1[@"name"], dict2[@"name"], dict3[@"name"]] valueForKeyPath:@"@unionOfArrays.self"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.