Divergence between manual populated A Array and variable data

Asked

Viewed 30 times

-3

I’m not sure how to treat this, if I put the data in the array manually the array will be correct if I put the same data coming from a variable the array already wrong. how can I make the variable work correctly

1 Manually entered data in array option

print_r ($poligono = array("-22.891996181150216, -43.127254512695345", "-22.86529776201615, -43.09548266210936"));
Resultado Correto
//Array ( [0] => -22.891996181150216, -43.127254512695345 [1] => -22.86529776201615, -43.09548266210936 ) 

2 Option identical data only coming in one variable

poli = '"-22.891996181150216, -43.127254512695345", "-22.86529776201615, -43.09548266210936"';
print_r ($poligono = array($poli));
Resultado Errado
//Array ( [0] => "-22.891996181150216, -43.127254512695345", "-22.86529776201615, -43.09548266210936" ) 
  • Hello. basically "-22.891996181150216, -43.127254512695345", "-22.86529776201615, -43.09548266210936" works my script if put on a variable does not work

  • more can not stay only in the first position

  • this values comes from the bank , the question is why manually goes and the variable does not

2 answers

1

There are a lot of problems in what you’re doing.

See the first case:

$poligono = array( "-22.8919, -43.1272", "-22.8652, -43.0954" );
                   |_____valor 1______|  |_____valor 2______|

You are clearly creating an array with two elements.

Let’s go to the second case:

$poligono = array(   $poli   );
                  |_valor 1_|

Similarly, you are clearly creating an array with only one value.

See an example like your second case:

$poligono = array( '"-22.8919, -43.1272", "-22.8652, -43.0954"' );
                   |__________________valor 1_________________|

This way you will get the same result as the previous example. A value only, at zero position.

If you want to place values in separate positions, you have to provide them separately:

$valor1 = "-22.8919, -43.1272";
$valor2 = "-22.8652, -43.0954";
$poligono = array( $valor1, $valor2 );

Basically PHP will do what you tell it to do. If sent by a value, it obeys. If sent by two, also obeys.

As for this here:

$poli = '"-22.8919, -43.1272", "-22.8652, -43.0954"';

You are creating a single value, between simple quotes '. If you put it in an array, it will remain a single value. It all depends on where you take the value, to see the best way to split, or to use split.

If you really need to do a gambit, one solution is this:

$poli = '"-22.8919, -43.1272", "-22.8652, -43.0954"';
$poli = str_replace( ' ', '', $poli );
$poli = str_replace( '","', '"|"', $poli );
print_r ( $poligono = explode( '|', $poli ) );

And if you don’t want the quotes in the result:

$poli = '"-22.8919, -43.1272", "-22.8652, -43.0954"';
$poli = str_replace( ' ', '', $poli );
$poli = str_replace( '","', '"|"', $poli );
$poli = str_replace( '"', '', $poli );
print_r ( $poligono = explode( '|', $poli ) );

But I don’t recommend this solution. The correct solution is to take the values separately from DB, and use separately in the array().

  • OK understood... just in my case how could separate these values are in the variable

  • po... almost now the division was much Array ( [0] => "-22.891996181150216 [1] => 43.127254512695345" [2] => "-22.86529776201615 [3] => -43.09548266210936" ) would have to be in 2 positions

  • @Fabiohenrique see result working -> http://ideone.com/Az5uu6

  • 1

    Show Thank you for being such a cunning will break my branch momentarily

1

In the first you have an array with 2 indexes.

In the second, it only generated an array with a single index because the array will not be auto-built from a string as parameter.

If you wanted to generate an array from a string, you need the string to have a pattern in order to use a function like explode(), to facilitate the "conversion":

$poli = '"-22.891996181150216, -43.127254512695345", "-22.86529776201615, -43.09548266210936"';

$poligono = explode('",', $poli);
print_r($poligono);

The logic here is to use ", as delimiter. Thus, the function explode() will generate an array with 2 indexes or the number delimiters you find.

But still it will have a strange result

Array
(
    [0] => "-22.891996181150216, -43.127254512695345
    [1] =>  "-22.86529776201615, -43.09548266210936"
)

Note the double quotes.

A more consistent example to generate the array:

$poli = '"-22.891996181150216, -43.127254512695345", "-22.86529776201615, -43.09548266210936"';
$poli = explode(', ', str_replace('"', '', $poli));
$poligono = array(
    $poli[0].' '.$poli[1],
    $poli[2].' '.$poli[3]
);
print_r($poligono);


/*
retorna

Array
(
    [0] => -22.891996181150216 -43.127254512695345
    [1] => -22.86529776201615 -43.09548266210936
)
*/

Anyway, it depends on how the original string is received in the variable $poli. Because if something out of the ordinary comes along, you’re going to have an error in some process. If you are really sure that the string will have the same format, the routine will work, if not, you will have to create something more consistent.

Browser other questions tagged

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