0

I have simple foreach loop and get data.. There is two array i get. I want to combine in single array.How can i do this ?

My Foreach loop :

foreach ($productInformation as $proInfo) {
        $userApprovalDat[]= UserProduct::userPendingApprovalList($proInfo->product_id, $id);
    }

Response for return data: Response :

 [
        {
            "id": 4,
            "user_id": 3,
            "product_id": 1,
            "approval_status": "Pending",
            "product_role": "Member",
            "created_at": "2018-04-27 18:49:24",
            "updated_at": "2018-04-27 18:49:24"
        },
        {
            "id": 7,
            "user_id": 4,
            "product_id": 1,
            "approval_status": "Pending",
            "product_role": "Member",
            "created_at": "2018-05-03 17:25:49",
            "updated_at": "2018-05-03 17:25:49"
        }
    ],
    [
        {
            "id": 2,
            "user_id": 2,
            "product_id": 2,
            "approval_status": "Pending",
            "product_role": "Member",
            "created_at": "2018-04-27 18:47:43",
            "updated_at": "2018-04-27 18:47:43"
        },
        {
            "id": 5,
            "user_id": 3,
            "product_id": 2,
            "approval_status": "Pending",
            "product_role": "Member",
            "created_at": "2018-04-27 18:49:24",
            "updated_at": "2018-04-27 18:49:24"
        },
        {
            "id": 8,
            "user_id": 4,
            "product_id": 2,
            "approval_status": "Pending",
            "product_role": "Member",
            "created_at": "2018-05-03 17:25:49",
            "updated_at": "2018-05-03 17:25:49"
        }
    ]

I want to combine in one array how can i do this ?

Javed
  • 767
  • 2
  • 16
  • 35

1 Answers1

2

You need to have something like the following (very similar to Aman's answer, just more detail)

$productInformation = json_decode($source, true); // decode the JSON as an array

// Create a base array for information to be added to
$data = array();
// Loop the product information from the JSON to get each individual product
foreach ($productInformation as $proInfo) 
{
    // Using your function, get the "fetched" values
    $fetched = UserProduct::userPendingApprovalList($proInfo->product_id, $id);
    // For each of the fetched values, add the value of this to the information array
    foreach ($fetched as $key => $value)
    {
        $data[]=$value;
    }
}

For each of the products in the JSON, you are wanting to collect information pertaining to these, so you'll need to loop the information held within each product to get this into a single dimensional array.
This is because the normal JSON decode will create a multi dimensional object and using the json_decode($source, true); passes JSON_OBJECT_AS_ARRAY and this will allow you to "flatten" the data through looping the information (http://php.net/manual/en/function.json-decode.php)

As comments have said also - look into array_merge (http://php.net/manual/en/function.array-merge.php) as a better solution

Can O' Spam
  • 2,459
  • 1
  • 15
  • 42
  • With respect - you answer was good, (hence removed my d/v once comments added), but your explanation was lacking in details - hence why I credited yours at the top and just gave extra detail :) – Can O' Spam May 10 '18 at 11:35