Question about array in php

Asked

Viewed 79 times

-4

I’m cracking my head and I’m already out of ideas, summing up my code is like this:

<?php

$var = 'arquivo_1'; // podendo ser arquivo_1 ou arquivo_2

$arquivo_1 = array(
"algo1" => "alguem1",
"algo2" => "alguem2"
);

$arquivo_2 = array(
"algo1" => "alguem3",
"algo2" => "alguem4"
);

// a minha função é assim:

function array_file_writte($file_array,$array){ 
$content = serialize($array);
$fd = @fopen($file_array, 'w+');
fwrite($fd,$content);
fclose($fd);
chmod($file_array, 0644);
return true;
}

// se fosse assim fucionaria corretamente:

array_file_writte($file_array,$arquivo_1);

// mas o valor da variavel $var muda, então eu precisaria fazer assim:

$var_selecionada = '$'.$var;
array_file_writte($file_array,$var_selecionada);

//algum metodo???
// mas não dá certo por que o que ele grava no arquivo acaba não sendo a variavel serializada, alguma idéia????
  • 2

    What is the doubt? was not clear, could [Edit] the question and add the details.

  • I think I get it, try calling it that array_file_writte($file_array,$$var);

  • at that point array_file_writte($file_array, $arquivo_1); the variable $file_array not defined, so what does it contain? The file name?

  • 1

    The question is not clear, please edit it and give more details.

  • 1

    It is not clear enough, what the proposed problem, I recommend seeing this link and edit your question.

  • If I understand correctly you want $$var instead of '$'. $var. And this doubt has nothing to do with arrays like the title and hashtag left to understand.

Show 1 more comment

2 answers

2

What if Voce simply changes the data structure/array?

$arquivos = array(
    'arquivo_1' = array(
        "algo1" => "alguem1",
        "algo2" => "alguem2"
    ),
    'arquivo_2' = array(
        "algo1" => "alguem3",
        "algo2" => "alguem4"
    ),
);

Now take the figures like this

foreach($arquivos as $nome=>$valores){
    //... valores vai ser um array e $nome o indice atual, no caso arquivo_1 ou arquivo_2
    //processa a escrita dos arquivos aqui
    array_file_writte($nome, $valores);
}

And change the writing function to something like

function array_file_writte($nomeArquivo, $lista){ 
    $content = serialize($lista);
    $fd = @fopen($nomeArquivo, 'w+');
    fwrite($fd, $content);
    fclose($fd);
    chmod($file_array, 0644);
    return true;
}

2

Just change the $var_selecionada = '$'.$var; for $var_selecionada = $$var;.


You must use $$var instead of '$'.$var, this way will create a variable, see this here. The way you’re currently doing $ is being passed as string, which does not work.

In the manner stated:

$var = 'arquivo_1';

$arquivo_1 = array(
"algo1" => "alguem1",
"algo2" => "alguem2"
);

$arquivo_2 = array(
"algo1" => "alguem3",
"algo2" => "alguem4"
);

$var_selecionada = $$var;

var_dump($var_selecionada); // Somente para ver o resultado

Will return:

array(2) { 
  ["algo1"]=> string(7) "alguem1" 
  ["algo2"]=> string(7) "alguem2" 
}

This one right here.

The variable variable, the $$var, will execute the $arquivo_1, because the $var was defined by $var = 'arquivo_1', in the end you will do as you wish.

Browser other questions tagged

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