Divergence between data from a Populated Array manual and from a variable

-3

I do not know how to handle this, if I put the data in the array manually the array comes correct if I put the same data coming from a variable the already wrong array. how can I make the variable work correctly

1 Option data entered manually in the array

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 a 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" ) 
    
asked by anonymous 04.04.2016 / 06:17

2 answers

1

There are a lot of problems in what you are 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_|

In the same way, you are clearly creating an array with a single value.

Here's an example of your second case:

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

In this way, you will have the same result as the previous example. One value only, in position zero.

If you want to put 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 he sends a value, he obeys. If you send for two, you also obey.

Regarding this here:

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

You are creating a value only, enclosed in single quotation marks ' . If you put it in an array, it will remain a single value. It all depends on where you get the value, to see the best way to split, or split.

If you really need to do some gambiarra, a 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 do not want the quotation marks 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 do not recommend this solution, if you see this in some real code, it is a bad sign. The correct solution is to get the values separately from the DB, and use separately in the array ().

    
04.04.2016 / 06:46
1

The first one has an array with 2 indexes.

In the second, you just generated an array with a unique index because the array will not be auto-built from a string as a parameter.

If you wanted to generate an array from a string, it is necessary for the string to have a pattern to be able to use a function such as explode() , to facilitate "conversion":

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

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

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

But it will still 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 $poli variable. For if something out of the standard comes, you will have a mistake in some process. If you are really sure that the string will have the same format, the routine will work, otherwise you will have to create something more consistent.

    
04.04.2016 / 06:57