There are 2 arrays: one is collected from a query to the database, the other is a response from the kernel via ZeroMQ. Here they are:

one.

array(2) { [0]=> array(4) { ["id"]=> string(1) "1" ["title"]=> string(25) "Numer One" ["enabled"]=> string(1) "1" ["source"]=> string(71) "rtsp://..." } [1]=> array(4) { ["id"]=> string(1) "2" ["title"]=> string(27) "Number Two" ["enabled"]=> string(1) "1" ["source"]=> string(87) "rtsp://..." } } 

2

 array(2) { [6139]=> string(1) "1" [6140]=> string(1) "2" } 

The values ​​in the second array are id, the keys are PID. What I'm trying to do:

 array(2) { [0]=> array(4) { ["id"]=> string(1) "1" ["pid"]=> string(4) "6139" ["title"]=> string(25) "Numer One" ["enabled"]=> string(1) "1" ["source"]=> string(71) "rtsp://..." } [1]=> array(4) { ["id"]=> string(1) "2" ["pid"]=> string(4) "6140" ["title"]=> string(27) "Number Two" ["enabled"]=> string(1) "1" ["source"]=> string(87) "rtsp://..." } } 

PS I know very well how to implement using SQL, but how to do it in PHP - I won’t put my mind to it :(

    3 answers 3

    As I understand it, all the values ​​in the second array are unique, so to simplify the process, turn it over

     $ar2 = array_flip($ar2); foreach($ar1 as &$i) { // Если во втором массиве есть элемент с соответствующим id if (isset($ar2[$i['id']])) { // Добавляем его в первый массив $i['pid'] = $ar2[$i['id']]; } } print_r($ar1); 

    demo

    • Yes, the PIDs are, of course, unique. I can't turn over in the core - there is a half day picking around ... - Vasiliy
    • so array_flip will turn it over - splash58
    • Well, this is understandable, I just explained why I had to use a crutch in the form of turning. - Vasiliy

    You can do the following:

     <?php $sql = array( array( "id" => "1", "title" => "Numer One", "enabled" => "1", "source" => "rtsp://..." ), array( "id" => "2", "title" => "Number Two", "enabled" => "1", "source" => "rtsp://..." ) ); $json = array( 6139 => "1", 6140 => "2" ); foreach($sql as $key => $arr) { $sql[$key]['pid'] = array_search($sql[$key]['id'], $json); } echo '<pre>'; print_r($sql); 
    • so much slower than checking the key and it is unclear whether pid => null is needed if id is not in the second array - splash58
    • @ splash58 much slower in an array of two values? Something tells me that on such volumes, the speed of all solutions will be the same. Tens and hundreds of thousands of elements in the array in the question are not stated. - cheops
    • I understand that this is a philosophical question, but we write something for the general case :) And then you can and so $ sql [1] ['pid'] = 6139; :) - splash58
    • In general - yes, it will be slow. And for my api, where the delay from the core will come down for more than a second. Now here I do not know, mark as a solution? - Vasiliy
    • @GrigoryE. Zelenograd! And where are you from, if not a secret? - splash58

    in addition to other answers, I advise you to go through this link and look at the join algorithms.

    I had to develop joins in php from different servers, and in principle, I would not advise the enemy to do this in php and, moreover, to do it in php with a mountain of data. For my particular case (join from different servers), by the way, the least possible was the option of creating a temporary table on server A, uploading data from server B to it, joining temporary on server A and returning the data to the application. (it was a long time ago and I won’t be surprised if there are already advanced solutions for such cases).

    In addition, I would not be surprised if it would be faster for you to fill in the data from the ZMQ in the database and join there. Not at all surprised.