PHP array copy certain keys, built-in functions? Nested loop performance?

后端 未结 2 1368
孤独总比滥情好
孤独总比滥情好 2021-02-07 12:09

I have a PHP array that I\'d like to duplicate but only copy elements from the array whose keys appear in another array.

Here are my arrays:

$data[123] =         


        
2条回答
  •  走了就别回头了
    2021-02-07 12:32

    Why not load the entire result set into an array, then begin processing with nested loops?

    $query_result = mysql_query($my_query) or die(mysql_error());
    $query_rows = mysql_num_rows($query_result);
    for ($i = 0; $i < $query_rows; $i++)
    {
        $row = mysql_fetch_assoc($query_result);
        // 'key' is the name of the column containing the data key (123)
        // 'value' is the name of the column containing the value (aaa)
        $data[$row['key']] = $row['value']; 
    }
    foreach ($data as $key => $value)
    {
        if ( in_array($key, $keys_to_copy)) 
        {
            $copied_data[$key] = $value;
        }
    }
    

提交回复
热议问题