skip to Main Content

I have a list of arrays with values ​​inside each array. I want to sort my list based on one of these values ​​so that unique arrays are displayed first, then duplicate arrays.
I don’t want any member to be removed from the array, I just want my array to be sorted

this is the list that I have

array:6 [▼
  0 => array:8 [▼
    "id" => 35
    "answer" => null
    "type" => 3
    "user_id" => 9
    "question_id" => 36
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  1 => array:8 [▼
    "id" => 36
    "answer" => null
    "type" => 3
    "user_id" => 9
    "question_id" => 36
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  2 => array:8 [▼
    "id" => 37
    "answer" => "Gello"
    "type" => 0
    "user_id" => 9
    "question_id" => 37
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  3 => array:8 [▼
    "id" => 38
    "answer" => "Uho"
    "type" => 0
    "user_id" => 9
    "question_id" => 37
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  4 => array:8 [▼
    "id" => 39
    "answer" => "Who"
    "type" => 1
    "user_id" => 9
    "question_id" => 38
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  5 => array:8 [▼
    "id" => 40
    "answer" => "Window"
    "type" => 1
    "user_id" => 9
    "question_id" => 38
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:40.000000Z"
    "updated_at" => "2023-04-07T09:03:40.000000Z"
  ]
]

and I want to sort by question_id. I want my list to eventually become this list

array:6 [▼
  0 => array:8 [▼
    "id" => 35
    "answer" => null
    "type" => 3
    "user_id" => 9
    "question_id" => 36
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  2 => array:8 [▼
    "id" => 37
    "answer" => "Hello"
    "type" => 0
    "user_id" => 9
    "question_id" => 37
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  4 => array:8 [▼
    "id" => 39
    "answer" => "Who"
    "type" => 1
    "user_id" => 9
    "question_id" => 38
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  1 => array:8 [▼
    "id" => 36
    "answer" => null
    "type" => 3
    "user_id" => 9
    "question_id" => 36
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  3 => array:8 [▼
    "id" => 38
    "answer" => "Uho"
    "type" => 0
    "user_id" => 9
    "question_id" => 37
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]

  5 => array:8 [▼
    "id" => 40
    "answer" => "Window"
    "type" => 1
    "user_id" => 9
    "question_id" => 38
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:40.000000Z"
    "updated_at" => "2023-04-07T09:03:40.000000Z"
  ]
]

3

Answers


  1. Well, it looks like returned data from DB, it depends on that what you use:

    Login or Signup to reply.
  2. as you mention you are having an array this can be a proper solution for it

    <?php 
    $records =[
      [
        "id" => 1,
        "user_id" => 9,
        "question_id" => 16,
        "answer_id" => 1,
      ],
      [
        "id" => 2,
        "user_id" => 9,
        "question_id" => 26,
        "answer_id" => 1,
      ],
      [
        "id" => 3,
        "user_id" => 9,
        "question_id" => 6,
        "answer_id" => 1,
      ],
      [
        "id" => 13,
        "user_id" => 9,
        "question_id" => 36,
        "answer_id" => 1,
      ],
      [
        "id" => 18,
        "user_id" => 9,
        "question_id" => 26,
        "answer_id" => 1,
      ],
        [
        "id" => 21,
        "user_id" => 9,
        "question_id" => 06,
        "answer_id" => 1,
      ],
      [
        "id" => 21,
        "user_id" => 9,
        "question_id" => 11,
        "answer_id" => 1,
      ],
    ];
    
    function sortCallback($firstRecord,$secondRecord){
        return $firstRecord['question_id']>$secondRecord['question_id'] ;
    }
    $recordsCopy=$records;
    usort($recordsCopy,"sortCallback");
    print_r($recordsCopy);
    
    ?>
    

    but it is a Larvel collection then you can sort method of it with same call back method.

    Login or Signup to reply.
  3. You would actually need to use a map/set to store rows per each question ID. Then use array_column and array_merge to make them appear group by group.

    Note: Since yours is a Laravel collections, you can simply use ->toArray() to convert it to a PHP array and use the code below!

    Snippet:

    <?php
    
    $set = [];
    $result = [];
    $maxColumns = 0;
    
    foreach($records as $r){
       $set[ $r['question_id'] ] = $set[ $r['question_id'] ] ?? [];
       $set[ $r['question_id'] ][] = $r;
       $maxColumns = max($maxColumns, count($set[ $r['question_id'] ]));
    }
    
    for($i = 0; $i < $maxColumns; ++$i){
        $result = array_merge($result, array_column($set, $i));
    }
    
    print_r($result);
    

    Online Demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search