A
A
Alexander Kirshin2017-09-08 11:22:29
PHP
Alexander Kirshin, 2017-09-08 11:22:29

I ask for help how to make a nested array from a database selection?

Good day to all friends, I have a question how can I convert the 2 arrays below to 1 by the album_id and book_id keys, if they differ in the array, or how can I immediately do it from the database.

[0]=>
  array(6) {
    ["album_id"]=>
    int(1)
    ["type"]=>
    int(1)
    ["album"]=>
    string(16) "я на море"
    ["usr_id"]=>
    int(1)
    ["date_album"]=>
    string(19) "2017-09-06 18:37:31"
    ["book"]=>
    array(4) {
      ["book_id"]=>
      int(1)
      ["book_name"]=>
      string(11) "книга1"
      ["alb_id"]=>
      int(1)
      ["photo"]=>
      array(8) {
        ["photo_id"]=>
        int(1)
        ["bk_id"]=>
        int(1)
        ["picture"]=>
        string(10) "photo1.jpg"
        ["url"]=>
        string(65) "188.225.82.218/content/users/albums/email/Я на море/book1/"
        ["location"]=>
        string(28) "Украина, Одесса"
        ["annotation"]=>
        string(13) "бла-бла"
        ["date_photo"]=>
        string(10) "22.07.2017"
        ["date_picture"]=>
        string(19) "2017-09-06 18:26:12"
      }
    }
  }
  [1]=>
  array(6) {
    ["album_id"]=>
    int(1)
    ["type"]=>
    int(1)
    ["album"]=>
    string(16) "я на море"
    ["usr_id"]=>
    int(1)
    ["date_album"]=>
    string(19) "2017-09-06 18:37:31"
    ["book"]=>
    array(4) {
      ["book_id"]=>
      int(1)
      ["book_name"]=>
      string(11) "книга1"
      ["alb_id"]=>
      int(1)
      ["photo"]=>
      array(8) {
        ["photo_id"]=>
        int(2)
        ["bk_id"]=>
        int(1)
        ["picture"]=>
        string(10) "photo2.jpg"
        ["url"]=>
        string(65) "188.225.82.218/content/users/albums/email/Я на море/book1/"
        ["location"]=>
        string(28) "Украина, Одесса"
        ["annotation"]=>
        string(13) "бла-бла"
        ["date_photo"]=>
        string(10) "22.07.2017"
        ["date_picture"]=>
        string(19) "2017-09-06 18:26:12"
      }
    }
  }
  [2]=>
  array(6) {
    ["album_id"]=>
    int(1)
    ["type"]=>
    int(1)
    ["album"]=>
    string(16) "я на море"
    ["usr_id"]=>
    int(1)
    ["date_album"]=>
    string(19) "2017-09-06 18:37:31"
    ["book"]=>
    array(4) {
      ["book_id"]=>
      int(1)
      ["book_name"]=>
      string(11) "книга1"
      ["alb_id"]=>
      int(1)
      ["photo"]=>
      array(8) {
        ["photo_id"]=>
        int(3)
        ["bk_id"]=>
        int(1)
        ["picture"]=>
        string(10) "photo3.jpg"
        ["url"]=>
        string(65) "188.225.82.218/content/users/albums/email/Я на море/book1/"
        ["location"]=>
        string(28) "Украина, Одесса"
        ["annotation"]=>
        string(13) "бла-бла"
        ["date_photo"]=>
        string(10) "22.07.2017"
        ["date_picture"]=>
        string(19) "2017-09-06 18:26:12"
      }
    }
  }
  [3]=>
  array(6) {
    ["album_id"]=>
    int(3)
    ["type"]=>
    int(1)
    ["album"]=>
    string(8) "дача"
    ["usr_id"]=>
    int(1)
    ["date_album"]=>
    string(19) "2017-09-07 13:10:19"
    ["book"]=>
    array(4) {
      ["book_id"]=>
      NULL
      ["book_name"]=>
      NULL
      ["alb_id"]=>
      NULL
      ["photo"]=>
      array(8) {
        ["photo_id"]=>
        NULL
        ["bk_id"]=>
        NULL
        ["picture"]=>
        NULL
        ["url"]=>
        NULL
        ["location"]=>
        NULL
        ["annotation"]=>
        NULL
        ["date_photo"]=>
        NULL
        ["date_picture"]=>
        NULL
      }
    }
  }
}

ad97e89f584345169e5dfc8381e87df6.jpg
as a result, the output should be one array:
c 2 album_id, and not 3, also, depending on the album, book should be nested in it and in books photo
Please help how to implement this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-09-08
@thewind

array_walk, closures - familiar words?) one pass and that's it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question