Jump to content

How to process the multi dimensional array into specific array?


soyaslim
 Share

Go to solution Solved by Barand,

Recommended Posts

Hello guys. I want the array to be processed into my final array how am I supposed to achieve this. Below is the array I want to process.

Array 
(
    Array
      (
          [part_id] => 2338117
          [supplier] => COOLDRIVE DISTRIBUTION
          [quantity] => 12
      )
    Array
      (
          [part_id] => 2338117
          [supplier] => ROLAN
          [quantity] => 20
      )
    Array
      (
          [part_id] => 51154
          [supplier] => ROLAN
          [quantity] => 20
      )
)

This is the final array I want.

Array
(
    [COOLDRIVE DISTRIBUTION] => Array 
          (
              [proudctID] => Array 
                     (
                         [0] => 2338117
                     )
           )
    [ROLAN] => Array 
           (
               [productID] => Array 
                  (
                       [0] => 2338117
                       [1] => 51154
                  )
           )
)

 

Edited by soyaslim
Link to comment
Share on other sites

  • soyaslim changed the title to How to process the multi dimensional array into specific array?
for ($i=0; $i<count($results)-1; $i++) {
            $productID = ($results[0]['supplier'] == $results[$i+1]['supplier']) ? array($results[$i]['rolan_part_id'], $results[$i+1]['rolan_part_id']) : array($results[$i+1]['rolan_part_id']);
            $finalArray[$results[$i]['supplier']] = array(
                "productID" => $productID,
            );
        }
        print_r($finalArray);

so, the $results contain the array I want to change, which is three nested arrays. The above output is:

Array
(
    [COOLDRIVE DISTRIBUTION] => Array
        (
            [productID] => Array
                (
                    [0] => 2338117
                )

        )

    [ROLAN] => Array
        (
            [productID] => Array
                (
                    [0] => 51154
                )

        )

)

I couldn't get the other productID in 'ROLAN' array.

Link to comment
Share on other sites

  • Solution

All you need to do is loop through your original array using foreach() and append the products to a new array, providing the required key values.

foreach ($orig as $rec) 
    $new[ $rec['supplier'] ]['productID'][] = $rec['part_id'];

Result for $new

Array
(
    [COOLDRIVE DISTRIBUTION] => Array
        (
            [productID] => Array
                (
                    [0] => 2338117
                )

        )

    [ROLAN] => Array
        (
            [productID] => Array
                (
                    [0] => 2338117
                    [1] => 51154
                )

        )

)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.