S
S
svilkov872016-10-08 14:33:03
PHP
svilkov87, 2016-10-08 14:33:03

Iterate over the elements of two arrays and output in a loop?

Good afternoon!
There are two arrays.
First with users:

array(3) {
  [0]=>
  array(2) {
    ["id"]=>
    &string(2) "14"
    ["name"]=>
    string(23) "Юрий"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(2) "50"
    ["name"]=>
    string(25) "Мария"
  }
  [2]=>
  array(3) {
    ["id"]=>
    string(2) "49"
    ["name"]=>
    string(25) "Сергей"
  }
}

The second array contains the comments of these users:
array(3) {
  [0]=>
  array(4) {
    ["id"]=>
    string(3) "155"
    ["text"]=>
    string(20) "Комментарий Сергея"
    ["article_id"]=>
    string(2) "70"
    ["user_id"]=>
    string(2) "49"
  }
  [1]=>
  array(4) {
    ["id"]=>
    string(3) "162"
    ["text"]=>
    string(14) "Комментарий Маши"
    ["article_id"]=>
    string(2) "70"
    ["user_id"]=>
    string(2) "50"
  }
  [2]=>
  array(4) {
    ["id"]=>
    string(3) "163"
    ["text"]=>
    string(49) "комментарий Юры"
    ["article_id"]=>
    string(2) "70"
    ["user_id"]=>
    string(2) "14"
  }
}

Question: how to make a query in the database (preferably with pdo, but it is possible without), in which the element of the first array['id'], corresponds to the element of the second array ["user_id"]?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zhainar, 2016-10-08
@zhainar

see SQL JOIN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question