0

Complete code

I need sort an array based on another array. Currently I have this function that creates new array containing user information.

Each user is defined in JSON string and is stored in MySQL like so:

{"Firstname":"John","Lastname":"Smith","Email":"johnsmith@outlook.com"},
{"Firstname":"Hans","Lastname":"Jobens", "Age", "Too old"}

I loop every object to get each header and then push them into headers array.

$headers = array("Age", "Email", "Firstname", "Lastname")

Then I create new user object for each user row.

$user_object = array();

And add each header to each user object and fill them with data or placeholder "-"

//add missing keys and values
foreach($users as $user){
    //get user from user row
    $row = $user['user'];
    //loop headers for each row
    foreach($headers as $header){
        //check if $header found in each row
        if (!array_key_exists($header,$row)){
            //if not found
            $row->$header = "-";
        }
    }
}

Objects are pushed into users array and now they look like this:

{"Firstname":"John","Lastname":"Smith","Email":"johnsmith@outlook.com","Age":"-"},
{"Firstname":"Hans","Lastname":"Jobens","Age":"Too old","Email":"-"}

But they are not in the right order. I want each user's object keys to maintain their values but be in order by $headers array.

Needed format:

{"Age":"-","Email":"johnsmith@outlook.com","Firstname":"John","Lastname":"Smith"},
{"Age":"Too old","Email":"-","Firstname":"Hans","Lastname":"Jobens"}

Complete code

EDIT:

Function in short:

  1. Initialize $list, $users and $headers array
  2. Get array of user information (stored as javascript object) from mysql
  3. Decode and loop every user and get each object key
  4. Append each new key into $headers array
  5. Loop $users again to add missing keys and placeholder (dash mark)
  6. Arrange OBJECT by $headers array
  7. Append $headers and $users into $list
  8. echo json_encode($list)

Console.log

Jaakko Uusitalo
  • 497
  • 6
  • 19

3 Answers3

0

use array_replace function

$user_object = array();
foreach($users as $user){
    // array which set order
    $template = array_fill_keys(["Age", "Email", "Firstname", "Lastname"], '-');
    // Get current existing value
    $user_object[] = array_replace($template, $user['user']);
}
print_r($user_object);

demo

splash58
  • 25,216
  • 3
  • 19
  • 31
0

One way is by pushing each corresponding data (-) if null as you loop thru the headers.

foreach($users as $key => $user){
    //get user from user row
    $row = $user['user'];
    // Set new row
    $newRow = [];
    // Loop thru headers
    foreach ($headers as $header) {
        // Check if row has the header, set '-' if not
        $newRow[$header] = (!array_key_exists($header, $row)) ? '-' : $row[$header];
    }
    // set key of users to newRow
    $users[$key] = $newRow;
}

print_r($users); // Print users
Erwin
  • 1,978
  • 1
  • 8
  • 18
0

You can order an array by key using another as a reference by combining uksort() and array_search():

uksort($array, function ($a, $b) use ($headers) {
 return array_search($a, $headers) > array_search($b, $headers);
}); 
dhinchliff
  • 1,006
  • 8
  • 16