From this site for the university of Nottingham QFT notes in the notes for Lecture 3 there is an exercise on page 4 to show that $$\hat a(\boldsymbol{k})=\int d^3x\left[E(\boldsymbol{k})\hat\phi(\boldsymbol{x})+i\hat \pi(\boldsymbol{x})\right]e^{i\boldsymbol{k}\cdot \boldsymbol{x}}\tag{1}$$ $$\hat a^\dagger(\boldsymbol{k})=\int d^3x\left[E(\boldsymbol{k})\hat\phi(\boldsymbol{x})-i\hat \pi(\boldsymbol{x})\right]e^{-i\boldsymbol{k}\cdot \boldsymbol{x}}\tag{2}$$ by taking the Fourier transform and solving for the operator coefficients $\hat a(\boldsymbol{k}),\,\hat a^\dagger(\boldsymbol{k})$ in $$\hat \phi (\boldsymbol{x})=\int\frac{d^3\boldsymbol{k}}{(2\pi)^32E(\boldsymbol{k})}\left(\hat a(\boldsymbol{k})e^{i\boldsymbol{k}\cdot\boldsymbol{x}}+\hat a^\dagger(\boldsymbol{k})e^{-i\boldsymbol{k}\cdot\boldsymbol{x}}\right)\tag{3}$$
$$\hat \pi (\boldsymbol{x})=\frac{1}{2i}\int\frac{d^3\boldsymbol{k}}{(2\pi)^3}\left(\hat a(\boldsymbol{k})e^{i\boldsymbol{k}\cdot\boldsymbol{x}}-\hat a^\dagger(\boldsymbol{k})e^{-i\boldsymbol{k}\cdot\boldsymbol{x}}\right)\tag{4}$$
So to try to prove eqns. $(1)$ and $(2)$ I start by taking the Fourier transform of eqn. $(3)$ by multiplying both sides by $e^{-i\boldsymbol{k}\cdot\boldsymbol{x^\prime}}$ and integrating over $\boldsymbol{x^\prime}$, (I have chosen $\boldsymbol{x^\prime}$ as an integration variable to distinguish it from $\boldsymbol{x}$):
$$\mathcal{F}\left[\phi (\boldsymbol{x})\right] =\int d^3 \boldsymbol{x^\prime} e^{-i\boldsymbol{k}\cdot\boldsymbol{x^\prime}}\hat \phi (\boldsymbol{x})$$
$$=\int d^3 \boldsymbol{x^\prime} e^{-i\boldsymbol{k}\cdot\boldsymbol{x^\prime}}\int\frac{d^3\boldsymbol{k}}{(2\pi)^32E(\boldsymbol{k})}\hat a(\boldsymbol{k})e^{i\boldsymbol{k}\cdot\boldsymbol{x}}+\int d^3 \boldsymbol{x^\prime} e^{-i\boldsymbol{k}\cdot\boldsymbol{x^\prime}}\int\frac{d^3\boldsymbol{k}}{(2\pi)^32E(\boldsymbol{k})}\hat a^\dagger(\boldsymbol{k})e^{-i\boldsymbol{k}\cdot\boldsymbol{x}}$$
$$=\int d^3 \boldsymbol{x^\prime} \int\frac{d^3\boldsymbol{k}}{(2\pi)^3}e^{i\boldsymbol{k}\cdot\left(\boldsymbol{x}-\boldsymbol{x^\prime}\right)}\color{red}{\frac{\hat a(\boldsymbol{k})}{2E(\boldsymbol{k})}}+\int d^3 \boldsymbol{x^\prime} \int\frac{d^3\boldsymbol{k}}{(2\pi)^3}e^{-i\boldsymbol{k}\cdot\left(\boldsymbol{x}+\boldsymbol{x^\prime}\right)}\color{red}{\frac{\hat a^\dagger(\boldsymbol{k})}{2E(\boldsymbol{k})}}\tag{A}$$
$$=\int d^3 \boldsymbol{x^\prime} \frac{\hat a(\boldsymbol{k})}{2E(\boldsymbol{k})}\delta^{(3)}\left(\boldsymbol{x}-\boldsymbol{x^\prime}\right)+\int d^3 \boldsymbol{x^\prime} \frac{\hat a^\dagger(\boldsymbol{k})}{2E(\boldsymbol{k})}\delta^{(3)}\left(\boldsymbol{x}+\boldsymbol{x^\prime}\right)\tag{B}$$
$$=\frac{\hat a(\boldsymbol{k})+\hat a^\dagger(\boldsymbol{k})}{2E(\boldsymbol{k})}\tag{C}$$
There are two steps where I'm doubtful of the logic I used in arriving at $$\mathcal{F}\left[\phi (\boldsymbol{x})\right]=\frac{\hat a(\boldsymbol{k})+\hat a^\dagger(\boldsymbol{k})}{2E(\boldsymbol{k})}\tag{5}$$
For $(\mathrm{A})\to (\mathrm{B})$, I used the exponential form of the $\delta$ function $$\delta(x-\alpha)=\frac{1}{2\pi} \int_{-\infty}^\infty e^{ip(x-\alpha)}\,dp$$ which for my case, in 3d, is $$\delta^{(3)}(\boldsymbol{x}-\boldsymbol{x^\prime})=\frac{1}{(2\pi)^3} \int e^{i\boldsymbol{k}\cdot(\boldsymbol{x}-\boldsymbol{x^\prime})}\,d^3\boldsymbol{k}$$ $$=\frac{1}{(2\pi)^3}\int e^{-i\boldsymbol{k}\cdot(\boldsymbol{x}-\boldsymbol{x^\prime})}\,d^3\boldsymbol{k}=\delta^{(3)}(\boldsymbol{x^\prime}-\boldsymbol{x})\tag{6}$$ where I have assumed that since the Dirac-delta is an even 'function' I can replace $i$ with $-i$ in the complex exponential and the result doesn't change. But the problem is that the integrand in both terms of $(\mathrm{A})$ have factors (marked red) that depend on the integration variable, $\boldsymbol{k}$, so I'm not sure I can apply eqn. $(6)$ to $(\mathrm{A})$.
For $(\mathrm{B})\to (\mathrm{C})$ I used the fact that $$\int_{-\infty}^\infty d^3 \boldsymbol{x^\prime}\delta^{(3)}(\boldsymbol{x}-\boldsymbol{x^\prime})=\int_{-\infty}^\infty d^3 \boldsymbol{x^\prime}\delta^{(3)}(\boldsymbol{x}+\boldsymbol{x^\prime})=1$$ But I'm not sure if such a manipulation is defined in the 3 dimensional Dirac-delta function.
To summarize, I am trying to derive eqns. $(1)$ and $(2)$ by taking the Fourier transform of $(3)$ and $(4)$, respectively, but I fail to see how this approach is getting me closer to proving eqns. $(1)$ and $(2)$. Even if the logic used is valid, all I have shown so far is that $$\mathcal{F}\left[\phi (\boldsymbol{x})\right]=\frac{\hat a(\boldsymbol{k})+\hat a^\dagger(\boldsymbol{k})}{2E(\boldsymbol{k})}$$ I could arrive at a similar expression by taking the Fourier transform of $(4)$, but I don't see the point as eqn. $(5)$ does not seem to be of any use to show eqns. $(1)$ and $(2)$.
Could someone please explain where I am going wrong or address the questions I asked regarding steps $(\mathrm{A})\to (\mathrm{B})$ and $(\mathrm{B})\to (\mathrm{C})$?
Update:
I have been given two answers, in the comment below the answer by @Mark Viola, I asked if there is a way of arriving at the results, $(1)$ and $(2)$ without interchanging the order of integration, as in that answer it is mentioned explicitly mentioned that this is NOT legitimate.
I haven't received any response since, so I will have to assume that there is no way of arriving at $(1)$ and $(2)$ with the order of integration maintained. So as a final question, what is the justification for swapping the order of integration?
Starting from eqs. (3) and (4) of your original question, you find $$\phi(\mathbf{x})=\int \!\frac{d^3k}{(2\pi)^3 2 E(\mathbf{k})}\left[a(\mathbf{k})e^{i \mathbf{k}\cdot \mathbf{x}}+a^\dagger(\mathbf{k})e^{-i \mathbf{k}\cdot \mathbf{x}}\right]=\int \!\frac{d^3k}{(2\pi)^3}\frac{a(\mathbf{k})+a^\dagger(\mathbf{-k})}{2E(\mathbf{k})}\, e^{i\mathbf{k}\cdot \mathbf{x}} $$ and $$ \pi(\mathbf{x})=\frac{1}{2i} \int \! \frac{d^3k}{(2 \pi)^3}\left[a(\mathbf{k})e^{i \mathbf{k} \cdot \mathbf{x}}-a^\dagger(\mathbf{k})e^{-i \mathbf{k}\cdot\mathbf{x}} \right] = \frac{1}{2i}\int \! \frac{d^3 k}{(2\pi)^3}\left[a(\mathbf{k})-a^\dagger(-\mathbf{k})\right]e^{i \mathbf{k}\cdot \mathbf{x}}, $$ where $E(\mathbf{k}):=\sqrt{\mathbf{k}^2+m^2}=E(-\mathbf{k})$. In the second terms of the original integrands, the transformation of variables $\mathbf{k} \to \mathbf{-k}$ was performed in order to arrive at the final integral representation of the field $\phi(\mathbf{x})$ and its canonically conjugated field $\pi(\mathbf{x})$. Written in this form, you can immediately read off the inverse Fourier transforms $$\frac{a(\mathbf{k})+a^\dagger(-\mathbf{k})}{2E(\mathbf{k})}= \int \! d^3x \, \phi(\mathbf{x})\, e^{-i \mathbf{k}\cdot \mathbf{x}}, \qquad \frac{a(\mathbf{k})-a^\dagger(-\mathbf{k})}{2i}= \int \!d^3 x \, \pi(\mathbf{x}) \, e^{-i \mathbf{k}\cdot{x}}$$ without the necessity of any lengthy calculation. Combining these two equations, you arrive at $$ a(\mathbf{k})= \int \! d^3 x\, \left[E(\mathbf{k}) \phi(\mathbf{x})+i \pi(\mathbf{x})\right]\, e^{-i \mathbf{k}\cdot\mathbf{x}}, \quad a^\dagger(\mathbf{k})=\int \! d^3 x \, \left[E(\mathbf{k}) \phi(\mathbf{x})-i \pi(\mathbf{x}) \right] e^{i \mathbf{k}\cdot \mathbf{x}},$$ showing that there were simply two typos ($e^{i \mathbf{k} \cdot \mathbf{x}} \leftrightarrow e^{-i \mathbf{k}\cdot \mathbf{x}}$) in eqs. (1) and (2) of your question. These typos are already present in the lecture notes.