skip to Main Content

My query:

SELECT * 
FROM outlets 
INNER JOIN users ON outlets.owner=users.id;

The result is an array in which the indexes from one table match those of another table. Is it possible to somehow make such a request that owner = array, and there is already a structure
Result:

Array ( [id] => 1 [owner] => 1 [name] => Рома [email] => admin  => admin [password] => admin [role] => 1 [photo] => [permissions] => {"menu": {"menu.shop": 1, "menu.users": 1, "menu.stocks": 1, "menu.clients": 1, "menu.finances": 1, "menu.partners": 1, "menu.products": 1, "menu.purchase": 1, "menu.dashboard": 1}} [status] => 1 )

2

Answers


  1. To return a result as an associative array use

    Login or Signup to reply.
  2. If I understood correctly. You want to have an owner value containing data of the user (but not having data on the same level).

    Option 1: By SQL you can do 2 separate queries and join results manually by PHP. But then you may have performance penalties.

    Option 2: As a recommendation, if you need such a payload, just reformat your array as desired manually like this:

    $originalArray = ['id' => 1, 'owner' => 1, 'name' => 'test', 'username' => 'test'];
    
    $reformatedArray = [];
    foreach($originalArray as $values){
        $reformatedArray['id'] = $values['id'];
        $reformatedArray['owner']['name'] = $values['name'];
        $reformatedArray['owner']['name'] = $values['username'];
        // ...
    }
    
    print_r($reformatedArray);
    
    // ['id' => 1, 'owner' => ['name' => 'test', 'username' => 'test']]
    

    Hope it helps.

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