3

I have a little problem, and I need help.

I want to loop through a multidimensional array, and every time I find a value for a key ex."name" that is equal to ex. "Hello". I want to copy that array object into another array.

How do I do that?

2
  • 3
    You will probably find that Stack Overflow provides better answers if you include the code you already wrote in your question. Commented Aug 19, 2012 at 9:04
  • I agree with Adam. Without codes it is difficult to understand exact situation. Commented Aug 19, 2012 at 9:39

2 Answers 2

7

This is how you would do it:

NSArray* newArray = [NSArray arrayWithArray:oldArray]
Sign up to request clarification or add additional context in comments.

2 Comments

-1; Morten wants to copy something only when certain values / conditions are satisfied, so your answer isn't as helpful as it should be.
As simple as that! +1
2

I assume you have a 2-dimensional array and since I don't know how your objects within the array look like I take the id type and the method valueForKey:

 NSMutableArray *tmp = [[NSMutableArray alloc] init];
 for(NSArray *dim1Array in yourMultidimensionalArray)
 {
    for(id obj in dim1Array)
    {
       if([[obj valueForKey:@"name"] isEqualToString:@"Hello"])
       {
          [tmp addObject:dim1Array];
          break; // I assume you only want to add it once
       }
    }
 }

1 Comment

I may have misunderstood the question but for me it seems as if the OP is asking for a copy of the object and not the array. But then again, I am unsure. Anyways, in case I got it right, only the most inner two lines would have to be changed towards [tmp addObject:obj];.

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.