Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6649449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:46:31+00:00 2026-05-26T00:46:31+00:00

I’ve got this array: Array ( [0] => Array ( [id]=>1 [account_id] => 1

  • 0

I’ve got this array:

Array
(
    [0] => Array
        (
            [id]=>1
            [account_id] => 1
            [object_id] => 43
            [object_type] => PHOTO
            [action_type] => UPLOAD_PHOTO
        )

    [1] => Array
        (
            [id] => 1
            [account_id] => 1
            [object_id] => 42
            [object_type] => PHOTO
            [action_type] => UPLOAD_PHOTO
        )

    [2] => Array
        (
            [id] => 1
            [account_id] => 1
            [object_id] => 41
            [object_type] => PHOTO
            [action_type] => UPLOAD_PHOTO
        )

    [3] => Array
        (
            [id] => 2
            [account_id] => 2
            [object_id] => 1
            [object_type] => USER
            [action_type] => FOLLOW_USER
        )

    [4] => Array
        (
            [id] => 1
            [account_id] => 1
            [object_id] => 2
            [object_type] => USER
            [action_type] => FOLLOW_USER
        )

    [5] => Array
        (
            [id] => 1
            [account_id] => 1
            [object_id] => 1
            [object_type] => PHOTO
            [action_type] => UPLOAD_PHOTO
        )

)

Now I want to group elements have same value (for example UPLOAD_PHOTO) by id as Primary Key and action_type as Secondary Key, like:

Array
(
    [0] => Array(
        [id] => 1
        [account_id] => 1
        [actions] => array(
          [1] => array(
              [object_id] => 42
              [object_type] => PHOTO
              [action_type] => UPLOAD_PHOTO
           )
          [2] => array(
              [object_id] => 43
              [object_type] => PHOTO
              [action_type] => UPLOAD_PHOTO
           )
         )      

 ) 
[2] => Array
    (
        [id] => 1
        [account_id] => 1
        [actions] = array(
            [0] => Array
             (
               [object_id] => 3
               [object_type] => USER
               [action_type] => FOLLOW_USER
             )
            [1] => Array
             (
               [object_id] => 4
               [object_type] => USER
               [action_type] => FOLLOW_USER
             )

        )

    )

)

I tried some solutions but didn’t succeed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:46:31+00:00Added an answer on May 26, 2026 at 12:46 am

    When constructing your output array, you’ll want to use meaningful array keys. That way you can find the out element that the in element needs to be added to:

    $in = (...your array...);
    $out = array();
    
    foreach($in as $element) {
       $id = $element['id'];
       //If that id hasn't been seen yet, create the out element.
       if (!isset($out[$id])) {
           $out[$id] = array(
               'id'=>$element['id'],
               'account_id' => $element['account_id'],
               'actions' => array()
           );
       }
    
       //Add that action to the out element
       $out[$id]['actions'][] = array(
           'object_id' => $element['object_id'],
           'object_type' => $element['object_type'],
           'action_type' => $element['action_type']
       );
    }
    

    I’m not sure why your input elements have different fields… If this is a desired feature, where one set of possible fields belongs to the id group and another set belongs to the action, you can do this instead (slightly less readable, but more flexible):

    $in = (...your array...);
    $out = array();
    //These define which fields from an 
    //input element belong to the id,
    //and which to the action.
    $id_fields = array_flip(array('id','account_id','object_user_id');
    $action_fields = array_flip(array('object_id','object_type','action_type');
    
    foreach($in as $element) {
       $id = $element['id'];
       //If that id hasn't been seen yet, create the out element.
       if (!isset($out[$id])) {
           $out[$id] = array_intersect_key($element, $id_fields);
           $out[$id]['actions'] = array();
       }
    
       //Add that action to the out element
       $out[$id]['actions'][] = array_intersect_key($element, $action_fields);
    }
    

    This is a better solution if the input elements can be different, because if an expected field (other than 'id') is missing, the script will cope with it easily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.