首页 > 解决方案 > Array Combination need as like below

问题描述

I have 2 arrays $finalTitle and $finalDescription i have used array_merge and create main array as $finalResult and output as below

[
    {
        title: "ABC's Script"
    },
    {
        title: "DEF's Script"
    },
    {
        title: "GHI's Script"
    },
    {
        title: "JKL's Script"
    },
    {
        description: "My Name is ABC, I am living in Rajkot, I can able to speak English Language."
    },
    {
        description: "My Name is DEF, I am living in Surat, I can able to speak Hindi Language."
    },
    {
        description: "My Name is GHI, I am living in Vadodara, I can able to speak Tamil Language."
    },
    {
        description: "My Name is JKL, I am living in Ahmedabad, I can able to speak Gujarati Language."
    }
]

I want array result as below in final output. I want title and description in single array instead of different array name.

[
    {
        title: "ABC's Script",
        description: "My Name is ABC, I am living in Rajkot, I can able to speak English Language."
    },
    {
        title: "DEF's Script",
        description: "My Name is DEF, I am living in Surat, I can able to speak Hindi Language."
    },
    {
        title: "GHI's Script",
        description: "My Name is GHI, I am living in Vadodara, I can able to speak Tamil Language."
    },
    {
        title: "JKL's Script",
        description: "My Name is JKL, I am living in Ahmedabad, I can able to speak Gujarati Language."
    }
]

标签: phparraysmultidimensional-array

解决方案


Rather than merging the two arrays, could you not do a nested for loop so the data at each point in the array is returned then concatenate the result?

 I = 0;
 j = 0;
 For (i = 0; 0 < finalTitle.length; I++){
       For (j = 0; 0 < finalDescription.length, I++) {
           finalTitle + ‘/n’ + finalDescription
       }
  }

推荐阅读