0

I want to create array of array

ex-

let myImages: [[String]] = [

    ["image_url_0_0", "image_url_0_1", "image_url_0_2", "image_url_0_3"],

    ["image_url_1_0", "image_url_1_1", "image_url_1_2"],

    ["image_url_2_0", "image_url_2_1", "image_url_2_2", "image_url_2_3", "image_url_2_4"]
]

I tried this, but it doesn't work perfectly.

var myArray :[[String]] = []

for custom_attributes! in items { // items is the json items array
    var arrImage: [String] = []
    for customParams in custom_attributes! {

        if let attCode = customParams["attribute_code"] as? String, attCode == "small_image"   {
             print(customParams["value"])

             var myString = customParams["value"] as! String
             arrImage.append(myString)
         }

     }
     myArray.append(arrImage)
 }

But I get result in this form

[["image_url_0_0"], ["image_url_0_0","image_url_0_1"], ["image_url_0_0","image_url_0_1","image_url_0_3"].....]

How should I do this?Please Help.

  • 2
    Paste your json in question. – Pratik Sodha Oct 03 '19 at 07:21
  • intialize your array like that var myArray :[[String]] = [[:]] – Ben10 Oct 03 '19 at 09:31
  • You are not getting any correct answers (so far) so as already requested, show us a sample of your input data so we understand what to work with. – Joakim Danielson Oct 03 '19 at 12:18
  • @-Joakim Danielson, [ { "id": 6, "products": { "items": [ { "custom_attributes": [ { "attribute_code": "small_image", "value": "small_image" } ] }, { "custom_attributes": [ { "attribute_code": "small_image", "value": "small_image" } ] } ] } } ] this is my Json – Kaustubh Navale Oct 04 '19 at 04:53
  • Possible duplicate of [How to add/insert element in array one after another like stack swift 5](https://stackoverflow.com/questions/58198403/how-to-add-insert-element-in-array-one-after-another-like-stack-swift-5). As you can see someone posted a question with exactly the same Json data already. – Joakim Danielson Oct 04 '19 at 11:33

2 Answers2

0

First you need to create simple string array and then append this array to your myArray. Check following code may be it's help

var myArray :[[String]] = []

for custom_attributes! in items { // items is the json items array
    var arrImage: [String] = []
    for customParams in custom_attributes! {

        if let attCode = customParams["attribute_code"] as? String, attCode == "small_image"   {
             print(customParams["value"])

             var myString = customParams["value"] as! String
             arrImage.append(myString)
         }

     }
     myArray.append(arrImage)
 }
serhat sezer
  • 1,330
  • 1
  • 10
  • 26
Ashish
  • 706
  • 8
  • 22
  • Hey it didnt work.it gives me result [ ["image_url_0_0", "image_url_0_1", "image_url_0_2", "image_url_0_3"], ["image_url_1_0", "image_url_1_1", "image_url_1_2"], ["image_url_0_0", "image_url_0_1", "image_url_0_2", "image_url_0_3""image_url_2_0", "image_url_2_1", "image_url_2_2", "image_url_2_3", "image_url_2_4"] ] – Kaustubh Navale Oct 03 '19 at 09:01
  • This is array of array and it's same as your example in your post – Ashish Oct 03 '19 at 09:27
  • hey it adds previous data into next and so on..so in last array of arrray it contain all the data.you can see it in my question – Kaustubh Navale Oct 03 '19 at 09:30
0

You try this

var myArray :[[String]] = []

for custom_attributes! in items { // items is the json items array
     let arr : [String] = []
    for customParams in custom_attributes! {

        if(customParams["attribute_code"] as! String == "small_image") 
             {
             print(customParams["value"])

             var myString = customParams["value"] as! String

             arr.append(myString)
         }

     }
myArray.append(arr)

 }
Lokesh gupta
  • 94
  • 10