2 arrays in foreach

Asked

Viewed 519 times

0

there is how 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 there, how can I get the extension of each image of the array?

I can even do with a foreach like that:

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;
    }
} 

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

  • Provide an example of the structure of arrays returned by the class, so we understand whether it is two-dimensional or two isolated, whether it is of the type assoc or index. Details like this will make you get faster answers :)

  • I use the Wideimage function to bring the image array follows function, $image = Wideimage::loadFromUpload('image', 'image.php'); Then I need to use another variable to bring the array with the names, to later take the image extension, $imageExtensao = $_FILES['image']['name']; so I need the foreach, to be able to scan the 2 arrays at the same time.

  • Do not use comments, edit the question, ask the question and provide the format of arrays (you can capture such format using print_r).

  • So you already have an array of extensions, is that it? And it has the same size as the image array? Are the indexes of the two arrays matching? For example, the image in $image[5] is the owner of the $imagemExtensao[5]?

  • Yeah, that’s right.

1 answer

2


If they 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;
} 
  • You’re the man, it worked right, thank you!!!

  • One more help, how can I send these image names to the bank? hehe I’m starting now, sorry ignorance

  • @user13840 There is already a totally new question, you can post separately. In the new question, you need to situate people about what you already know/tried, what is your bank and how is your table, if you already know connect to bank, etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.