2 arrays in foreach

0

Is there any way to put 2 arrays in the same foreach?

Let me explain what I need, I'm using the WideImage class to resize the images, I need to grab the image extension to save it later. My problem is, how can I get the length of each array image?

I can even do with a foreach type like this:

foreach ($image as $imagem) {
    foreach ($imagemExtensao as $extensao) {
        $extensaoDaImagem = pathinfo($extensao, PATHINFO_EXTENSION); 
        $imagemFinal = $imagem->resize(400, 300);
        $geraNome = md5(time().rand());
        $imagemFinal->saveToFile('imagens/usuarios/' . $geraNome .'.'.$extensaoDaImagem);
        $nomeCompleto = $geraNome.'.'.$extensaoDaImagem;
    }
} 

But it duplicates the images, the names, everything. So I have to do it once, or if you have another idea.

    
asked by anonymous 19.02.2015 / 21:33

1 answer

1

If there are two "parallel" arrays, numerically indexed, and with corresponding indexing, just loop one of them, and use the same index in the other. For example:

foreach ($image as $indice => $imagem) {
    $extensao = $imagemExtensao[$indice];
    $extensaoDaImagem = pathinfo($extensao, PATHINFO_EXTENSION); 
    $imagemFinal = $imagem->resize(400, 300);
    $geraNome = md5(time().rand());
    $imagemFinal->saveToFile('imagens/usuarios/' . $geraNome .'.'.$extensaoDaImagem);
    $nomeCompleto = $geraNome.'.'.$extensaoDaImagem;
} 
    
19.02.2015 / 22:07